/
authflow.go
230 lines (205 loc) · 6.72 KB
/
authflow.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
package viewmodels
import (
"encoding/json"
"fmt"
"net/http"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/auth/webapp"
authflow "github.com/authgear/authgear-server/pkg/lib/authenticationflow"
"github.com/authgear/authgear-server/pkg/lib/config"
)
type AuthflowViewModel struct {
IdentityCandidates []map[string]interface{}
LoginIDDisabled bool
PhoneLoginIDEnabled bool
EmailLoginIDEnabled bool
UsernameLoginIDEnabled bool
PasskeyEnabled bool
// NonPhoneLoginIDInputType is the "type" attribute for the non-phone <input>.
// It is "email" or "text".
NonPhoneLoginIDInputType string
// NonPhoneLoginIDType is the type of non-phone login ID.
// It is "email", "username" or "email_or_username".
NonPhoneLoginIDType string
// LoginIDKey is the key the end-user has chosen.
// It is "email", "phone", or "username".
LoginIDKey string
// LoginIDInputType is the input the end-user has chosen.
// It is "email", "phone" or "text".
LoginIDInputType string
// LoginIDContextualType is the type the end-user thinks they should enter.
// It depends on LoginIDInputType.
// It is "email", "phone", "username", or "email_or_username".
LoginIDContextualType string
PasskeyRequestOptionsJSON string
}
type AuthflowViewModeler struct {
Authentication *config.AuthenticationConfig
LoginID *config.LoginIDConfig
}
// nolint: gocognit
func (m *AuthflowViewModeler) NewWithAuthflow(f *authflow.FlowResponse, r *http.Request) AuthflowViewModel {
options := webapp.GetIdentificationOptions(f)
var firstLoginIDIdentification config.AuthenticationFlowIdentification
var firstNonPhoneLoginIDIdentification config.AuthenticationFlowIdentification
hasEmail := false
hasUsername := false
hasPhone := false
passkeyEnabled := false
passkeyRequestOptionsJSON := ""
for _, o := range options {
switch o.Identification {
case config.AuthenticationFlowIdentificationEmail:
if firstLoginIDIdentification == "" {
firstLoginIDIdentification = config.AuthenticationFlowIdentificationEmail
}
if firstNonPhoneLoginIDIdentification == "" {
firstNonPhoneLoginIDIdentification = config.AuthenticationFlowIdentificationEmail
}
hasEmail = true
case config.AuthenticationFlowIdentificationPhone:
if firstLoginIDIdentification == "" {
firstLoginIDIdentification = config.AuthenticationFlowIdentificationPhone
}
hasPhone = true
case config.AuthenticationFlowIdentificationUsername:
if firstLoginIDIdentification == "" {
firstLoginIDIdentification = config.AuthenticationFlowIdentificationUsername
}
if firstNonPhoneLoginIDIdentification == "" {
firstNonPhoneLoginIDIdentification = config.AuthenticationFlowIdentificationUsername
}
hasUsername = true
case config.AuthenticationFlowIdentificationPasskey:
passkeyEnabled = true
bytes, err := json.Marshal(o.RequestOptions)
if err != nil {
panic(err)
}
passkeyRequestOptionsJSON = string(bytes)
}
}
// Then we determine NonPhoneLoginIDInputType.
nonPhoneLoginIDInputType := "text"
if hasEmail && !hasUsername {
nonPhoneLoginIDInputType = "email"
}
nonPhoneLoginIDType := "email"
switch {
case hasEmail && hasUsername:
nonPhoneLoginIDType = "email_or_username"
case hasUsername:
nonPhoneLoginIDType = "username"
}
loginIDInputType := r.FormValue("q_login_id_input_type")
switch {
case loginIDInputType == "phone" && hasPhone:
// valid
break
case loginIDInputType == nonPhoneLoginIDInputType:
// valid
break
default:
if firstLoginIDIdentification != "" {
if firstLoginIDIdentification == config.AuthenticationFlowIdentificationPhone {
loginIDInputType = "phone"
} else {
loginIDInputType = nonPhoneLoginIDInputType
}
} else {
// Otherwise set a default value.
loginIDInputType = "text"
}
}
loginIDKey := r.FormValue("q_login_id_key")
switch {
case loginIDKey == "email" && hasEmail:
// valid
break
case loginIDKey == "phone" && hasPhone:
// valid
break
case loginIDKey == "username" && hasUsername:
// valid
break
default:
// Otherwise set q_login_id_key to match q_login_id_input_type.
switch loginIDInputType {
case "phone":
loginIDKey = "phone"
case "email":
loginIDKey = "email"
default:
if firstNonPhoneLoginIDIdentification != "" {
loginIDKey = string(firstNonPhoneLoginIDIdentification)
} else {
// Otherwise set a default value.
loginIDKey = "email"
}
}
}
var loginIDContextualType string
switch {
case loginIDInputType == "phone":
loginIDContextualType = "phone"
default:
loginIDContextualType = nonPhoneLoginIDType
}
loginIDDisabled := !hasEmail && !hasUsername && !hasPhone
makeLoginIDCandidate := func(t model.LoginIDKeyType) map[string]interface{} {
var inputType string
switch t {
case model.LoginIDKeyTypePhone:
inputType = "phone"
case model.LoginIDKeyTypeEmail:
fallthrough
case model.LoginIDKeyTypeUsername:
inputType = nonPhoneLoginIDInputType
default:
panic(fmt.Errorf("unexpected login id key: %v", t))
}
candidate := map[string]interface{}{
"type": string(model.IdentityTypeLoginID),
"login_id_type": string(t),
"login_id_key": string(t),
"login_id_input_type": inputType,
}
return candidate
}
var candidates []map[string]interface{}
for _, o := range options {
switch o.Identification {
case config.AuthenticationFlowIdentificationEmail:
candidates = append(candidates, makeLoginIDCandidate(model.LoginIDKeyTypeEmail))
case config.AuthenticationFlowIdentificationPhone:
candidates = append(candidates, makeLoginIDCandidate(model.LoginIDKeyTypePhone))
case config.AuthenticationFlowIdentificationUsername:
candidates = append(candidates, makeLoginIDCandidate(model.LoginIDKeyTypeUsername))
case config.AuthenticationFlowIdentificationOAuth:
candidate := map[string]interface{}{
"type": string(model.IdentityTypeOAuth),
"provider_type": string(o.ProviderType),
"provider_alias": o.Alias,
"provider_app_type": string(o.WechatAppType),
}
candidates = append(candidates, candidate)
case config.AuthenticationFlowIdentificationPasskey:
// Passkey was not handled by candidates.
break
}
}
return AuthflowViewModel{
IdentityCandidates: candidates,
LoginIDDisabled: loginIDDisabled,
PhoneLoginIDEnabled: hasPhone,
EmailLoginIDEnabled: hasEmail,
UsernameLoginIDEnabled: hasUsername,
PasskeyEnabled: passkeyEnabled,
PasskeyRequestOptionsJSON: passkeyRequestOptionsJSON,
LoginIDKey: loginIDKey,
LoginIDInputType: loginIDInputType,
NonPhoneLoginIDInputType: nonPhoneLoginIDInputType,
NonPhoneLoginIDType: nonPhoneLoginIDType,
LoginIDContextualType: loginIDContextualType,
}
}