-
Notifications
You must be signed in to change notification settings - Fork 27
/
ensure_remove_anonymous_identity.go
98 lines (88 loc) · 2.96 KB
/
ensure_remove_anonymous_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
package nodes
import (
"github.com/authgear/authgear-server/pkg/api/event/nonblocking"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/lib/interaction"
)
func init() {
interaction.RegisterNode(&NodeEnsureRemoveAnonymousIdentity{})
}
type EdgeEnsureRemoveAnonymousIdentity struct{}
func (e *EdgeEnsureRemoveAnonymousIdentity) Instantiate(ctx *interaction.Context, graph *interaction.Graph, input interface{}) (interaction.Node, error) {
return &NodeEnsureRemoveAnonymousIdentity{
IsAdminAPI: interaction.IsAdminAPI(input),
}, nil
}
type NodeEnsureRemoveAnonymousIdentity struct {
AnonymousIdentity *identity.Info `json:"anonymous_identity,omitempty"`
IsAdminAPI bool `json:"is_admin_api"`
}
func (n *NodeEnsureRemoveAnonymousIdentity) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
userID := graph.MustGetUserID()
iis, err := ctx.Identities.ListByUser(graph.MustGetUserID())
if err != nil {
return err
}
anonymousIdentities := identity.ApplyFilters(
iis,
identity.KeepType(model.IdentityTypeAnonymous),
)
if len(anonymousIdentities) > 1 {
panic("interaction: more than 1 anonymous identities: " + userID)
}
if len(anonymousIdentities) == 1 {
if !n.IsAdminAPI {
// This node is used when adding new identity
// when admin add a new identifiable identity to the anonymous user
// anonymous will be removed
panic("interaction: unexpected anonymous user adding identity")
}
n.AnonymousIdentity = anonymousIdentities[0]
}
return nil
}
func (n *NodeEnsureRemoveAnonymousIdentity) GetEffects() ([]interaction.Effect, error) {
return []interaction.Effect{
interaction.EffectRun(func(ctx *interaction.Context, graph *interaction.Graph, nodeIndex int) error {
if n.AnonymousIdentity == nil {
return nil
}
err := ctx.Identities.Delete(n.AnonymousIdentity)
if err != nil {
return err
}
return nil
}),
interaction.EffectOnCommit(func(ctx *interaction.Context, graph *interaction.Graph, nodeIndex int) error {
if n.AnonymousIdentity == nil {
return nil
}
// if anonymous identity is removed during adding identity
// it is a promotion flow
userID := graph.MustGetUserID()
anonUserRef := model.UserRef{
Meta: model.Meta{
ID: userID,
},
}
var identityModels []model.Identity
for _, info := range graph.GetUserNewIdentities() {
identityModels = append(identityModels, info.ToModel())
}
err := ctx.Events.DispatchEventOnCommit(&nonblocking.UserAnonymousPromotedEventPayload{
AnonymousUserRef: anonUserRef,
UserRef: anonUserRef,
Identities: identityModels,
AdminAPI: n.IsAdminAPI,
})
if err != nil {
return err
}
return nil
}),
}, nil
}
func (n *NodeEnsureRemoveAnonymousIdentity) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
return graph.Intent.DeriveEdgesForNode(graph, n)
}