@@ -87,11 +87,7 @@ func (a *API) adminGenerateLink(w http.ResponseWriter, r *http.Request) error {
87
87
88
88
var url string
89
89
now := time .Now ()
90
- otp , err := crypto .GenerateOtp (config .Mailer .OtpLength )
91
- if err != nil {
92
- // OTP generation must always succeed
93
- panic (err )
94
- }
90
+ otp := crypto .GenerateOtp (config .Mailer .OtpLength )
95
91
96
92
hashedToken := crypto .GenerateTokenHash (params .Email , otp )
97
93
@@ -300,19 +296,18 @@ func (a *API) adminGenerateLink(w http.ResponseWriter, r *http.Request) error {
300
296
}
301
297
302
298
func (a * API ) sendConfirmation (r * http.Request , tx * storage.Connection , u * models.User , flowType models.FlowType ) error {
299
+ var err error
300
+
303
301
config := a .config
304
302
maxFrequency := config .SMTP .MaxFrequency
305
303
otpLength := config .Mailer .OtpLength
306
304
307
- if err : = validateSentWithinFrequencyLimit (u .ConfirmationSentAt , maxFrequency ); err != nil {
305
+ if err = validateSentWithinFrequencyLimit (u .ConfirmationSentAt , maxFrequency ); err != nil {
308
306
return err
309
307
}
310
308
oldToken := u .ConfirmationToken
311
- otp , err := crypto .GenerateOtp (otpLength )
312
- if err != nil {
313
- // OTP generation must succeeed
314
- panic (err )
315
- }
309
+ otp := crypto .GenerateOtp (otpLength )
310
+
316
311
token := crypto .GenerateTokenHash (u .GetEmail (), otp )
317
312
u .ConfirmationToken = addFlowPrefixToToken (token , flowType )
318
313
now := time .Now ()
@@ -342,11 +337,8 @@ func (a *API) sendInvite(r *http.Request, tx *storage.Connection, u *models.User
342
337
otpLength := config .Mailer .OtpLength
343
338
var err error
344
339
oldToken := u .ConfirmationToken
345
- otp , err := crypto .GenerateOtp (otpLength )
346
- if err != nil {
347
- // OTP generation must succeed
348
- panic (err )
349
- }
340
+ otp := crypto .GenerateOtp (otpLength )
341
+
350
342
u .ConfirmationToken = crypto .GenerateTokenHash (u .GetEmail (), otp )
351
343
now := time .Now ()
352
344
if err = a .sendEmail (r , tx , u , mail .InviteVerification , otp , "" , u .ConfirmationToken ); err != nil {
@@ -382,15 +374,12 @@ func (a *API) sendPasswordRecovery(r *http.Request, tx *storage.Connection, u *m
382
374
}
383
375
384
376
oldToken := u .RecoveryToken
385
- otp , err := crypto .GenerateOtp (otpLength )
386
- if err != nil {
387
- // OTP generation must succeed
388
- panic (err )
389
- }
377
+ otp := crypto .GenerateOtp (otpLength )
378
+
390
379
token := crypto .GenerateTokenHash (u .GetEmail (), otp )
391
380
u .RecoveryToken = addFlowPrefixToToken (token , flowType )
392
381
now := time .Now ()
393
- if err = a .sendEmail (r , tx , u , mail .RecoveryVerification , otp , "" , u .RecoveryToken ); err != nil {
382
+ if err : = a .sendEmail (r , tx , u , mail .RecoveryVerification , otp , "" , u .RecoveryToken ); err != nil {
394
383
u .RecoveryToken = oldToken
395
384
if errors .Is (err , EmailRateLimitExceeded ) {
396
385
return tooManyRequestsError (ErrorCodeOverEmailSendRateLimit , EmailRateLimitExceeded .Error ())
@@ -422,11 +411,8 @@ func (a *API) sendReauthenticationOtp(r *http.Request, tx *storage.Connection, u
422
411
}
423
412
424
413
oldToken := u .ReauthenticationToken
425
- otp , err := crypto .GenerateOtp (otpLength )
426
- if err != nil {
427
- // OTP generation must succeed
428
- panic (err )
429
- }
414
+ otp := crypto .GenerateOtp (otpLength )
415
+
430
416
u .ReauthenticationToken = crypto .GenerateTokenHash (u .GetEmail (), otp )
431
417
now := time .Now ()
432
418
@@ -452,6 +438,7 @@ func (a *API) sendReauthenticationOtp(r *http.Request, tx *storage.Connection, u
452
438
}
453
439
454
440
func (a * API ) sendMagicLink (r * http.Request , tx * storage.Connection , u * models.User , flowType models.FlowType ) error {
441
+ var err error
455
442
config := a .config
456
443
otpLength := config .Mailer .OtpLength
457
444
@@ -462,11 +449,8 @@ func (a *API) sendMagicLink(r *http.Request, tx *storage.Connection, u *models.U
462
449
}
463
450
464
451
oldToken := u .RecoveryToken
465
- otp , err := crypto .GenerateOtp (otpLength )
466
- if err != nil {
467
- // OTP generation must succeed
468
- panic (err )
469
- }
452
+ otp := crypto .GenerateOtp (otpLength )
453
+
470
454
token := crypto .GenerateTokenHash (u .GetEmail (), otp )
471
455
u .RecoveryToken = addFlowPrefixToToken (token , flowType )
472
456
@@ -501,22 +485,16 @@ func (a *API) sendEmailChange(r *http.Request, tx *storage.Connection, u *models
501
485
return err
502
486
}
503
487
504
- otpNew , err := crypto .GenerateOtp (otpLength )
505
- if err != nil {
506
- // OTP generation must succeed
507
- panic (err )
508
- }
488
+ otpNew := crypto .GenerateOtp (otpLength )
489
+
509
490
u .EmailChange = email
510
491
token := crypto .GenerateTokenHash (u .EmailChange , otpNew )
511
492
u .EmailChangeTokenNew = addFlowPrefixToToken (token , flowType )
512
493
513
494
otpCurrent := ""
514
495
if config .Mailer .SecureEmailChangeEnabled && u .GetEmail () != "" {
515
- otpCurrent , err = crypto .GenerateOtp (otpLength )
516
- if err != nil {
517
- // OTP generation must succeed
518
- panic (err )
519
- }
496
+ otpCurrent = crypto .GenerateOtp (otpLength )
497
+
520
498
currentToken := crypto .GenerateTokenHash (u .GetEmail (), otpCurrent )
521
499
u .EmailChangeTokenCurrent = addFlowPrefixToToken (currentToken , flowType )
522
500
}
0 commit comments