-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathmail.go
More file actions
924 lines (821 loc) · 33.6 KB
/
mail.go
File metadata and controls
924 lines (821 loc) · 33.6 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
package api
import (
"net/http"
"regexp"
"strings"
"time"
"github.com/supabase/auth/internal/hooks/v0hooks"
mail "github.com/supabase/auth/internal/mailer"
"github.com/supabase/auth/internal/mailer/validateclient"
"github.com/supabase/auth/internal/observability"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/badoux/checkmail"
"github.com/fatih/structs"
"github.com/pkg/errors"
"github.com/sethvargo/go-password/password"
"github.com/supabase/auth/internal/api/apierrors"
"github.com/supabase/auth/internal/api/provider"
"github.com/supabase/auth/internal/crypto"
"github.com/supabase/auth/internal/models"
"github.com/supabase/auth/internal/storage"
"github.com/supabase/auth/internal/utilities"
)
var (
EmailRateLimitExceeded error = errors.New("email rate limit exceeded")
emailSendCounter = observability.ObtainMetricCounter("global_auth_email_send_operations_total", "Number of email send operations")
emailErrorsCounter = observability.ObtainMetricCounter("global_auth_email_send_errors_total", "Number of email send errors")
)
type GenerateLinkParams struct {
Type string `json:"type"`
Email string `json:"email"`
NewEmail string `json:"new_email"`
Password string `json:"password"`
Data map[string]interface{} `json:"data"`
RedirectTo string `json:"redirect_to"`
}
type GenerateLinkResponse struct {
models.User
ActionLink string `json:"action_link"`
EmailOtp string `json:"email_otp"`
HashedToken string `json:"hashed_token"`
VerificationType string `json:"verification_type"`
RedirectTo string `json:"redirect_to"`
}
func (a *API) adminGenerateLink(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
db := a.db.WithContext(ctx)
config := a.config
mailer := a.Mailer()
adminUser := getAdminUser(ctx)
params := &GenerateLinkParams{}
if err := retrieveRequestParams(r, params); err != nil {
return err
}
var err error
params.Email, err = a.validateEmail(params.Email)
if err != nil {
return err
}
referrer := utilities.GetReferrer(r, config)
if utilities.IsRedirectURLValid(config, params.RedirectTo) {
referrer = params.RedirectTo
}
aud := a.requestAud(ctx, r)
user, err := models.FindUserByEmailAndAudience(db, params.Email, aud)
if err != nil {
if models.IsNotFoundError(err) {
switch params.Type {
case mail.MagicLinkVerification:
params.Type = mail.SignupVerification
params.Password, err = password.Generate(64, 10, 1, false, true)
if err != nil {
// password generation must always succeed
panic(err)
}
case mail.RecoveryVerification, mail.EmailChangeCurrentVerification, mail.EmailChangeNewVerification:
return apierrors.NewNotFoundError(apierrors.ErrorCodeUserNotFound, "User with this email not found")
}
} else {
return apierrors.NewInternalServerError("Database error finding user").WithInternalError(err)
}
}
var url string
now := time.Now()
otp := crypto.GenerateOtp(config.Mailer.OtpLength)
hashedToken := crypto.GenerateTokenHash(params.Email, otp)
var (
createdUser bool
signupUser *models.User
inviteUser *models.User
)
switch {
case params.Type == mail.SignupVerification && user == nil:
signupParams := &SignupParams{
Email: params.Email,
Password: params.Password,
Data: params.Data,
Provider: "email",
Aud: aud,
}
if err := a.validateSignupParams(ctx, signupParams); err != nil {
return err
}
signupUser, err = signupParams.ToUserModel(false /* <- isSSOUser */)
if err != nil {
return err
}
if err := a.triggerBeforeUserCreated(r, db, signupUser); err != nil {
return err
}
case params.Type == mail.InviteVerification && user == nil:
signupParams := &SignupParams{
Email: params.Email,
Data: params.Data,
Provider: "email",
Aud: aud,
}
inviteUser, err = signupParams.ToUserModel(false /* <- isSSOUser */)
if err != nil {
return err
}
if err := a.triggerBeforeUserCreated(r, db, inviteUser); err != nil {
return err
}
}
err = db.Transaction(func(tx *storage.Connection) error {
var terr error
switch params.Type {
case mail.MagicLinkVerification, mail.RecoveryVerification:
if terr = models.NewAuditLogEntry(config.AuditLog, r, tx, user, models.UserRecoveryRequestedAction, "", nil); terr != nil {
return terr
}
user.RecoveryToken = hashedToken
user.RecoverySentAt = &now
terr = tx.UpdateOnly(user, "recovery_token", "recovery_sent_at")
if terr != nil {
terr = errors.Wrap(terr, "Database error updating user for recovery")
return terr
}
terr = models.CreateOneTimeToken(tx, user.ID, user.GetEmail(), user.RecoveryToken, models.RecoveryToken)
if terr != nil {
terr = errors.Wrap(terr, "Database error creating recovery token in admin")
return terr
}
case mail.InviteVerification:
if user != nil {
if user.IsConfirmed() {
return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg)
}
} else {
createdUser = true
user, terr = a.signupNewUser(tx, inviteUser)
if terr != nil {
return terr
}
identity, terr := a.createNewIdentity(tx, user, "email", structs.Map(provider.Claims{
Subject: user.ID.String(),
Email: user.GetEmail(),
}))
if terr != nil {
return terr
}
user.Identities = []models.Identity{*identity}
}
if terr = models.NewAuditLogEntry(config.AuditLog, r, tx, adminUser, models.UserInvitedAction, "", map[string]interface{}{
"user_id": user.ID,
"user_email": user.Email,
}); terr != nil {
return terr
}
user.ConfirmationToken = hashedToken
user.ConfirmationSentAt = &now
user.InvitedAt = &now
terr = tx.UpdateOnly(user, "confirmation_token", "confirmation_sent_at", "invited_at")
if terr != nil {
terr = errors.Wrap(terr, "Database error updating user for invite")
return terr
}
terr = models.CreateOneTimeToken(tx, user.ID, user.GetEmail(), user.ConfirmationToken, models.ConfirmationToken)
if terr != nil {
terr = errors.Wrap(terr, "Database error creating confirmation token for invite in admin")
return terr
}
case mail.SignupVerification:
if user != nil {
if user.IsConfirmed() {
return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg)
}
if err := user.UpdateUserMetaData(tx, params.Data); err != nil {
return apierrors.NewInternalServerError("Database error updating user").WithInternalError(err)
}
} else {
// you should never use SignupParams with
// password here to generate a new user, use
// signupUser which is a model generated from
// SignupParams above
createdUser = true
user, terr = a.signupNewUser(tx, signupUser)
if terr != nil {
return terr
}
identity, terr := a.createNewIdentity(tx, user, "email", structs.Map(provider.Claims{
Subject: user.ID.String(),
Email: user.GetEmail(),
}))
if terr != nil {
return terr
}
user.Identities = []models.Identity{*identity}
}
user.ConfirmationToken = hashedToken
user.ConfirmationSentAt = &now
terr = tx.UpdateOnly(user, "confirmation_token", "confirmation_sent_at")
if terr != nil {
terr = errors.Wrap(terr, "Database error updating user for confirmation")
return terr
}
terr = models.CreateOneTimeToken(tx, user.ID, user.GetEmail(), user.ConfirmationToken, models.ConfirmationToken)
if terr != nil {
terr = errors.Wrap(terr, "Database error creating confirmation token for signup in admin")
return terr
}
case mail.EmailChangeCurrentVerification, mail.EmailChangeNewVerification:
if !config.Mailer.SecureEmailChangeEnabled && params.Type == "email_change_current" {
return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Enable secure email change to generate link for current email")
}
params.NewEmail, terr = a.validateEmail(params.NewEmail)
if terr != nil {
return terr
}
if duplicateUser, terr := models.IsDuplicatedEmail(tx, params.NewEmail, user.Aud, user, config.Experimental.ProvidersWithOwnLinkingDomain, config.Security.DangerousSSOAutoLinking); terr != nil {
return apierrors.NewInternalServerError("Database error checking email").WithInternalError(terr)
} else if duplicateUser != nil {
return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg)
}
now := time.Now()
user.EmailChangeSentAt = &now
user.EmailChange = params.NewEmail
user.EmailChangeConfirmStatus = zeroConfirmation
if params.Type == "email_change_current" {
user.EmailChangeTokenCurrent = hashedToken
} else if params.Type == "email_change_new" {
user.EmailChangeTokenNew = crypto.GenerateTokenHash(params.NewEmail, otp)
}
terr = tx.UpdateOnly(user, "email_change_token_current", "email_change_token_new", "email_change", "email_change_sent_at", "email_change_confirm_status")
if terr != nil {
terr = errors.Wrap(terr, "Database error updating user for email change")
return terr
}
if user.EmailChangeTokenCurrent != "" {
terr = models.CreateOneTimeToken(tx, user.ID, user.GetEmail(), user.EmailChangeTokenCurrent, models.EmailChangeTokenCurrent)
if terr != nil {
terr = errors.Wrap(terr, "Database error creating email change token current in admin")
return terr
}
}
if user.EmailChangeTokenNew != "" {
terr = models.CreateOneTimeToken(tx, user.ID, user.EmailChange, user.EmailChangeTokenNew, models.EmailChangeTokenNew)
if terr != nil {
terr = errors.Wrap(terr, "Database error creating email change token new in admin")
return terr
}
}
default:
return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Invalid email action link type requested: %v", params.Type)
}
if terr != nil {
return terr
}
externalURL := getExternalHost(ctx)
url, terr = mailer.GetEmailActionLink(user, params.Type, referrer, externalURL)
if terr != nil {
return terr
}
return nil
})
if err != nil {
return err
}
if createdUser {
if err := a.triggerAfterUserCreated(r, db, user); err != nil {
return err
}
}
resp := GenerateLinkResponse{
User: *user,
ActionLink: url,
EmailOtp: otp,
HashedToken: hashedToken,
VerificationType: params.Type,
RedirectTo: referrer,
}
return sendJSON(w, http.StatusOK, resp)
}
func (a *API) sendConfirmation(r *http.Request, tx *storage.Connection, u *models.User, flowType models.FlowType) error {
var err error
config := a.config
maxFrequency := config.SMTP.MaxFrequency
otpLength := config.Mailer.OtpLength
if err = validateSentWithinFrequencyLimit(u.ConfirmationSentAt, maxFrequency); err != nil {
return err
}
oldToken := u.ConfirmationToken
otp := crypto.GenerateOtp(otpLength)
token := crypto.GenerateTokenHash(u.GetEmail(), otp)
u.ConfirmationToken = addFlowPrefixToToken(token, flowType)
now := time.Now()
if err = a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.SignupVerification,
otp: otp,
tokenHashWithPrefix: u.ConfirmationToken,
}); err != nil {
u.ConfirmationToken = oldToken
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending confirmation email").WithInternalError(err)
}
u.ConfirmationSentAt = &now
if err := tx.UpdateOnly(u, "confirmation_token", "confirmation_sent_at"); err != nil {
return apierrors.NewInternalServerError("Error sending confirmation email").WithInternalError(errors.Wrap(err, "Database error updating user for confirmation"))
}
if err := models.CreateOneTimeToken(tx, u.ID, u.GetEmail(), u.ConfirmationToken, models.ConfirmationToken); err != nil {
return apierrors.NewInternalServerError("Error sending confirmation email").WithInternalError(errors.Wrap(err, "Database error creating confirmation token"))
}
return nil
}
func (a *API) sendInvite(r *http.Request, tx *storage.Connection, u *models.User) error {
config := a.config
otpLength := config.Mailer.OtpLength
var err error
oldToken := u.ConfirmationToken
otp := crypto.GenerateOtp(otpLength)
u.ConfirmationToken = crypto.GenerateTokenHash(u.GetEmail(), otp)
now := time.Now()
err = a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.InviteVerification,
otp: otp,
tokenHashWithPrefix: u.ConfirmationToken,
})
if err != nil {
u.ConfirmationToken = oldToken
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending invite email").WithInternalError(err)
}
u.InvitedAt = &now
u.ConfirmationSentAt = &now
err = tx.UpdateOnly(u, "confirmation_token", "confirmation_sent_at", "invited_at")
if err != nil {
return apierrors.NewInternalServerError("Error inviting user").WithInternalError(errors.Wrap(err, "Database error updating user for invite"))
}
err = models.CreateOneTimeToken(tx, u.ID, u.GetEmail(), u.ConfirmationToken, models.ConfirmationToken)
if err != nil {
return apierrors.NewInternalServerError("Error inviting user").WithInternalError(errors.Wrap(err, "Database error creating confirmation token for invite"))
}
return nil
}
func (a *API) sendPasswordRecovery(r *http.Request, tx *storage.Connection, u *models.User, flowType models.FlowType) error {
config := a.config
otpLength := config.Mailer.OtpLength
if err := validateSentWithinFrequencyLimit(u.RecoverySentAt, config.SMTP.MaxFrequency); err != nil {
return err
}
oldToken := u.RecoveryToken
otp := crypto.GenerateOtp(otpLength)
token := crypto.GenerateTokenHash(u.GetEmail(), otp)
u.RecoveryToken = addFlowPrefixToToken(token, flowType)
now := time.Now()
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.RecoveryVerification,
otp: otp,
tokenHashWithPrefix: u.RecoveryToken,
})
if err != nil {
u.RecoveryToken = oldToken
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending recovery email").WithInternalError(err)
}
u.RecoverySentAt = &now
if err := tx.UpdateOnly(u, "recovery_token", "recovery_sent_at"); err != nil {
return apierrors.NewInternalServerError("Error sending recovery email").WithInternalError(errors.Wrap(err, "Database error updating user for recovery"))
}
if err := models.CreateOneTimeToken(tx, u.ID, u.GetEmail(), u.RecoveryToken, models.RecoveryToken); err != nil {
return apierrors.NewInternalServerError("Error sending recovery email").WithInternalError(errors.Wrap(err, "Database error creating recovery token"))
}
return nil
}
func (a *API) sendReauthenticationOtp(r *http.Request, tx *storage.Connection, u *models.User) error {
config := a.config
maxFrequency := config.SMTP.MaxFrequency
otpLength := config.Mailer.OtpLength
if err := validateSentWithinFrequencyLimit(u.ReauthenticationSentAt, maxFrequency); err != nil {
return err
}
oldToken := u.ReauthenticationToken
otp := crypto.GenerateOtp(otpLength)
u.ReauthenticationToken = crypto.GenerateTokenHash(u.GetEmail(), otp)
now := time.Now()
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.ReauthenticationVerification,
otp: otp,
tokenHashWithPrefix: u.ReauthenticationToken,
})
if err != nil {
u.ReauthenticationToken = oldToken
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending reauthentication email").WithInternalError(err)
}
u.ReauthenticationSentAt = &now
if err := tx.UpdateOnly(u, "reauthentication_token", "reauthentication_sent_at"); err != nil {
return apierrors.NewInternalServerError("Error sending reauthentication email").WithInternalError(errors.Wrap(err, "Database error updating user for reauthentication"))
}
if err := models.CreateOneTimeToken(tx, u.ID, u.GetEmail(), u.ReauthenticationToken, models.ReauthenticationToken); err != nil {
return apierrors.NewInternalServerError("Error sending reauthentication email").WithInternalError(errors.Wrap(err, "Database error creating reauthentication token"))
}
return nil
}
func (a *API) sendMagicLink(r *http.Request, tx *storage.Connection, u *models.User, flowType models.FlowType) error {
var err error
config := a.config
otpLength := config.Mailer.OtpLength
// since Magic Link is just a recovery with a different template and behaviour
// around new users we will reuse the recovery db timer to prevent potential abuse
if err := validateSentWithinFrequencyLimit(u.RecoverySentAt, config.SMTP.MaxFrequency); err != nil {
return err
}
oldToken := u.RecoveryToken
otp := crypto.GenerateOtp(otpLength)
token := crypto.GenerateTokenHash(u.GetEmail(), otp)
u.RecoveryToken = addFlowPrefixToToken(token, flowType)
now := time.Now()
if err = a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.MagicLinkVerification,
otp: otp,
tokenHashWithPrefix: u.RecoveryToken,
}); err != nil {
u.RecoveryToken = oldToken
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending magic link email").WithInternalError(err)
}
u.RecoverySentAt = &now
if err := tx.UpdateOnly(u, "recovery_token", "recovery_sent_at"); err != nil {
return apierrors.NewInternalServerError("Error sending magic link email").WithInternalError(errors.Wrap(err, "Database error updating user for recovery"))
}
if err := models.CreateOneTimeToken(tx, u.ID, u.GetEmail(), u.RecoveryToken, models.RecoveryToken); err != nil {
return apierrors.NewInternalServerError("Error sending magic link email").WithInternalError(errors.Wrap(err, "Database error creating recovery token"))
}
return nil
}
// sendEmailChange sends out an email change token to the new email.
func (a *API) sendEmailChange(r *http.Request, tx *storage.Connection, u *models.User, email string, flowType models.FlowType) error {
config := a.config
otpLength := config.Mailer.OtpLength
if err := validateSentWithinFrequencyLimit(u.EmailChangeSentAt, config.SMTP.MaxFrequency); err != nil {
return err
}
otpNew := crypto.GenerateOtp(otpLength)
u.EmailChange = email
token := crypto.GenerateTokenHash(u.EmailChange, otpNew)
u.EmailChangeTokenNew = addFlowPrefixToToken(token, flowType)
otpCurrent := ""
if config.Mailer.SecureEmailChangeEnabled && u.GetEmail() != "" {
otpCurrent = crypto.GenerateOtp(otpLength)
currentToken := crypto.GenerateTokenHash(u.GetEmail(), otpCurrent)
u.EmailChangeTokenCurrent = addFlowPrefixToToken(currentToken, flowType)
}
u.EmailChangeConfirmStatus = zeroConfirmation
now := time.Now()
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.EmailChangeVerification,
otp: otpCurrent,
otpNew: otpNew,
tokenHashWithPrefix: u.EmailChangeTokenNew,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending email change email").WithInternalError(err)
}
u.EmailChangeSentAt = &now
if err := tx.UpdateOnly(
u,
"email_change_token_current",
"email_change_token_new",
"email_change",
"email_change_sent_at",
"email_change_confirm_status",
); err != nil {
return apierrors.NewInternalServerError("Error sending email change email").WithInternalError(errors.Wrap(err, "Database error updating user for email change"))
}
if u.EmailChangeTokenCurrent != "" {
if err := models.CreateOneTimeToken(tx, u.ID, u.GetEmail(), u.EmailChangeTokenCurrent, models.EmailChangeTokenCurrent); err != nil {
return apierrors.NewInternalServerError("Error sending email change email").WithInternalError(errors.Wrap(err, "Database error creating email change token current"))
}
}
if u.EmailChangeTokenNew != "" {
if err := models.CreateOneTimeToken(tx, u.ID, u.EmailChange, u.EmailChangeTokenNew, models.EmailChangeTokenNew); err != nil {
return apierrors.NewInternalServerError("Error sending email change email").WithInternalError(errors.Wrap(err, "Database error creating email change token new"))
}
}
return nil
}
func (a *API) sendPasswordChangedNotification(r *http.Request, tx *storage.Connection, u *models.User) error {
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.PasswordChangedNotification,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending password changed notification email").WithInternalError(err)
}
return nil
}
func (a *API) sendEmailChangedNotification(r *http.Request, tx *storage.Connection, u *models.User, oldEmail string) error {
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.EmailChangedNotification,
oldEmail: oldEmail,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending email changed notification email").WithInternalError(err)
}
return nil
}
func (a *API) sendPhoneChangedNotification(r *http.Request, tx *storage.Connection, u *models.User, oldPhone string) error {
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.PhoneChangedNotification,
oldPhone: oldPhone,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending phone changed notification email").WithInternalError(err)
}
return nil
}
func (a *API) sendIdentityLinkedNotification(r *http.Request, tx *storage.Connection, u *models.User, provider string) error {
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.IdentityLinkedNotification,
provider: provider,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending identity linked notification email").WithInternalError(err)
}
return nil
}
func (a *API) sendIdentityUnlinkedNotification(r *http.Request, tx *storage.Connection, u *models.User, provider string) error {
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.IdentityUnlinkedNotification,
provider: provider,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending identity unlinked notification email").WithInternalError(err)
}
return nil
}
func (a *API) sendMFAFactorEnrolledNotification(r *http.Request, tx *storage.Connection, u *models.User, factorType string) error {
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.MFAFactorEnrolledNotification,
factorType: factorType,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending MFA factor enrolled notification email").WithInternalError(err)
}
return nil
}
func (a *API) sendMFAFactorUnenrolledNotification(r *http.Request, tx *storage.Connection, u *models.User, factorType string) error {
err := a.sendEmail(r, tx, u, sendEmailParams{
emailActionType: mail.MFAFactorUnenrolledNotification,
factorType: factorType,
})
if err != nil {
if errors.Is(err, EmailRateLimitExceeded) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", EmailRateLimitExceeded.Error())
} else if herr, ok := err.(*HTTPError); ok {
return herr
}
return apierrors.NewInternalServerError("Error sending MFA factor unenrolled notification email").WithInternalError(err)
}
return nil
}
func (a *API) validateEmail(email string) (string, error) {
if email == "" {
return "", apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "An email address is required")
}
if len(email) > 255 {
return "", apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "An email address is too long")
}
if err := checkmail.ValidateFormat(email); err != nil {
return "", apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Unable to validate email address: %s", err.Error())
}
return strings.ToLower(email), nil
}
func validateSentWithinFrequencyLimit(sentAt *time.Time, frequency time.Duration) error {
if sentAt != nil && sentAt.Add(frequency).After(time.Now()) {
return apierrors.NewTooManyRequestsError(apierrors.ErrorCodeOverEmailSendRateLimit, "%s", generateFrequencyLimitErrorMessage(sentAt, frequency))
}
return nil
}
var emailLabelPattern = regexp.MustCompile("[+][^@]+@")
func (a *API) checkEmailAddressAuthorization(email string) bool {
if len(a.config.External.Email.AuthorizedAddresses) > 0 {
// allow labelled emails when authorization rules are in place
normalized := emailLabelPattern.ReplaceAllString(email, "@")
for _, authorizedAddress := range a.config.External.Email.AuthorizedAddresses {
if strings.EqualFold(normalized, authorizedAddress) {
return true
}
}
return false
}
return true
}
type sendEmailParams struct {
emailActionType string
otp string
otpNew string
tokenHashWithPrefix string
oldEmail string
oldPhone string
provider string
factorType string
}
func (a *API) sendEmail(r *http.Request, tx *storage.Connection, u *models.User, params sendEmailParams) error {
ctx := r.Context()
config := a.config
referrerURL := utilities.GetReferrer(r, config)
externalURL := getExternalHost(ctx)
otp := params.otp
if params.emailActionType != mail.EmailChangeVerification {
if u.GetEmail() != "" && !a.checkEmailAddressAuthorization(u.GetEmail()) {
return apierrors.NewBadRequestError(apierrors.ErrorCodeEmailAddressNotAuthorized, "Email address %q cannot be used as it is not authorized", u.GetEmail())
}
} else {
// first check that the user can update their address to the
// new one in u.EmailChange
if u.EmailChange != "" && !a.checkEmailAddressAuthorization(u.EmailChange) {
return apierrors.NewBadRequestError(apierrors.ErrorCodeEmailAddressNotAuthorized, "Email address %q cannot be used as it is not authorized", u.EmailChange)
}
// if secure email change is enabled, check that the user
// account (which could have been created before the authorized
// address authorization restriction was enabled) can even
// receive the confirmation message to the existing address
if config.Mailer.SecureEmailChangeEnabled && u.GetEmail() != "" && !a.checkEmailAddressAuthorization(u.GetEmail()) {
return apierrors.NewBadRequestError(apierrors.ErrorCodeEmailAddressNotAuthorized, "Email address %q cannot be used as it is not authorized", u.GetEmail())
}
}
// if the number of events is set to zero, we immediately apply rate limits.
if config.RateLimitEmailSent.Events == 0 {
emailRateLimitCounter.Add(
ctx,
1,
metric.WithAttributeSet(attribute.NewSet(attribute.String("path", r.URL.Path))),
)
return EmailRateLimitExceeded
}
// TODO(km): Deprecate this behaviour - rate limits should still be applied to autoconfirm
if !config.Mailer.Autoconfirm {
// apply rate limiting before the email is sent out
if ok := a.limiterOpts.Email.Allow(); !ok {
emailRateLimitCounter.Add(
ctx,
1,
metric.WithAttributeSet(attribute.NewSet(attribute.String("path", r.URL.Path))),
)
return EmailRateLimitExceeded
}
}
if config.Hook.SendEmail.Enabled {
// When secure email change is disabled, we place the token for the new email on emailData.Token
if params.emailActionType == mail.EmailChangeVerification && !config.Mailer.SecureEmailChangeEnabled && u.GetEmail() != "" {
// BUG(cstockton): This introduced a bug which mismatched the token
// and hash fields, such that:
//
// EmailData.TokenHashNew = Hash(CurEmail, EmailData.Token)
// EmailData.TokenHash = Hash(NewEmail, EmailData.TokenNew)
//
// Specifically with email changes we should look to fix this
// behavior in a BC way to maintain that:
//
// Token Always contains the Token for user.email
// TokenHash Always contains the Hash for user.email
//
// Token Always contains the Token for user.email_new
// TokenHash Always contains the Hash for user.email_new
//
otp = params.otpNew
}
emailData := mail.EmailData{
Token: otp,
EmailActionType: params.emailActionType,
RedirectTo: referrerURL,
SiteURL: externalURL.String(),
TokenHash: params.tokenHashWithPrefix,
}
if params.emailActionType == mail.EmailChangeVerification {
if config.Mailer.SecureEmailChangeEnabled && u.GetEmail() != "" {
emailData.TokenNew = params.otpNew
emailData.TokenHashNew = u.EmailChangeTokenCurrent
} else if emailData.Token == "" && u.EmailChange != "" {
// BUG(cstockton): This matches the current behavior but is not
// intuitive and should be changed in a future release. See the
// comment above for more details.
emailData.Token = params.otpNew
}
}
// Augment the email data for the email send hook with notification-specific fields
switch params.emailActionType {
case mail.EmailChangedNotification:
emailData.OldEmail = params.oldEmail
case mail.PhoneChangedNotification:
emailData.OldPhone = params.oldPhone
case mail.IdentityLinkedNotification, mail.IdentityUnlinkedNotification:
emailData.Provider = params.provider
case mail.MFAFactorEnrolledNotification, mail.MFAFactorUnenrolledNotification:
emailData.FactorType = params.factorType
}
input := v0hooks.SendEmailInput{
User: u,
EmailData: emailData,
}
output := v0hooks.SendEmailOutput{}
return a.hooksMgr.InvokeHook(tx, r, &input, &output)
}
// Increment email send operations here, since this metric is meant to count number of mail
// send operations rather than simply number of attempts to send mail
emailSendCounter.Add(ctx, 1, metric.WithAttributes(attribute.String("type", params.emailActionType)))
mr := a.Mailer()
var err error
switch params.emailActionType {
case mail.SignupVerification:
err = mr.ConfirmationMail(r, u, otp, referrerURL, externalURL)
case mail.MagicLinkVerification:
err = mr.MagicLinkMail(r, u, otp, referrerURL, externalURL)
case mail.ReauthenticationVerification:
err = mr.ReauthenticateMail(r, u, otp)
case mail.RecoveryVerification:
err = mr.RecoveryMail(r, u, otp, referrerURL, externalURL)
case mail.InviteVerification:
err = mr.InviteMail(r, u, otp, referrerURL, externalURL)
case mail.EmailChangeVerification:
err = mr.EmailChangeMail(r, u, params.otpNew, otp, referrerURL, externalURL)
case mail.PasswordChangedNotification:
err = mr.PasswordChangedNotificationMail(r, u)
case mail.EmailChangedNotification:
err = mr.EmailChangedNotificationMail(r, u, params.oldEmail)
case mail.PhoneChangedNotification:
err = mr.PhoneChangedNotificationMail(r, u, params.oldPhone)
case mail.IdentityLinkedNotification:
err = mr.IdentityLinkedNotificationMail(r, u, params.provider)
case mail.IdentityUnlinkedNotification:
err = mr.IdentityUnlinkedNotificationMail(r, u, params.provider)
case mail.MFAFactorEnrolledNotification:
err = mr.MFAFactorEnrolledNotificationMail(r, u, params.factorType)
case mail.MFAFactorUnenrolledNotification:
err = mr.MFAFactorUnenrolledNotificationMail(r, u, params.factorType)
default:
err = errors.New("invalid email action type")
}
switch {
case errors.Is(err, validateclient.ErrInvalidEmailAddress),
errors.Is(err, validateclient.ErrInvalidEmailFormat),
errors.Is(err, validateclient.ErrInvalidEmailDNS):
emailErrorsCounter.Add(ctx, 1, metric.WithAttributes(attribute.String("type", params.emailActionType)))
return apierrors.NewBadRequestError(
apierrors.ErrorCodeEmailAddressInvalid,
"Email address %q is invalid",
u.GetEmail())
case err != nil:
emailErrorsCounter.Add(ctx, 1, metric.WithAttributes(attribute.String("type", params.emailActionType)))
return err
default:
return err
}
}