/
authflow_routes.go
450 lines (421 loc) · 17.8 KB
/
authflow_routes.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package webapp
import (
"errors"
"fmt"
"net/http"
"net/url"
"github.com/authgear/authgear-server/pkg/api"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/api/model"
authflow "github.com/authgear/authgear-server/pkg/lib/authenticationflow"
"github.com/authgear/authgear-server/pkg/lib/authenticationflow/declarative"
"github.com/authgear/authgear-server/pkg/lib/authn/otp"
"github.com/authgear/authgear-server/pkg/lib/authn/user"
"github.com/authgear/authgear-server/pkg/lib/config"
)
const (
AuthflowRouteLogin = "/login"
AuthflowRouteSignup = "/signup"
AuthflowRoutePromote = "/flows/promote_user"
AuthflowRouteReauth = "/reauth"
// AuthflowRouteSignupLogin is login because login page has passkey.
AuthflowRouteSignupLogin = AuthflowRouteLogin
AuthflowRouteTerminateOtherSessions = "/authflow/terminate_other_sessions"
// nolint: gosec
AuthflowRoutePromptCreatePasskey = "/authflow/prompt_create_passkey"
AuthflowRouteViewRecoveryCode = "/authflow/view_recovery_code"
// nolint: gosec
AuthflowRouteCreatePassword = "/authflow/create_password"
// nolint: gosec
AuthflowRouteChangePassword = "/authflow/change_password"
// nolint: gosec
AuthflowRouteEnterPassword = "/authflow/enter_password"
AuthflowRouteEnterRecoveryCode = "/authflow/enter_recovery_code"
AuthflowRouteEnterOOBOTP = "/authflow/enter_oob_otp"
AuthflowRouteWhatsappOTP = "/authflow/whatsapp_otp"
AuthflowRouteOOBOTPLink = "/authflow/oob_otp_link"
AuthflowRouteEnterTOTP = "/authflow/enter_totp"
AuthflowRouteSetupTOTP = "/authflow/setup_totp"
AuthflowRouteSetupOOBOTP = "/authflow/setup_oob_otp"
// nolint: gosec
AuthflowRouteUsePasskey = "/authflow/use_passkey"
// nolint: gosec
AuthflowRouteForgotPassword = "/authflow/forgot_password"
// nolint: gosec
AuthflowRouteForgotPasswordOTP = "/authflow/forgot_password/otp"
// nolint: gosec
AuthflowRouteForgotPasswordSuccess = "/authflow/forgot_password/success"
// nolint: gosec
AuthflowRouteResetPassword = "/authflow/reset_password"
// nolint: gosec
AuthflowRouteResetPasswordSuccess = "/authflow/reset_password/success"
AuthflowRouteWechat = "/authflow/wechat"
// The following routes are dead ends.
AuthflowRouteAccountStatus = "/authflow/account_status"
AuthflowRouteNoAuthenticator = "/authflow/no_authenticator"
AuthflowRouteFinishFlow = "/authflow/finish"
)
type AuthflowNavigatorEndpointsProvider interface {
ErrorEndpointURL(uiImpl config.UIImplementation) *url.URL
SelectAccountEndpointURL(uiImpl config.UIImplementation) *url.URL
}
type AuthflowNavigator struct {
Endpoints AuthflowNavigatorEndpointsProvider
UIConfig *config.UIConfig
ErrorCookie *ErrorCookie
}
func (n *AuthflowNavigator) NavigateNonRecoverableError(r *http.Request, u *url.URL, e error) {
switch {
case user.IsAccountStatusError(e):
u.Path = AuthflowRouteAccountStatus
case errors.Is(e, api.ErrNoAuthenticator):
u.Path = AuthflowRouteNoAuthenticator
case errors.Is(e, authflow.ErrFlowNotFound):
u.Path = n.Endpoints.ErrorEndpointURL(n.UIConfig.Implementation).Path
case apierrors.IsKind(e, WebUIInvalidSession):
// Show WebUIInvalidSession error in different page.
u.Path = n.Endpoints.ErrorEndpointURL(n.UIConfig.Implementation).Path
case r.Method == http.MethodGet:
// If the request method is Get, avoid redirect back to the same path
// which causes infinite redirect loop
u.Path = n.Endpoints.ErrorEndpointURL(n.UIConfig.Implementation).Path
}
}
func (n *AuthflowNavigator) Navigate(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result) {
if s.HasBranchToTake() {
panic(fmt.Errorf("expected screen to have its branches taken"))
}
if s.StateTokenFlowResponse.Action.Type == authflow.FlowActionTypeFinished {
s.Advance(AuthflowRouteFinishFlow, result)
return
}
switch s.StateTokenFlowResponse.Type {
case authflow.FlowTypeSignup:
n.navigateSignup(s, r, webSessionID, result)
case authflow.FlowTypePromote:
n.navigatePromote(s, r, webSessionID, result)
case authflow.FlowTypeLogin:
n.navigateLogin(s, r, webSessionID, result)
case authflow.FlowTypeSignupLogin:
n.navigateSignupLogin(s, r, webSessionID, result)
case authflow.FlowTypeReauth:
n.navigateReauth(s, r, webSessionID, result)
case authflow.FlowTypeAccountRecovery:
n.navigateAccountRecovery(s, r, webSessionID, result)
default:
panic(fmt.Errorf("unexpected flow type: %v", s.StateTokenFlowResponse.Type))
}
}
func (n *AuthflowNavigator) NavigateChangePasswordSuccessPage(s *AuthflowScreen, r *http.Request, webSessionID string) (result *Result) {
return &Result{}
}
func (n *AuthflowNavigator) NavigateResetPasswordSuccessPage() string {
return AuthflowRouteResetPasswordSuccess
}
func (n *AuthflowNavigator) navigateSignup(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result) {
n.navigateSignupPromote(s, r, webSessionID, result, AuthflowRouteSignup)
}
func (n *AuthflowNavigator) navigatePromote(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result) {
n.navigateSignupPromote(s, r, webSessionID, result, AuthflowRoutePromote)
}
//nolint:gocognit
func (n *AuthflowNavigator) navigateSignupPromote(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result, expectedPath string) {
switch config.AuthenticationFlowStepType(s.StateTokenFlowResponse.Action.Type) {
case config.AuthenticationFlowStepTypeIdentify:
n.navigateStepIdentify(s, r, webSessionID, result, expectedPath)
case config.AuthenticationFlowStepTypeCreateAuthenticator:
// If the current step already tells the authentication, use it
authentication := s.StateTokenFlowResponse.Action.Authentication
if authentication == "" {
// Else, get it from the first option of the branch step
options := s.BranchStateTokenFlowResponse.Action.Data.(declarative.IntentSignupFlowStepCreateAuthenticatorData).Options
index := *s.Screen.TakenBranchIndex
option := options[index]
authentication = option.Authentication
}
switch authentication {
case config.AuthenticationFlowAuthenticationPrimaryPassword:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryPassword:
s.Advance(AuthflowRouteCreatePassword, result)
case config.AuthenticationFlowAuthenticationPrimaryOOBOTPEmail:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryOOBOTPEmail:
switch data := s.StateTokenFlowResponse.Action.Data.(type) {
case declarative.VerifyOOBOTPData:
// 1. We do not need to enter the target.
switch data.OTPForm {
case otp.FormCode:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case otp.FormLink:
s.Advance(AuthflowRouteOOBOTPLink, result)
default:
panic(fmt.Errorf("unexpected otp form: %v", data.OTPForm))
}
case declarative.IntentSignupFlowStepCreateAuthenticatorData:
// 2. We need to enter the target.
s.Advance(AuthflowRouteSetupOOBOTP, result)
default:
panic(fmt.Errorf("unexpected data: %T", s.StateTokenFlowResponse.Action.Data))
}
case config.AuthenticationFlowAuthenticationSecondaryTOTP:
s.Advance(AuthflowRouteSetupTOTP, result)
case config.AuthenticationFlowAuthenticationPrimaryOOBOTPSMS:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryOOBOTPSMS:
data := s.StateTokenFlowResponse.Action.Data
switch data := data.(type) {
case declarative.VerifyOOBOTPData:
// 1. We do not need to enter the target.
channel := data.Channel
switch channel {
case model.AuthenticatorOOBChannelSMS:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case model.AuthenticatorOOBChannelWhatsapp:
s.Advance(AuthflowRouteWhatsappOTP, result)
default:
panic(fmt.Errorf("unexpected channel: %v", channel))
}
case declarative.IntentSignupFlowStepCreateAuthenticatorData:
// 2. We need to enter the target.
s.Advance(AuthflowRouteSetupOOBOTP, result)
default:
panic(fmt.Errorf("unexpected data: %T", s.StateTokenFlowResponse.Action.Data))
}
default:
panic(fmt.Errorf("unexpected authentication: %v", s.StateTokenFlowResponse.Action.Authentication))
}
case config.AuthenticationFlowStepTypeVerify:
data := s.StateTokenFlowResponse.Action.Data.(declarative.VerifyOOBOTPData)
channel := data.Channel
switch data.OTPForm {
case otp.FormCode:
switch channel {
case model.AuthenticatorOOBChannelEmail:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case model.AuthenticatorOOBChannelSMS:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case model.AuthenticatorOOBChannelWhatsapp:
s.Advance(AuthflowRouteWhatsappOTP, result)
case "":
// Verify may not have branches.
s.Advance(AuthflowRouteEnterOOBOTP, result)
default:
panic(fmt.Errorf("unexpected channel: %v", channel))
}
case otp.FormLink:
s.Advance(AuthflowRouteOOBOTPLink, result)
}
case config.AuthenticationFlowStepTypeFillInUserProfile:
panic(fmt.Errorf("fill_in_user_profile is not supported yet"))
case config.AuthenticationFlowStepTypeViewRecoveryCode:
s.Advance(AuthflowRouteViewRecoveryCode, result)
case config.AuthenticationFlowStepTypePromptCreatePasskey:
s.Advance(AuthflowRoutePromptCreatePasskey, result)
default:
panic(fmt.Errorf("unexpected action type: %v", s.StateTokenFlowResponse.Action.Type))
}
}
func (n *AuthflowNavigator) navigateStepIdentify(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result, expectedPath string) {
identification := s.StateTokenFlowResponse.Action.Identification
switch identification {
case "":
fallthrough
case config.AuthenticationFlowIdentificationIDToken:
fallthrough
case config.AuthenticationFlowIdentificationEmail:
fallthrough
case config.AuthenticationFlowIdentificationPhone:
fallthrough
case config.AuthenticationFlowIdentificationUsername:
fallthrough
case config.AuthenticationFlowIdentificationPasskey:
// Redirect to the expected path with x_step set.
u := *r.URL
u.Path = expectedPath
q := u.Query()
q.Set(AuthflowQueryKey, s.Screen.StateToken.XStep)
u.RawQuery = q.Encode()
result.NavigationAction = "replace"
result.RedirectURI = u.String()
case config.AuthenticationFlowIdentificationOAuth:
data := s.StateTokenFlowResponse.Action.Data.(declarative.OAuthData)
switch data.OAuthProviderType {
case config.OAuthSSOProviderTypeWechat:
s.Advance(AuthflowRouteWechat, result)
default:
authorizationURL, _ := url.Parse(data.OAuthAuthorizationURL)
q := authorizationURL.Query()
state := AuthflowOAuthState{
WebSessionID: webSessionID,
XStep: s.Screen.StateToken.XStep,
ErrorRedirectURI: expectedPath,
}
q.Set("state", state.Encode())
authorizationURL.RawQuery = q.Encode()
result.NavigationAction = "redirect"
result.RedirectURI = authorizationURL.String()
}
default:
panic(fmt.Errorf("unexpected identification: %v", identification))
}
}
func (n *AuthflowNavigator) navigateLogin(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result) {
switch config.AuthenticationFlowStepType(s.StateTokenFlowResponse.Action.Type) {
case config.AuthenticationFlowStepTypeIdentify:
n.navigateStepIdentify(s, r, webSessionID, result, AuthflowRouteLogin)
case config.AuthenticationFlowStepTypeAuthenticate:
options := s.BranchStateTokenFlowResponse.Action.Data.(declarative.StepAuthenticateData).Options
index := *s.Screen.TakenBranchIndex
option := options[index]
switch option.Authentication {
case config.AuthenticationFlowAuthenticationPrimaryPassword:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryPassword:
s.Advance(AuthflowRouteEnterPassword, result)
case config.AuthenticationFlowAuthenticationPrimaryOOBOTPEmail:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryOOBOTPEmail:
switch data := s.StateTokenFlowResponse.Action.Data.(type) {
case declarative.VerifyOOBOTPData:
switch data.OTPForm {
case otp.FormCode:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case otp.FormLink:
s.Advance(AuthflowRouteOOBOTPLink, result)
default:
panic(fmt.Errorf("unexpected otp form: %v", data.OTPForm))
}
default:
panic(fmt.Errorf("unexpected data: %T", s.StateTokenFlowResponse.Action.Data))
}
case config.AuthenticationFlowAuthenticationSecondaryTOTP:
s.Advance(AuthflowRouteEnterTOTP, result)
case config.AuthenticationFlowAuthenticationPrimaryOOBOTPSMS:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryOOBOTPSMS:
channel := s.Screen.TakenChannel
switch channel {
case model.AuthenticatorOOBChannelSMS:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case model.AuthenticatorOOBChannelWhatsapp:
s.Advance(AuthflowRouteWhatsappOTP, result)
default:
panic(fmt.Errorf("unexpected channel: %v", channel))
}
case config.AuthenticationFlowAuthenticationRecoveryCode:
s.Advance(AuthflowRouteEnterRecoveryCode, result)
case config.AuthenticationFlowAuthenticationPrimaryPasskey:
s.Advance(AuthflowRouteUsePasskey, result)
default:
panic(fmt.Errorf("unexpected authentication: %v", option.Authentication))
}
case config.AuthenticationFlowStepTypeCheckAccountStatus:
s.Advance(AuthflowRouteAccountStatus, result)
case config.AuthenticationFlowStepTypeTerminateOtherSessions:
s.Advance(AuthflowRouteTerminateOtherSessions, result)
case config.AuthenticationFlowStepTypeChangePassword:
s.Advance(AuthflowRouteChangePassword, result)
case config.AuthenticationFlowStepTypePromptCreatePasskey:
s.Advance(AuthflowRoutePromptCreatePasskey, result)
default:
panic(fmt.Errorf("unexpected action type: %v", s.StateTokenFlowResponse.Action.Type))
}
}
func (n *AuthflowNavigator) navigateReauth(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result) {
switch config.AuthenticationFlowStepType(s.StateTokenFlowResponse.Action.Type) {
case config.AuthenticationFlowStepTypeIdentify:
n.navigateStepIdentify(s, r, webSessionID, result, AuthflowRouteReauth)
case config.AuthenticationFlowStepTypeAuthenticate:
options := s.BranchStateTokenFlowResponse.Action.Data.(declarative.StepAuthenticateData).Options
index := *s.Screen.TakenBranchIndex
option := options[index]
switch option.Authentication {
case config.AuthenticationFlowAuthenticationPrimaryPassword:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryPassword:
s.Advance(AuthflowRouteEnterPassword, result)
case config.AuthenticationFlowAuthenticationPrimaryOOBOTPEmail:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryOOBOTPEmail:
switch data := s.StateTokenFlowResponse.Action.Data.(type) {
case declarative.VerifyOOBOTPData:
switch data.OTPForm {
case otp.FormCode:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case otp.FormLink:
s.Advance(AuthflowRouteOOBOTPLink, result)
default:
panic(fmt.Errorf("unexpected otp form: %v", data.OTPForm))
}
default:
panic(fmt.Errorf("unexpected data: %T", s.StateTokenFlowResponse.Action.Data))
}
case config.AuthenticationFlowAuthenticationSecondaryTOTP:
s.Advance(AuthflowRouteEnterTOTP, result)
case config.AuthenticationFlowAuthenticationPrimaryOOBOTPSMS:
fallthrough
case config.AuthenticationFlowAuthenticationSecondaryOOBOTPSMS:
channel := s.Screen.TakenChannel
switch channel {
case model.AuthenticatorOOBChannelSMS:
s.Advance(AuthflowRouteEnterOOBOTP, result)
case model.AuthenticatorOOBChannelWhatsapp:
s.Advance(AuthflowRouteWhatsappOTP, result)
default:
panic(fmt.Errorf("unexpected channel: %v", channel))
}
case config.AuthenticationFlowAuthenticationPrimaryPasskey:
s.Advance(AuthflowRouteUsePasskey, result)
default:
panic(fmt.Errorf("unexpected authentication: %v", option.Authentication))
}
default:
panic(fmt.Errorf("unexpected action type: %v", s.StateTokenFlowResponse.Action.Type))
}
}
func (n *AuthflowNavigator) navigateSignupLogin(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result) {
switch config.AuthenticationFlowStepType(s.StateTokenFlowResponse.Action.Type) {
case config.AuthenticationFlowStepTypeIdentify:
n.navigateStepIdentify(s, r, webSessionID, result, AuthflowRouteSignupLogin)
default:
panic(fmt.Errorf("unexpected action type: %v", s.StateTokenFlowResponse.Action.Type))
}
}
func (n *AuthflowNavigator) navigateAccountRecovery(s *AuthflowScreenWithFlowResponse, r *http.Request, webSessionID string, result *Result) {
navigate := func(path string, query *url.Values) {
u := *r.URL
u.Path = path
q := u.Query()
q.Set(AuthflowQueryKey, s.Screen.StateToken.XStep)
for k, param := range *query {
for _, p := range param {
q.Add(k, p)
}
}
u.RawQuery = q.Encode()
result.NavigationAction = "replace"
result.RedirectURI = u.String()
}
switch config.AuthenticationFlowStepType(s.StateTokenFlowResponse.Action.Type) {
case config.AuthenticationFlowStepTypeIdentify:
navigate(AuthflowRouteForgotPassword, &url.Values{})
case config.AuthenticationFlowStepTypeSelectDestination:
navigate(AuthflowRouteForgotPassword, &url.Values{})
case config.AuthenticationFlowStepTypeVerifyAccountRecoveryCode:
data, ok := s.StateTokenFlowResponse.Action.Data.(declarative.IntentAccountRecoveryFlowStepVerifyAccountRecoveryCodeData)
if ok && data.OTPForm == declarative.AccountRecoveryOTPFormCode {
navigate(AuthflowRouteForgotPasswordOTP, &url.Values{"x_can_back_to_login": []string{"true"}})
} else {
navigate(AuthflowRouteForgotPasswordSuccess, &url.Values{"x_can_back_to_login": []string{"false"}})
}
case config.AuthenticationFlowStepTypeResetPassword:
navigate(AuthflowRouteResetPassword, &url.Values{})
default:
panic(fmt.Errorf("unexpected action type: %v", s.StateTokenFlowResponse.Action.Type))
}
}
func (n *AuthflowNavigator) NavigateSelectAccount(result *Result) {
url := n.Endpoints.SelectAccountEndpointURL(n.UIConfig.Implementation)
result.RedirectURI = url.String()
}