/
authenticator.go
131 lines (122 loc) · 3.88 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package graphql
import (
"context"
"fmt"
"github.com/graphql-go/graphql"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
"github.com/authgear/authgear-server/pkg/util/graphqlutil"
)
const typeAuthenticator = "Authenticator"
var authenticatorType = graphql.NewEnum(graphql.EnumConfig{
Name: "AuthenticatorType",
Values: graphql.EnumValueConfigMap{
"PASSWORD": &graphql.EnumValueConfig{
Value: string(model.AuthenticatorTypePassword),
},
"TOTP": &graphql.EnumValueConfig{
Value: string(model.AuthenticatorTypeTOTP),
},
"OOB_OTP_EMAIL": &graphql.EnumValueConfig{
Value: string(model.AuthenticatorTypeOOBEmail),
},
"OOB_OTP_SMS": &graphql.EnumValueConfig{
Value: string(model.AuthenticatorTypeOOBSMS),
},
"PASSKEY": &graphql.EnumValueConfig{
Value: string(model.AuthenticatorTypePasskey),
},
},
})
var authenticatorKind = graphql.NewEnum(graphql.EnumConfig{
Name: "AuthenticatorKind",
Values: graphql.EnumValueConfigMap{
"PRIMARY": &graphql.EnumValueConfig{
Value: "primary",
},
"SECONDARY": &graphql.EnumValueConfig{
Value: "secondary",
},
},
})
var nodeAuthenticator = node(
graphql.NewObject(graphql.ObjectConfig{
Name: typeAuthenticator,
Interfaces: []*graphql.Interface{
nodeDefs.NodeInterface,
entityInterface,
},
Fields: graphql.Fields{
"id": entityIDField(typeAuthenticator),
"createdAt": entityCreatedAtField(loadAuthenticator),
"updatedAt": entityUpdatedAtField(loadAuthenticator),
"type": &graphql.Field{
Type: graphql.NewNonNull(authenticatorType),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
ref := p.Source.(interface{ ToRef() *authenticator.Ref }).ToRef()
return string(ref.Type), nil
},
},
"kind": &graphql.Field{
Type: graphql.NewNonNull(authenticatorKind),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
info := loadAuthenticator(p.Context, p.Source)
return info.Map(func(value interface{}) (interface{}, error) {
a := value.(*authenticator.Info)
return string(a.Kind), nil
}).Value, nil
},
},
"isDefault": &graphql.Field{
Type: graphql.NewNonNull(graphql.Boolean),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
info := loadAuthenticator(p.Context, p.Source)
return info.Map(func(value interface{}) (interface{}, error) {
a := value.(*authenticator.Info)
return a.IsDefault, nil
}).Value, nil
},
},
"claims": &graphql.Field{
Type: graphql.NewNonNull(AuthenticatorClaims),
Args: map[string]*graphql.ArgumentConfig{
"names": {Type: graphql.NewList(graphql.NewNonNull(graphql.String))},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
names, hasNames := p.Args["names"].([]interface{})
info := loadAuthenticator(p.Context, p.Source)
claims := info.Map(func(value interface{}) (interface{}, error) {
claims := value.(*authenticator.Info).ToPublicClaims()
if hasNames {
filteredClaims := make(map[string]interface{})
for _, name := range names {
name := name.(string)
if value, ok := claims[name]; ok {
filteredClaims[name] = value
}
}
claims = filteredClaims
}
return claims, nil
})
return claims.Value, nil
},
},
},
}),
&authenticator.Info{},
func(ctx *Context, id string) (interface{}, error) {
return ctx.Authenticators.Load(id).Value, nil
},
)
var connAuthenticator = graphqlutil.NewConnectionDef(nodeAuthenticator)
func loadAuthenticator(ctx context.Context, obj interface{}) *graphqlutil.Lazy {
switch obj := obj.(type) {
case *authenticator.Info:
return graphqlutil.NewLazyValue(obj)
case *authenticator.Ref:
return GQLContext(ctx).Authenticators.Load(obj.ID)
default:
panic(fmt.Sprintf("graphql: unknown authenticator type: %T", obj))
}
}