-
Notifications
You must be signed in to change notification settings - Fork 28
/
do_ensure_session.go
231 lines (197 loc) · 7.11 KB
/
do_ensure_session.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package nodes
import (
"net/http"
"time"
"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/authenticationinfo"
"github.com/authgear/authgear-server/pkg/lib/interaction"
"github.com/authgear/authgear-server/pkg/lib/session"
"github.com/authgear/authgear-server/pkg/lib/session/idpsession"
)
func init() {
interaction.RegisterNode(&NodeDoEnsureSession{})
}
type EnsureSessionMode string
const (
EnsureSessionModeDefault EnsureSessionMode = ""
EnsureSessionModeCreate EnsureSessionMode = "create"
EnsureSessionModeUpdateOrCreate EnsureSessionMode = "update_or_create"
EnsureSessionModeNoop EnsureSessionMode = "noop"
)
type EdgeDoEnsureSession struct {
CreateReason session.CreateReason
Mode EnsureSessionMode
}
func (e *EdgeDoEnsureSession) Instantiate(ctx *interaction.Context, graph *interaction.Graph, input interface{}) (interaction.Node, error) {
amr := graph.GetAMR()
userID := graph.MustGetUserID()
mode := e.Mode
if mode == EnsureSessionModeDefault {
mode = EnsureSessionModeCreate
}
attrs := session.NewAttrs(userID)
attrs.SetAMR(amr)
sessionToCreate, token := ctx.Sessions.MakeSession(attrs)
sessionCookie := ctx.CookieManager.ValueCookie(ctx.SessionCookie.Def, token)
authenticationInfo := sessionToCreate.GetAuthenticationInfo()
authenticationInfo.ShouldFireAuthenticatedEventWhenIssueOfflineGrant = mode == EnsureSessionModeNoop && e.CreateReason == session.CreateReasonLogin
authenticationInfoEntry := authenticationinfo.NewEntry(authenticationInfo, ctx.OAuthSessionID)
var updateSessionID string
var updateSessionAMR []string
if mode == EnsureSessionModeUpdateOrCreate {
s := session.GetSession(ctx.Request.Context())
if idp, ok := s.(*idpsession.IDPSession); ok && idp.GetUserID() == userID {
updateSessionID = idp.ID
updateSessionAMR = amr
sessionToCreate = nil
sessionCookie = nil
}
}
if mode == EnsureSessionModeNoop {
updateSessionID = ""
updateSessionAMR = nil
sessionToCreate = nil
sessionCookie = nil
}
sameSiteStrictCookie := ctx.CookieManager.ValueCookie(
ctx.SessionCookie.SameSiteStrictDef,
"true",
)
now := ctx.Clock.NowUTC()
return &NodeDoEnsureSession{
CreateReason: e.CreateReason,
SessionToCreate: sessionToCreate,
AuthenticationInfoEntry: authenticationInfoEntry,
UpdateLoginTime: now,
UpdateSessionID: updateSessionID,
UpdateSessionAMR: updateSessionAMR,
SessionCookie: sessionCookie,
SameSiteStrictCookie: sameSiteStrictCookie,
IsAdminAPI: interaction.IsAdminAPI(input),
}, nil
}
type NodeDoEnsureSession struct {
CreateReason session.CreateReason `json:"reason"`
SessionToCreate *idpsession.IDPSession `json:"session_to_create,omitempty"`
AuthenticationInfoEntry *authenticationinfo.Entry `json:"authentication_info_entry,omitempty"`
UpdateLoginTime time.Time `json:"update_login_time,omitempty"`
UpdateSessionID string `json:"update_session_id,omitempty"`
UpdateSessionAMR []string `json:"update_session_amr,omitempty"`
SessionCookie *http.Cookie `json:"session_cookie,omitempty"`
SameSiteStrictCookie *http.Cookie `json:"same_site_strict_cookie,omitempty"`
IsAdminAPI bool `json:"is_admin_api"`
}
// GetCookies implements CookiesGetter
func (n *NodeDoEnsureSession) GetCookies() (cookies []*http.Cookie) {
if n.SessionCookie != nil {
cookies = append(cookies, n.SessionCookie)
}
if n.SameSiteStrictCookie != nil {
cookies = append(cookies, n.SameSiteStrictCookie)
}
return
}
func (n *NodeDoEnsureSession) GetAuthenticationInfoEntry() *authenticationinfo.Entry {
return n.AuthenticationInfoEntry
}
func (n *NodeDoEnsureSession) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
// nolint:gocognit
func (n *NodeDoEnsureSession) GetEffects() ([]interaction.Effect, error) {
return []interaction.Effect{
interaction.EffectOnCommit(func(ctx *interaction.Context, graph *interaction.Graph, nodeIndex int) error {
return ctx.AuthenticationInfoService.Save(n.AuthenticationInfoEntry)
}),
interaction.EffectOnCommit(func(ctx *interaction.Context, graph *interaction.Graph, nodeIndex int) error {
if n.CreateReason != session.CreateReasonPromote {
return nil
}
userID := graph.MustGetUserID()
newUserRef := model.UserRef{
Meta: model.Meta{
ID: userID,
},
}
anonUserRef := newUserRef
if identityCheck, ok := getIdentityConflictNode(graph); ok && identityCheck.DuplicatedIdentity != nil {
// Logging as existing user when promoting: old user is different.
anonUserRef = model.UserRef{
Meta: model.Meta{
ID: identityCheck.NewIdentity.UserID,
},
}
}
var identityModels []model.Identity
for _, info := range graph.GetUserNewIdentities() {
identityModels = append(identityModels, info.ToModel())
}
err := ctx.Events.DispatchEventOnCommit(&nonblocking.UserAnonymousPromotedEventPayload{
AnonymousUserRef: anonUserRef,
UserRef: newUserRef,
Identities: identityModels,
AdminAPI: n.IsAdminAPI,
})
if err != nil {
return err
}
return nil
}),
interaction.EffectOnCommit(func(ctx *interaction.Context, graph *interaction.Graph, nodeIndex int) error {
userID := graph.MustGetUserID()
var err error
if !n.UpdateLoginTime.IsZero() {
err = ctx.Users.UpdateLoginTime(userID, n.UpdateLoginTime)
if err != nil {
return err
}
}
userRef := model.UserRef{
Meta: model.Meta{
ID: userID,
},
}
if n.SessionToCreate != nil {
if n.CreateReason == session.CreateReasonLogin || n.CreateReason == session.CreateReasonReauthenticate {
// ref: https://github.com/authgear/authgear-server/issues/2930
// For authentication that involves IDP session will dispatch user.authenticated event here
// For authentication that suppresses IDP session. e.g. biometric login
// They are handled in their own node.
err = ctx.Events.DispatchEventOnCommit(&nonblocking.UserAuthenticatedEventPayload{
UserRef: userRef,
Session: *n.SessionToCreate.ToAPIModel(),
AdminAPI: n.IsAdminAPI,
})
if err != nil {
return err
}
}
}
if n.SessionToCreate != nil {
err = ctx.Sessions.Create(n.SessionToCreate)
if err != nil {
return err
}
// Clean up unreachable IdP Session.
s := session.GetSession(ctx.Request.Context())
if s != nil && s.SessionType() == session.TypeIdentityProvider {
err = ctx.SessionManager.RevokeWithoutEvent(s)
if err != nil {
return err
}
}
}
if n.UpdateSessionID != "" {
err = ctx.Sessions.Reauthenticate(n.UpdateSessionID, n.UpdateSessionAMR)
if err != nil {
return err
}
}
return nil
}),
}, nil
}
func (n *NodeDoEnsureSession) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
return graph.Intent.DeriveEdgesForNode(graph, n)
}