/
select_account.go
267 lines (227 loc) · 8.01 KB
/
select_account.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
package authflowv2
import (
"net/http"
"github.com/authgear/authgear-server/pkg/api/model"
handlerwebapp "github.com/authgear/authgear-server/pkg/auth/handler/webapp"
"github.com/authgear/authgear-server/pkg/auth/handler/webapp/viewmodels"
"github.com/authgear/authgear-server/pkg/auth/webapp"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticationinfo"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/session"
"github.com/authgear/authgear-server/pkg/util/accesscontrol"
"github.com/authgear/authgear-server/pkg/util/httproute"
"github.com/authgear/authgear-server/pkg/util/slice"
"github.com/authgear/authgear-server/pkg/util/template"
)
var TemplateWebSelectAccountHTML = template.RegisterHTML(
"web/authflowv2/select_account.html",
handlerwebapp.Components...,
)
func ConfigureAuthflowV2SelectAccountRoute(route httproute.Route) httproute.Route {
return route.
WithMethods("OPTIONS", "POST", "GET").
WithPathPattern(AuthflowV2RouteSelectAccount)
}
type SelectAccountUserService interface {
Get(userID string, role accesscontrol.Role) (*model.User, error)
}
type SelectAccountIdentityService interface {
ListByUser(userID string) ([]*identity.Info, error)
}
type SelectAccountAuthenticationInfoService interface {
Save(entry *authenticationinfo.Entry) error
}
type SelectAccountUIInfoResolver interface {
SetAuthenticationInfoInQuery(redirectURI string, e *authenticationinfo.Entry) string
}
type SelectAccountViewModel struct {
IdentityDisplayName string
UserProfile handlerwebapp.UserProfile
}
type AuthflowV2SelectAccountHandler struct {
ControllerFactory handlerwebapp.ControllerFactory
BaseViewModel *viewmodels.BaseViewModeler
Renderer handlerwebapp.Renderer
AuthenticationConfig *config.AuthenticationConfig
SignedUpCookie webapp.SignedUpCookieDef
Users SelectAccountUserService
Identities SelectAccountIdentityService
AuthenticationInfoService SelectAccountAuthenticationInfoService
UIInfoResolver SelectAccountUIInfoResolver
Cookies handlerwebapp.CookieManager
OAuthConfig *config.OAuthConfig
UIConfig *config.UIConfig
OAuthClientResolver handlerwebapp.WebappOAuthClientResolver
}
func (h *AuthflowV2SelectAccountHandler) GetData(r *http.Request, rw http.ResponseWriter, userID string) (map[string]interface{}, error) {
data := make(map[string]interface{})
baseViewModel := h.BaseViewModel.ViewModel(r, rw)
viewmodels.Embed(data, baseViewModel)
identities, err := h.Identities.ListByUser(userID)
if err != nil {
return nil, err
}
user, err := h.Users.Get(userID, accesscontrol.RoleGreatest)
if err != nil {
return nil, err
}
userProfile := handlerwebapp.GetUserProfile(user)
displayID := handlerwebapp.IdentitiesDisplayName(identities)
selectAccountViewModel := SelectAccountViewModel{
IdentityDisplayName: displayID,
UserProfile: userProfile,
}
viewmodels.Embed(data, selectAccountViewModel)
return data, nil
}
// nolint: gocognit
func (h *AuthflowV2SelectAccountHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctrl, err := h.ControllerFactory.New(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
idpSession := session.GetSession(r.Context())
webSession := webapp.GetSession(r.Context())
oauthSessionID := ""
loginPrompt := false
userIDHint := ""
canUseIntentReauthenticate := false
suppressIDPSessionCookie := false
oauthProviderAlias := ""
if webSession != nil {
oauthSessionID = webSession.OAuthSessionID
loginPrompt = slice.ContainsString(webSession.Prompt, "login")
userIDHint = webSession.UserIDHint
canUseIntentReauthenticate = webSession.CanUseIntentReauthenticate
suppressIDPSessionCookie = webSession.SuppressIDPSessionCookie
oauthProviderAlias = webSession.OAuthProviderAlias
}
// When x_suppress_idp_session_cookie is true, ignore IDP session cookie.
if suppressIDPSessionCookie {
idpSession = nil
}
continueWithCurrentAccount := func() error {
redirectURI := ""
// Complete the web session and redirect to web session's RedirectURI
if webSession != nil {
redirectURI = webSession.RedirectURI
if err := ctrl.DeleteSession(webSession.ID); err != nil {
return err
}
}
if redirectURI == "" {
redirectURI = webapp.DerivePostLoginRedirectURIFromRequest(r, h.OAuthClientResolver, h.UIConfig)
}
// Write authentication info cookie
if idpSession != nil {
info := idpSession.GetAuthenticationInfo()
entry := authenticationinfo.NewEntry(info, oauthSessionID)
err := h.AuthenticationInfoService.Save(entry)
if err != nil {
return err
}
redirectURI = h.UIInfoResolver.SetAuthenticationInfoInQuery(redirectURI, entry)
}
http.Redirect(w, r, redirectURI, http.StatusFound)
return nil
}
gotoSignupOrLogin := func() {
// Page has the highest precedence if it is specified.
if webSession != nil && webSession.Page != "" {
var path string
switch webSession.Page {
case "signup":
if h.AuthenticationConfig.PublicSignupDisabled {
path = "/login"
} else {
path = "/signup"
}
case "login":
path = "/login"
}
if path != "" {
h.continueFlow(w, r, path)
return
}
}
// Page is something that we do not understand or it is absent.
// In this case, we look at the cookie.
signedUpCookie, err := h.Cookies.GetCookie(r, h.SignedUpCookie.Def)
signedUp := (err == nil && signedUpCookie.Value == "true")
path := "/signup"
if h.AuthenticationConfig.PublicSignupDisabled || signedUp {
path = "/login"
}
h.continueFlow(w, r, path)
}
gotoLogin := func() {
h.continueFlow(w, r, "/login")
}
gotoReauth := func() {
h.continueFlow(w, r, "/reauth")
}
// ctrl.Serve() always write response.
// So we have to put http.Redirect before it.
defer ctrl.Serve()
ctrl.Get(func() error {
// When promote anonymous user, the end-user should not see this page.
if webSession != nil && webSession.LoginHint != "" {
h.continueFlow(w, r, "/flows/promote_user")
return nil
}
// When UserIDHint is present, the end-user should never need to select anything in /select_account,
// so this if block always ends with a return statement, and each branch must write response.
if userIDHint != "" {
if loginPrompt && canUseIntentReauthenticate {
gotoReauth()
} else if !loginPrompt && idpSession != nil && idpSession.GetAuthenticationInfo().UserID == userIDHint {
// Continue without user interaction
// 1. UserIDHint present
// 2. IDP session present and the same as UserIDHint
// 3. prompt!=login
err := continueWithCurrentAccount()
if err != nil {
return err
}
} else {
gotoLogin()
}
return nil
}
// If anything of the following condition holds,
// the end-user does not need to select anything.
// - If x_oauth_provider_alisa is provided via authorization endpoint
// - The request is not from the authorization endpoint, e.g. /
// - There is no session, so nothing to select.
// - prompt=login, in this case, the end-user cannot select existing account.
if oauthProviderAlias != "" {
gotoLogin()
return nil
}
fromAuthzEndpoint := oauthSessionID != ""
if !fromAuthzEndpoint || idpSession == nil || loginPrompt {
gotoSignupOrLogin()
return nil
}
data, err := h.GetData(r, w, idpSession.GetAuthenticationInfo().UserID)
if err != nil {
return err
}
h.Renderer.RenderHTML(w, r, TemplateWebSelectAccountHTML, data)
return nil
})
ctrl.PostAction("continue", func() error {
return continueWithCurrentAccount()
})
ctrl.PostAction("login", func() error {
gotoSignupOrLogin()
return nil
})
}
func (h *AuthflowV2SelectAccountHandler) continueFlow(w http.ResponseWriter, r *http.Request, path string) {
// preserve query only when continuing the login flow
u := webapp.MakeRelativeURL(path, webapp.PreserveQuery(r.URL.Query()))
http.Redirect(w, r, u.String(), http.StatusFound)
}