support unlinking external authentication
This commit is contained in:
@@ -11,7 +11,54 @@ type UserExternalAuth struct {
|
||||
CreatedUnixTime int64
|
||||
}
|
||||
|
||||
// UserExternalAuthRevokeRequest represents all parameters of user external auth revoke request
|
||||
type UserExternalAuthRevokeRequest struct {
|
||||
ExternalAuthType core.UserExternalAuthType `json:"externalAuthType" binding:"required,notBlank"`
|
||||
// UserExternalAuthUnlinkRequest represents all parameters of user external auth unlink request
|
||||
type UserExternalAuthUnlinkRequest struct {
|
||||
ExternalAuthType string `json:"externalAuthType" binding:"required,notBlank"`
|
||||
Password string `json:"password" binding:"required,min=6,max=128"`
|
||||
}
|
||||
|
||||
// UserExternalAuthInfoResponse represents a view-object of user external auth
|
||||
type UserExternalAuthInfoResponse struct {
|
||||
ExternalAuthCategory string `json:"externalAuthCategory"`
|
||||
ExternalAuthType core.UserExternalAuthType `json:"externalAuthType"`
|
||||
Linked bool `json:"linked"`
|
||||
ExternalUsername string `json:"externalUsername,omitempty"`
|
||||
CreatedAt int64 `json:"createdAt,omitempty"`
|
||||
}
|
||||
|
||||
// ToUserExternalAuthInfoResponse returns a view-object according to database model
|
||||
func (a *UserExternalAuth) ToUserExternalAuthInfoResponse() *UserExternalAuthInfoResponse {
|
||||
return &UserExternalAuthInfoResponse{
|
||||
ExternalAuthCategory: a.ExternalAuthType.GetCategory(),
|
||||
ExternalAuthType: a.ExternalAuthType,
|
||||
Linked: true,
|
||||
ExternalUsername: a.ExternalUsername,
|
||||
CreatedAt: a.CreatedUnixTime,
|
||||
}
|
||||
}
|
||||
|
||||
// UserExternalAuthInfoResponsesSlice represents the slice data structure of UserExternalAuthInfoResponse
|
||||
type UserExternalAuthInfoResponsesSlice []*UserExternalAuthInfoResponse
|
||||
|
||||
// Len returns the count of items
|
||||
func (a UserExternalAuthInfoResponsesSlice) Len() int {
|
||||
return len(a)
|
||||
}
|
||||
|
||||
// Swap swaps two items
|
||||
func (a UserExternalAuthInfoResponsesSlice) Swap(i, j int) {
|
||||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
|
||||
// Less reports whether the first item is less than the second one
|
||||
func (a UserExternalAuthInfoResponsesSlice) Less(i, j int) bool {
|
||||
if a[i].Linked && !a[j].Linked {
|
||||
return true
|
||||
} else if !a[i].Linked && a[j].Linked {
|
||||
return false
|
||||
} else if !a[i].Linked && !a[j].Linked {
|
||||
return a[i].ExternalAuthType < a[j].ExternalAuthType
|
||||
}
|
||||
|
||||
return a[i].CreatedAt > a[j].CreatedAt
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
)
|
||||
|
||||
func TestUserExternalAuthInfoResponsesSliceLess(t *testing.T) {
|
||||
var userExternalAuthInfoResponsesSlice UserExternalAuthInfoResponsesSlice
|
||||
userExternalAuthInfoResponsesSlice = append(userExternalAuthInfoResponsesSlice, &UserExternalAuthInfoResponse{
|
||||
ExternalAuthType: core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_OIDC,
|
||||
Linked: true,
|
||||
ExternalUsername: "test",
|
||||
CreatedAt: int64(1),
|
||||
})
|
||||
userExternalAuthInfoResponsesSlice = append(userExternalAuthInfoResponsesSlice, &UserExternalAuthInfoResponse{
|
||||
ExternalAuthType: core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_NEXTCLOUD,
|
||||
Linked: false,
|
||||
})
|
||||
userExternalAuthInfoResponsesSlice = append(userExternalAuthInfoResponsesSlice, &UserExternalAuthInfoResponse{
|
||||
ExternalAuthType: core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_GITEA,
|
||||
Linked: false,
|
||||
})
|
||||
userExternalAuthInfoResponsesSlice = append(userExternalAuthInfoResponsesSlice, &UserExternalAuthInfoResponse{
|
||||
ExternalAuthType: core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_GITHUB,
|
||||
Linked: true,
|
||||
ExternalUsername: "test4",
|
||||
CreatedAt: int64(2),
|
||||
})
|
||||
|
||||
sort.Sort(userExternalAuthInfoResponsesSlice)
|
||||
|
||||
assert.Equal(t, core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_GITHUB, userExternalAuthInfoResponsesSlice[0].ExternalAuthType)
|
||||
assert.Equal(t, core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_OIDC, userExternalAuthInfoResponsesSlice[1].ExternalAuthType)
|
||||
assert.Equal(t, core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_GITEA, userExternalAuthInfoResponsesSlice[2].ExternalAuthType)
|
||||
assert.Equal(t, core.USER_EXTERNAL_AUTH_TYPE_OAUTH2_NEXTCLOUD, userExternalAuthInfoResponsesSlice[3].ExternalAuthType)
|
||||
}
|
||||
Reference in New Issue
Block a user