/
handler_register_totp.go
350 lines (249 loc) · 11.8 KB
/
handler_register_totp.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
package handlers
import (
"encoding/json"
"fmt"
"time"
"github.com/valyala/fasthttp"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/model"
"github.com/authelia/authelia/v4/internal/regulation"
"github.com/authelia/authelia/v4/internal/session"
"github.com/authelia/authelia/v4/internal/utils"
)
// TOTPRegisterGET returns the registration specific options.
func TOTPRegisterGET(ctx *middlewares.AutheliaCtx) {
var (
userSession session.UserSession
err error
)
if userSession, err = ctx.GetSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred retrieving TOTP registration options: %s", errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToOptionsOneTimePassword)
return
}
if userSession.IsAnonymous() {
ctx.Logger.WithError(errUserAnonymous).Error("Error occurred retrieving TOTP registration options")
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToOptionsOneTimePassword)
return
}
if err = ctx.SetJSONBody(ctx.Providers.TOTP.Options()); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred retrieving TOTP registration options for user '%s': %s", userSession.Username, errStrRespBody)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToOptionsOneTimePassword)
}
}
// TOTPRegisterPUT handles the users choice of registration specific options and returns the generated configuration.
func TOTPRegisterPUT(ctx *middlewares.AutheliaCtx) {
var (
userSession session.UserSession
bodyJSON bodyRegisterTOTP
err error
)
if userSession, err = ctx.GetSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a TOTP registration session: %s", errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if userSession.IsAnonymous() {
ctx.Logger.WithError(errUserAnonymous).Error("Error occurred generating a TOTP registration session")
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if err = json.Unmarshal(ctx.PostBody(), &bodyJSON); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a TOTP registration session for user '%s': %s", userSession.Username, errStrReqBodyParse)
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
opts := ctx.Providers.TOTP.Options()
if !utils.IsStringInSlice(bodyJSON.Algorithm, opts.Algorithms) ||
!utils.IsIntegerInSlice(bodyJSON.Period, opts.Periods) ||
!utils.IsIntegerInSlice(bodyJSON.Length, opts.Lengths) {
ctx.Logger.WithError(fmt.Errorf("the algorithm '%s', period '%d', or length '%d' was not permitted by configured policy", bodyJSON.Algorithm, bodyJSON.Period, bodyJSON.Length)).Errorf("Error occurred generating a TOTP registration session for user '%s': error occurred validating registration options selection", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
var config *model.TOTPConfiguration
if config, err = ctx.Providers.TOTP.GenerateCustom(ctx, userSession.Username, bodyJSON.Algorithm, "", uint(bodyJSON.Length), uint(bodyJSON.Period), 0); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a TOTP registration session for user '%s': error generating TOTP configuration", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
userSession.TOTP = &session.TOTP{
Issuer: config.Issuer,
Algorithm: config.Algorithm,
Digits: config.Digits,
Period: config.Period,
Secret: string(config.Secret),
Expires: ctx.Clock.Now().Add(time.Minute * 10),
}
if err = ctx.SaveSession(userSession); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a TOTP registration session for user '%s': %s", userSession.Username, errStrUserSessionDataSave)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
response := TOTPKeyResponse{
OTPAuthURL: config.URI(),
Base32Secret: userSession.TOTP.Secret,
}
if err = ctx.SetJSONBody(response); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a TOTP registration session for user '%s': %s", userSession.Username, errStrRespBody)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
}
}
// TOTPRegisterPOST handles validation that the user has properly registered the configuration.
func TOTPRegisterPOST(ctx *middlewares.AutheliaCtx) {
var (
userSession session.UserSession
bodyJSON bodyRegisterFinishTOTP
valid bool
step uint64
err error
)
if userSession, err = ctx.GetSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a TOTP registration session: %s", errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if userSession.IsAnonymous() {
ctx.Logger.WithError(errUserAnonymous).Error("Error occurred validating a TOTP registration session")
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if err = json.Unmarshal(ctx.PostBody(), &bodyJSON); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a TOTP registration session for user '%s': %s", userSession.Username, errStrReqBodyParse)
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if userSession.TOTP == nil {
ctx.Logger.Errorf("Error occurred validating a TOTP registration session for user '%s': the user did not initiate a registration session on their current session", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if ctx.Clock.Now().After(userSession.TOTP.Expires) {
ctx.Logger.WithError(fmt.Errorf("the registration session is expired")).Errorf("Error occurred validating a TOTP registration session for user '%s': error occurred validating the session", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
config := model.TOTPConfiguration{
CreatedAt: ctx.Clock.Now(),
Username: userSession.Username,
Issuer: userSession.TOTP.Issuer,
Algorithm: userSession.TOTP.Algorithm,
Period: userSession.TOTP.Period,
Digits: userSession.TOTP.Digits,
Secret: []byte(userSession.TOTP.Secret),
}
if valid, step, err = ctx.Providers.TOTP.Validate(ctx, bodyJSON.Token, &config); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a TOTP registration session for user '%s': error occurred validating the user input against the session", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if !valid {
ctx.Logger.WithError(fmt.Errorf("user input did not match any expected value")).Errorf("Error occurred validating a TOTP registration session for user '%s'", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
if !ctx.Configuration.TOTP.DisableReuseSecurityPolicy {
if err = ctx.Providers.StorageProvider.SaveTOTPHistory(ctx, userSession.Username, step*uint64(config.Period)); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a TOTP registration session for user '%s': error occurred saving the TOTP history to the storage backend", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
}
if err = ctx.Providers.StorageProvider.SaveTOTPConfiguration(ctx, config); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a TOTP registration session for user '%s': error occurred saving the TOTP configuration to the storage backend", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
userSession.TOTP = nil
if err = ctx.SaveSession(userSession); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a TOTP registration session for user '%s': %s", userSession.Username, errStrUserSessionDataSave)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterOneTimePassword)
return
}
ctxLogEvent(ctx, userSession.Username, eventLogAction2FAAdded, map[string]any{eventLogKeyAction: eventLogAction2FAAdded, eventLogKeyCategory: eventLogCategoryOneTimePassword})
ctx.ReplyOK()
}
// TOTPRegisterDELETE removes a pending TOTP registration.
func TOTPRegisterDELETE(ctx *middlewares.AutheliaCtx) {
var (
userSession session.UserSession
err error
)
if userSession, err = ctx.GetSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred deleting a TOTP registration session: %s", errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToDeleteRegisterOneTimePassword)
return
}
if userSession.IsAnonymous() {
ctx.Logger.WithError(errUserAnonymous).Error("Error occurred deleting a TOTP registration session")
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToDeleteRegisterOneTimePassword)
return
}
if userSession.TOTP == nil {
ctx.ReplyOK()
return
}
userSession.TOTP = nil
if err = ctx.SaveSession(userSession); err != nil {
ctx.Logger.WithError(err).Errorf(logFmtErrSessionSave, "deleted pending TOTP configuration", regulation.AuthTypeTOTP, logFmtActionRegistration, userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToDeleteRegisterOneTimePassword)
return
}
ctx.ReplyOK()
}
// TOTPConfigurationDELETE removes a registered TOTP configuration.
func TOTPConfigurationDELETE(ctx *middlewares.AutheliaCtx) {
var (
userSession session.UserSession
err error
)
if userSession, err = ctx.GetSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred deleting a TOTP configuration: %s", errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToDeleteOneTimePassword)
return
}
if userSession.IsAnonymous() {
ctx.Logger.WithError(errUserAnonymous).Error("Error occurred deleting a TOTP configuration")
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToDeleteOneTimePassword)
return
}
if _, err = ctx.Providers.StorageProvider.LoadTOTPConfiguration(ctx, userSession.Username); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred deleting a TOTP configuration for user '%s': error occurred loading configuration from the storage backend", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToDeleteOneTimePassword)
return
}
if err = ctx.Providers.StorageProvider.DeleteTOTPConfiguration(ctx, userSession.Username); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred deleting a TOTP configuration for user '%s': error occurred deleting configuration from the storage backend", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToDeleteOneTimePassword)
return
}
ctxLogEvent(ctx, userSession.Username, eventLogAction2FARemoved, map[string]any{eventLogKeyAction: eventLogAction2FARemoved, eventLogKeyCategory: eventLogCategoryOneTimePassword})
ctx.ReplyOK()
}