-
Notifications
You must be signed in to change notification settings - Fork 28
/
authenticator.go
52 lines (42 loc) · 1.54 KB
/
authenticator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package facade
import (
"sort"
apimodel "github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
interactionintents "github.com/authgear/authgear-server/pkg/lib/interaction/intents"
)
type AuthenticatorService interface {
Get(id string) (*authenticator.Info, error)
Count(userID string) (uint64, error)
ListRefsByUsers(userIDs []string, authenticatorType *apimodel.AuthenticatorType, authenticatorKind *authenticator.Kind) ([]*authenticator.Ref, error)
}
type AuthenticatorFacade struct {
Authenticators AuthenticatorService
Interaction InteractionService
}
func (f *AuthenticatorFacade) Get(id string) (*authenticator.Info, error) {
return f.Authenticators.Get(id)
}
func (f *AuthenticatorFacade) List(userID string, authenticatorType *apimodel.AuthenticatorType, authenticatorKind *authenticator.Kind) ([]*authenticator.Ref, error) {
refs, err := f.Authenticators.ListRefsByUsers([]string{userID}, authenticatorType, authenticatorKind)
if err != nil {
return nil, err
}
sort.Slice(refs, func(i, j int) bool {
if refs[i].CreatedAt != refs[j].CreatedAt {
return refs[i].CreatedAt.Before(refs[j].CreatedAt)
}
return refs[i].ID < refs[j].ID
})
return refs, nil
}
func (f *AuthenticatorFacade) Remove(authenticatorInfo *authenticator.Info) error {
_, err := f.Interaction.Perform(
interactionintents.NewIntentRemoveAuthenticator(authenticatorInfo.UserID),
&removeAuthenticatorInput{authenticatorInfo: authenticatorInfo},
)
if err != nil {
return err
}
return nil
}