Skip to content

Commit

Permalink
fix: update signature to contain context and request
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Mar 26, 2024
1 parent abb86f6 commit d01afaa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
14 changes: 7 additions & 7 deletions internal/api/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (a *API) sendConfirmation(ctx context.Context, r *http.Request, tx *storage
token := crypto.GenerateTokenHash(u.GetEmail(), otp)
u.ConfirmationToken = addFlowPrefixToToken(token, flowType)
now := time.Now()
if err := mailer.ConfirmationMail(u, otp, referrerURL, externalURL); err != nil {
if err := mailer.ConfirmationMail(ctx, r, u, otp, referrerURL, externalURL); err != nil {
u.ConfirmationToken = oldToken
return errors.Wrap(err, "Error sending confirmation email")
}
Expand Down Expand Up @@ -309,7 +309,7 @@ func (a *API) sendInvite(ctx context.Context, r *http.Request, tx *storage.Conne
}
u.ConfirmationToken = crypto.GenerateTokenHash(u.GetEmail(), otp)
now := time.Now()
if err := mailer.InviteMail(u, otp, referrerURL, externalURL); err != nil {
if err := mailer.InviteMail(ctx, r, u, otp, referrerURL, externalURL); err != nil {
u.ConfirmationToken = oldToken
return errors.Wrap(err, "Error sending invite email")
}
Expand Down Expand Up @@ -344,7 +344,7 @@ func (a *API) sendPasswordRecovery(ctx context.Context, r *http.Request, tx *sto
token := crypto.GenerateTokenHash(u.GetEmail(), otp)
u.RecoveryToken = addFlowPrefixToToken(token, flowType)
now := time.Now()
if err := mailer.RecoveryMail(u, otp, referrerURL, externalURL); err != nil {
if err := mailer.RecoveryMail(ctx, r, u, otp, referrerURL, externalURL); err != nil {
u.RecoveryToken = oldToken
return errors.Wrap(err, "Error sending recovery email")
}
Expand All @@ -357,7 +357,7 @@ func (a *API) sendPasswordRecovery(ctx context.Context, r *http.Request, tx *sto
return nil
}

func (a *API) sendReauthenticationOtp(tx *storage.Connection, u *models.User) error {
func (a *API) sendReauthenticationOtp(ctx context.Context, r *http.Request, tx *storage.Connection, u *models.User) error {
config := a.config
maxFrequency := config.SMTP.MaxFrequency
otpLength := config.Mailer.OtpLength
Expand All @@ -376,7 +376,7 @@ func (a *API) sendReauthenticationOtp(tx *storage.Connection, u *models.User) er
}
u.ReauthenticationToken = crypto.GenerateTokenHash(u.GetEmail(), otp)
now := time.Now()
if err := mailer.ReauthenticateMail(u, otp); err != nil {
if err := mailer.ReauthenticateMail(ctx, r, u, otp); err != nil {
u.ReauthenticationToken = oldToken
return errors.Wrap(err, "Error sending reauthentication email")
}
Expand Down Expand Up @@ -413,7 +413,7 @@ func (a *API) sendMagicLink(ctx context.Context, r *http.Request, tx *storage.Co
u.RecoveryToken = addFlowPrefixToToken(token, flowType)

now := time.Now()
if err := mailer.MagicLinkMail(u, otp, referrerURL, externalURL); err != nil {
if err := mailer.MagicLinkMail(ctx, r, u, otp, referrerURL, externalURL); err != nil {
u.RecoveryToken = oldToken
return errors.Wrap(err, "Error sending magic link email")
}
Expand Down Expand Up @@ -460,7 +460,7 @@ func (a *API) sendEmailChange(ctx context.Context, r *http.Request, tx *storage.

u.EmailChangeConfirmStatus = zeroConfirmation
now := time.Now()
if err := mailer.EmailChangeMail(u, otpNew, otpCurrent, referrerURL, externalURL); err != nil {
if err := mailer.EmailChangeMail(ctx, r, u, otpNew, otpCurrent, referrerURL, externalURL); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/reauthenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (a *API) Reauthenticate(w http.ResponseWriter, r *http.Request) error {
return terr
}
if email != "" {
return a.sendReauthenticationOtp(tx, user)
return a.sendReauthenticationOtp(ctx, r, tx, user)
} else if phone != "" {
smsProvider, terr := sms_provider.GetSmsProvider(*config)
if terr != nil {
Expand Down
14 changes: 8 additions & 6 deletions internal/mailer/mailer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mailer

import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/gofrs/uuid"
Expand All @@ -15,12 +17,12 @@ import (
// Mailer defines the interface a mailer must implement.
type Mailer interface {
Send(user *models.User, subject, body string, data map[string]interface{}) error
InviteMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error
ConfirmationMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error
RecoveryMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error
MagicLinkMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error
EmailChangeMail(user *models.User, otpNew, otpCurrent, referrerURL string, externalURL *url.URL) error
ReauthenticateMail(user *models.User, otp string) error
InviteMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error
ConfirmationMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error
RecoveryMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error
MagicLinkMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error
EmailChangeMail(ctx context.Context, r *http.Request, user *models.User, otpNew, otpCurrent, referrerURL string, externalURL *url.URL) error
ReauthenticateMail(ctx context.Context, r *http.Request, user *models.User, otp string) error
ValidateEmail(email string) error
GetEmailActionLink(user *models.User, actionType, referrerURL string, externalURL *url.URL) (string, error)
}
Expand Down
14 changes: 8 additions & 6 deletions internal/mailer/template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mailer

import (
"context"
"fmt"
"net/http"
"net/url"
"strings"

Expand Down Expand Up @@ -75,7 +77,7 @@ func (m TemplateMailer) ValidateEmail(email string) error {
}

// InviteMail sends a invite mail to a new user
func (m *TemplateMailer) InviteMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
func (m *TemplateMailer) InviteMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Invite, &EmailParams{
Token: user.ConfirmationToken,
Type: "invite",
Expand Down Expand Up @@ -106,7 +108,7 @@ func (m *TemplateMailer) InviteMail(user *models.User, otp, referrerURL string,
}

// ConfirmationMail sends a signup confirmation mail to a new user
func (m *TemplateMailer) ConfirmationMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
func (m *TemplateMailer) ConfirmationMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Confirmation, &EmailParams{
Token: user.ConfirmationToken,
Type: "signup",
Expand Down Expand Up @@ -136,7 +138,7 @@ func (m *TemplateMailer) ConfirmationMail(user *models.User, otp, referrerURL st
}

// ReauthenticateMail sends a reauthentication mail to an authenticated user
func (m *TemplateMailer) ReauthenticateMail(user *models.User, otp string) error {
func (m *TemplateMailer) ReauthenticateMail(ctx context.Context, r *http.Request, user *models.User, otp string) error {
data := map[string]interface{}{
"SiteURL": m.Config.SiteURL,
"Email": user.Email,
Expand All @@ -154,7 +156,7 @@ func (m *TemplateMailer) ReauthenticateMail(user *models.User, otp string) error
}

// EmailChangeMail sends an email change confirmation mail to a user
func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent, referrerURL string, externalURL *url.URL) error {
func (m *TemplateMailer) EmailChangeMail(ctx context.Context, r *http.Request, user *models.User, otpNew, otpCurrent, referrerURL string, externalURL *url.URL) error {
type Email struct {
Address string
Otp string
Expand Down Expand Up @@ -229,7 +231,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, otpNew, otpCurrent,
}

// RecoveryMail sends a password recovery mail
func (m *TemplateMailer) RecoveryMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
func (m *TemplateMailer) RecoveryMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
Token: user.RecoveryToken,
Type: "recovery",
Expand Down Expand Up @@ -258,7 +260,7 @@ func (m *TemplateMailer) RecoveryMail(user *models.User, otp, referrerURL string
}

// MagicLinkMail sends a login link mail
func (m *TemplateMailer) MagicLinkMail(user *models.User, otp, referrerURL string, externalURL *url.URL) error {
func (m *TemplateMailer) MagicLinkMail(ctx context.Context, r *http.Request, user *models.User, otp, referrerURL string, externalURL *url.URL) error {
path, err := getPath(m.Config.Mailer.URLPaths.Recovery, &EmailParams{
Token: user.RecoveryToken,
Type: "magiclink",
Expand Down

0 comments on commit d01afaa

Please sign in to comment.