-
Notifications
You must be signed in to change notification settings - Fork 27
/
identity.go
104 lines (96 loc) · 2.88 KB
/
identity.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
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/identity"
"github.com/authgear/authgear-server/pkg/util/graphqlutil"
)
const typeIdentity = "Identity"
var identityType = graphql.NewEnum(graphql.EnumConfig{
Name: "IdentityType",
Values: graphql.EnumValueConfigMap{
"LOGIN_ID": &graphql.EnumValueConfig{
Value: string(model.IdentityTypeLoginID),
},
"OAUTH": &graphql.EnumValueConfig{
Value: string(model.IdentityTypeOAuth),
},
"ANONYMOUS": &graphql.EnumValueConfig{
Value: string(model.IdentityTypeAnonymous),
},
"BIOMETRIC": &graphql.EnumValueConfig{
Value: string(model.IdentityTypeBiometric),
},
"PASSKEY": &graphql.EnumValueConfig{
Value: string(model.IdentityTypePasskey),
},
"SIWE": &graphql.EnumValueConfig{
Value: string(model.IdentityTypeSIWE),
},
},
})
var nodeIdentity = node(
graphql.NewObject(graphql.ObjectConfig{
Name: typeIdentity,
Interfaces: []*graphql.Interface{
nodeDefs.NodeInterface,
entityInterface,
},
Fields: graphql.Fields{
"id": entityIDField(typeIdentity),
"createdAt": entityCreatedAtField(loadIdentity),
"updatedAt": entityUpdatedAtField(loadIdentity),
"type": &graphql.Field{
Type: graphql.NewNonNull(identityType),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
ref := p.Source.(interface{ ToRef() *model.IdentityRef }).ToRef()
return string(ref.Type), nil
},
},
"claims": &graphql.Field{
Type: graphql.NewNonNull(IdentityClaims),
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 := loadIdentity(p.Context, p.Source)
claims := info.Map(func(value interface{}) (interface{}, error) {
info := value.(*identity.Info)
m := info.ToModel()
claims := m.Claims
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
},
},
},
}),
&identity.Info{},
func(ctx *Context, id string) (interface{}, error) {
return ctx.Identities.Load(id).Value, nil
},
)
var connIdentity = graphqlutil.NewConnectionDef(nodeIdentity)
func loadIdentity(ctx context.Context, obj interface{}) *graphqlutil.Lazy {
switch obj := obj.(type) {
case *identity.Info:
return graphqlutil.NewLazyValue(obj)
case *model.IdentityRef:
return GQLContext(ctx).Identities.Load(obj.ID)
default:
panic(fmt.Sprintf("graphql: unknown identity type: %T", obj))
}
}