Skip to content

Commit

Permalink
Rename Localize to Localizef
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Dec 29, 2023
1 parent a0994c3 commit 3abb766
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 57 deletions.
4 changes: 2 additions & 2 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (a *Auth) LoginPost(w http.ResponseWriter, r *http.Request) error {
pidUser, err := a.Authboss.Storage.Server.Load(r.Context(), pid)
if err == authboss.ErrUserNotFound {
logger.Infof("failed to load user requested by pid: %s", pid)
data := authboss.HTMLData{authboss.DataErr: a.Localize(r.Context(), authboss.TxtInvalidCredentials)}
data := authboss.HTMLData{authboss.DataErr: a.Localizef(r.Context(), authboss.TxtInvalidCredentials)}
return a.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageLogin, data)
} else if err != nil {
return err
Expand All @@ -85,7 +85,7 @@ func (a *Auth) LoginPost(w http.ResponseWriter, r *http.Request) error {
}

logger.Infof("user %s failed to log in", pid)
data := authboss.HTMLData{authboss.DataErr: a.Localize(r.Context(), authboss.TxtInvalidCredentials)}
data := authboss.HTMLData{authboss.DataErr: a.Localizef(r.Context(), authboss.TxtInvalidCredentials)}
return a.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageLogin, data)
}

Expand Down
8 changes: 4 additions & 4 deletions authboss.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ func (a *Authboss) VerifyPassword(user AuthableUser, password string) error {
return a.Core.Hasher.CompareHashAndPassword(user.GetPassword(), password)
}

// Localize is a helper to translate a key using the translator
// Localizef is a helper to translate a key using the translator
// If the localizer is nil or returns an empty string,
// then the original text will be returned using [fmt.Sprintf] to interpolate the args.
func (a *Authboss) Localize(ctx context.Context, text string, args ...any) string {
func (a *Authboss) Localizef(ctx context.Context, text string, args ...any) string {
if a.Config.Core.Localizer == nil {
return fmt.Sprintf(text, args...)
}

if translated := a.Config.Core.Localizer.Localize(ctx, text, args...); translated != "" {
if translated := a.Config.Core.Localizer.Localizef(ctx, text, args...); translated != "" {
return translated
}

Expand Down Expand Up @@ -231,7 +231,7 @@ func MountedMiddleware2(ab *Authboss, mountPathed bool, reqs MWRequirements, fai

ro := RedirectOptions{
Code: http.StatusTemporaryRedirect,
Failure: ab.Localize(r.Context(), TxtAuthFailed),
Failure: ab.Localizef(r.Context(), TxtAuthFailed),
RedirectPath: path.Join(ab.Config.Paths.Mount, fmt.Sprintf("/login?%s", vals.Encode())),
}

Expand Down
12 changes: 6 additions & 6 deletions confirm/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *Confirm) PreventAuth(w http.ResponseWriter, r *http.Request, handled bo
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
RedirectPath: c.Authboss.Config.Paths.ConfirmNotOK,
Failure: c.Localize(r.Context(), authboss.TxtAccountNotConfirmed),
Failure: c.Localizef(r.Context(), authboss.TxtAccountNotConfirmed),
}
return true, c.Authboss.Config.Core.Redirector.Redirect(w, r, ro)
}
Expand All @@ -114,7 +114,7 @@ func (c *Confirm) StartConfirmationWeb(w http.ResponseWriter, r *http.Request, h
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
RedirectPath: c.Authboss.Config.Paths.ConfirmNotOK,
Success: c.Localize(r.Context(), authboss.TxtConfirmYourAccount),
Success: c.Localizef(r.Context(), authboss.TxtConfirmYourAccount),
}
return true, c.Authboss.Config.Core.Redirector.Redirect(w, r, ro)
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func (c *Confirm) SendConfirmEmail(ctx context.Context, to, token string) {
To: []string{to},
From: c.Config.Mail.From,
FromName: c.Config.Mail.FromName,
Subject: c.Config.Mail.SubjectPrefix + c.Localize(ctx, authboss.TxtConfirmEmailSubject),
Subject: c.Config.Mail.SubjectPrefix + c.Localizef(ctx, authboss.TxtConfirmEmailSubject),
}

logger.Infof("sending confirm e-mail to: %s", to)
Expand Down Expand Up @@ -236,7 +236,7 @@ func (c *Confirm) Get(w http.ResponseWriter, r *http.Request) error {

ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
Success: c.Localize(r.Context(), authboss.TxtConfrimationSuccess),
Success: c.Localizef(r.Context(), authboss.TxtConfrimationSuccess),
RedirectPath: c.Authboss.Config.Paths.ConfirmOK,
}
return c.Authboss.Config.Core.Redirector.Redirect(w, r, ro)
Expand All @@ -256,7 +256,7 @@ func (c *Confirm) mailURL(token string) string {
func (c *Confirm) invalidToken(w http.ResponseWriter, r *http.Request) error {
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
Failure: c.Localize(r.Context(), authboss.TxtInvalidConfirmToken),
Failure: c.Localizef(r.Context(), authboss.TxtInvalidConfirmToken),
RedirectPath: c.Authboss.Config.Paths.ConfirmNotOK,
}
return c.Authboss.Config.Core.Redirector.Redirect(w, r, ro)
Expand Down Expand Up @@ -284,7 +284,7 @@ func Middleware(ab *authboss.Authboss) func(http.Handler) http.Handler {
logger.Infof("user %s prevented from accessing %s: not confirmed", user.GetPID(), r.URL.Path)
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
Failure: ab.Localize(r.Context(), authboss.TxtAccountNotConfirmed),
Failure: ab.Localizef(r.Context(), authboss.TxtAccountNotConfirmed),
RedirectPath: ab.Config.Paths.ConfirmNotOK,
}
if err := ab.Config.Core.Redirector.Redirect(w, r, ro); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions confirm/confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestGetValidationFailure(t *testing.T) {
if p := harness.redirector.Options.RedirectPath; p != harness.ab.Paths.ConfirmNotOK {
t.Error("redir path was wrong:", p)
}
if reason := harness.redirector.Options.Failure; reason != harness.ab.Localize(context.Background(), authboss.TxtInvalidConfirmToken) {
if reason := harness.redirector.Options.Failure; reason != harness.ab.Localizef(context.Background(), authboss.TxtInvalidConfirmToken) {
t.Error("reason for failure was wrong:", reason)
}
}
Expand All @@ -262,7 +262,7 @@ func TestGetBase64DecodeFailure(t *testing.T) {
if p := harness.redirector.Options.RedirectPath; p != harness.ab.Paths.ConfirmNotOK {
t.Error("redir path was wrong:", p)
}
if reason := harness.redirector.Options.Failure; reason != harness.ab.Localize(context.Background(), authboss.TxtInvalidConfirmToken) {
if reason := harness.redirector.Options.Failure; reason != harness.ab.Localizef(context.Background(), authboss.TxtInvalidConfirmToken) {
t.Error("reason for failure was wrong:", reason)
}
}
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestGetUserNotFoundFailure(t *testing.T) {
if p := harness.redirector.Options.RedirectPath; p != harness.ab.Paths.ConfirmNotOK {
t.Error("redir path was wrong:", p)
}
if reason := harness.redirector.Options.Failure; reason != harness.ab.Localize(context.Background(), authboss.TxtInvalidConfirmToken) {
if reason := harness.redirector.Options.Failure; reason != harness.ab.Localizef(context.Background(), authboss.TxtInvalidConfirmToken) {
t.Error("reason for failure was wrong:", reason)
}
}
Expand Down
6 changes: 4 additions & 2 deletions localizer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package authboss

import "context"
import (
"context"
)

type Localizer interface {
// Get the translation for the given text in the given context.
// If no translation is found, an empty string should be returned.
Localize(ctx context.Context, txt string, args ...any) string
Localizef(ctx context.Context, txt string, args ...any) string
}

// Translation constants
Expand Down
4 changes: 2 additions & 2 deletions lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (l *Lock) updateLockedState(w http.ResponseWriter, r *http.Request, wasCorr

ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
Failure: l.Localize(r.Context(), authboss.TxtLocked),
Failure: l.Localizef(r.Context(), authboss.TxtLocked),
RedirectPath: l.Authboss.Config.Paths.LockNotOK,
}
return true, l.Authboss.Config.Core.Redirector.Redirect(w, r, ro)
Expand Down Expand Up @@ -158,7 +158,7 @@ func Middleware(ab *authboss.Authboss) func(http.Handler) http.Handler {
logger.Infof("user %s prevented from accessing %s: locked", user.GetPID(), r.URL.Path)
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
Failure: ab.Localize(r.Context(), authboss.TxtLocked),
Failure: ab.Localizef(r.Context(), authboss.TxtLocked),
RedirectPath: ab.Config.Paths.LockNotOK,
}
if err := ab.Config.Core.Redirector.Redirect(w, r, ro); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (o *OAuth2) End(w http.ResponseWriter, r *http.Request) error {
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
RedirectPath: o.Authboss.Config.Paths.OAuth2LoginNotOK,
Failure: o.Localize(r.Context(), authboss.TxtOAuth2LoginNotOK, provider),
Failure: o.Localizef(r.Context(), authboss.TxtOAuth2LoginNotOK, provider),
}
return o.Authboss.Core.Redirector.Redirect(w, r, ro)
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (o *OAuth2) End(w http.ResponseWriter, r *http.Request) error {
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
RedirectPath: redirect,
Success: o.Localize(r.Context(), authboss.TxtOAuth2LoginOK, provider),
Success: o.Localizef(r.Context(), authboss.TxtOAuth2LoginOK, provider),
}
return o.Authboss.Config.Core.Redirector.Redirect(w, r, ro)
}
Expand Down
6 changes: 3 additions & 3 deletions otp/otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (o *OTP) LoginPost(w http.ResponseWriter, r *http.Request) error {
pidUser, err := o.Authboss.Storage.Server.Load(r.Context(), pid)
if err == authboss.ErrUserNotFound {
logger.Infof("failed to load user requested by pid: %s", pid)
data := authboss.HTMLData{authboss.DataErr: o.Localize(r.Context(), authboss.TxtInvalidCredentials)}
data := authboss.HTMLData{authboss.DataErr: o.Localizef(r.Context(), authboss.TxtInvalidCredentials)}
return o.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageLogin, data)
} else if err != nil {
return err
Expand Down Expand Up @@ -153,7 +153,7 @@ func (o *OTP) LoginPost(w http.ResponseWriter, r *http.Request) error {
}

logger.Infof("user %s failed to log in with otp", pid)
data := authboss.HTMLData{authboss.DataErr: o.Localize(r.Context(), authboss.TxtInvalidCredentials)}
data := authboss.HTMLData{authboss.DataErr: o.Localizef(r.Context(), authboss.TxtInvalidCredentials)}
return o.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageLogin, data)
}

Expand Down Expand Up @@ -218,7 +218,7 @@ func (o *OTP) AddPost(w http.ResponseWriter, r *http.Request) error {
currentOTPs := splitOTPs(otpUser.GetOTPs())

if len(currentOTPs) >= maxOTPs {
data := authboss.HTMLData{authboss.DataValidation: o.Localize(r.Context(), authboss.TxtTooManyOTPs, maxOTPs)}
data := authboss.HTMLData{authboss.DataValidation: o.Localizef(r.Context(), authboss.TxtTooManyOTPs, maxOTPs)}
return o.Core.Responder.Respond(w, r, http.StatusOK, PageAdd, data)
}

Expand Down
6 changes: 3 additions & 3 deletions otp/twofactor/sms2fa/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (s *SMS) PostSetup(w http.ResponseWriter, r *http.Request) error {
if len(number) == 0 {
data := authboss.HTMLData{
authboss.DataValidation: map[string][]string{FormValuePhoneNumber: {
s.Localize(r.Context(), authboss.TxtSMSNumberRequired),
s.Localizef(r.Context(), authboss.TxtSMSNumberRequired),
}},
}
return s.Core.Responder.Respond(w, r, http.StatusOK, PageSMSSetup, data)
Expand Down Expand Up @@ -357,7 +357,7 @@ func (s *SMSValidator) sendCode(w http.ResponseWriter, r *http.Request, user Use
var data authboss.HTMLData
err := s.SendCodeToUser(w, r, user.GetPID(), phoneNumber)
if err == errSMSRateLimit {
data = authboss.HTMLData{authboss.DataErr: s.Localize(r.Context(), authboss.TxtSMSWaitToResend)}
data = authboss.HTMLData{authboss.DataErr: s.Localizef(r.Context(), authboss.TxtSMSWaitToResend)}
} else if err != nil {
return err
}
Expand Down Expand Up @@ -403,7 +403,7 @@ func (s *SMSValidator) validateCode(w http.ResponseWriter, r *http.Request, user

logger.Infof("user %s sms 2fa failure (wrong code)", user.GetPID())
data := authboss.HTMLData{
authboss.DataValidation: map[string][]string{FormValueCode: {s.Localize(r.Context(), authboss.TxtInvalid2FACode)}},
authboss.DataValidation: map[string][]string{FormValueCode: {s.Localizef(r.Context(), authboss.TxtInvalid2FACode)}},
}
return s.Authboss.Core.Responder.Respond(w, r, http.StatusOK, s.Page, data)
}
Expand Down
6 changes: 3 additions & 3 deletions otp/twofactor/sms2fa/sms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestPostSetup(t *testing.T) {
t.Error("page wrong:", h.responder.Page)
}
validation := h.responder.Data[authboss.DataValidation].(map[string][]string)
if got := validation[FormValuePhoneNumber][0]; got != h.ab.Localize(context.Background(), authboss.TxtSMSNumberRequired) {
if got := validation[FormValuePhoneNumber][0]; got != h.ab.Localizef(context.Background(), authboss.TxtSMSNumberRequired) {
t.Error("data wrong:", got)
}
})
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestValidatorPostOk(t *testing.T) {
w.WriteHeader(http.StatusOK)

validation := h.responder.Data[authboss.DataValidation].(map[string][]string)
if got := validation[FormValueCode][0]; got != h.ab.Localize(context.Background(), authboss.TxtInvalid2FACode) {
if got := validation[FormValueCode][0]; got != h.ab.Localizef(context.Background(), authboss.TxtInvalid2FACode) {
t.Error("data wrong:", got)
}
})
Expand All @@ -574,7 +574,7 @@ func TestValidatorPostOk(t *testing.T) {
t.Error("page wrong:", h.responder.Page)
}
validation := h.responder.Data[authboss.DataValidation].(map[string][]string)
if got := validation[FormValueCode][0]; got != h.ab.Localize(context.Background(), authboss.TxtInvalid2FACode) {
if got := validation[FormValueCode][0]; got != h.ab.Localizef(context.Background(), authboss.TxtInvalid2FACode) {
t.Error("data wrong:", got)
}
})
Expand Down
20 changes: 10 additions & 10 deletions otp/twofactor/totp2fa/totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (t *TOTP) PostConfirm(w http.ResponseWriter, r *http.Request) error {
if !ok {
data := authboss.HTMLData{
authboss.DataValidation: map[string][]string{FormValueCode: {
t.Localize(r.Context(), authboss.TxtInvalid2FACode),
t.Localizef(r.Context(), authboss.TxtInvalid2FACode),
}},
DataTOTPSecret: totpSecret,
}
Expand Down Expand Up @@ -325,11 +325,11 @@ func (t *TOTP) PostRemove(w http.ResponseWriter, r *http.Request) error {
user, status, err := t.validate(r)
switch {
case err == errNoTOTPEnabled:
data := authboss.HTMLData{authboss.DataErr: t.Localize(r.Context(), authboss.TxtTOTP2FANotActive)}
data := authboss.HTMLData{authboss.DataErr: t.Localizef(r.Context(), authboss.TxtTOTP2FANotActive)}
return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPRemove, data)
case err != nil:
return err
case status != t.Localize(r.Context(), authboss.TxtSuccess):
case status != t.Localizef(r.Context(), authboss.TxtSuccess):
logger.Infof("user %s totp 2fa removal failure (%s)", user.GetPID(), status)
data := authboss.HTMLData{
authboss.DataValidation: map[string][]string{FormValueCode: {status}},
Expand Down Expand Up @@ -368,12 +368,12 @@ func (t *TOTP) PostValidate(w http.ResponseWriter, r *http.Request) error {
switch {
case err == errNoTOTPEnabled:
logger.Infof("user %s totp failure (not enabled)", user.GetPID())
data := authboss.HTMLData{authboss.DataErr: t.Localize(
data := authboss.HTMLData{authboss.DataErr: t.Localizef(
r.Context(), authboss.TxtTOTP2FANotActive)}
return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPValidate, data)
case err != nil:
return err
case status != t.Localize(r.Context(), authboss.TxtSuccess):
case status != t.Localizef(r.Context(), authboss.TxtSuccess):
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
handled, err := t.Authboss.Events.FireAfter(authboss.EventAuthFail, w, r)
if err != nil {
Expand Down Expand Up @@ -471,25 +471,25 @@ func (t *TOTP) validate(r *http.Request) (User, string, error) {
return nil, "", err
}
} else {
return user, t.Localize(r.Context(), authboss.TxtInvalid2FACode), nil
return user, t.Localizef(r.Context(), authboss.TxtInvalid2FACode), nil
}

return user, t.Localize(r.Context(), authboss.TxtSuccess), nil
return user, t.Localizef(r.Context(), authboss.TxtSuccess), nil
}

input := totpCodeValues.GetCode()

if oneTime, ok := user.(UserOneTime); ok {
oldCode := oneTime.GetTOTPLastCode()
if oldCode == input {
return user, t.Localize(r.Context(), authboss.TxtRepeated2FACode), nil
return user, t.Localizef(r.Context(), authboss.TxtRepeated2FACode), nil
}
oneTime.PutTOTPLastCode(input)
}

if !totp.Validate(input, secret) {
return user, t.Localize(r.Context(), authboss.TxtInvalid2FACode), nil
return user, t.Localizef(r.Context(), authboss.TxtInvalid2FACode), nil
}

return user, t.Localize(r.Context(), authboss.TxtSuccess), nil
return user, t.Localizef(r.Context(), authboss.TxtSuccess), nil
}
12 changes: 6 additions & 6 deletions otp/twofactor/totp2fa/totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func TestPostRemove(t *testing.T) {
if h.responder.Page != PageTOTPRemove {
t.Error("page wrong:", h.responder.Page)
}
if got := h.responder.Data[authboss.DataErr]; got != h.ab.Localize(context.Background(), authboss.TxtTOTP2FANotActive) {
if got := h.responder.Data[authboss.DataErr]; got != h.ab.Localizef(context.Background(), authboss.TxtTOTP2FANotActive) {
t.Error("data wrong:", got)
}
})
Expand All @@ -404,7 +404,7 @@ func TestPostRemove(t *testing.T) {
if h.responder.Page != PageTOTPRemove {
t.Error("page wrong:", h.responder.Page)
}
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localize(context.Background(), authboss.TxtInvalid2FACode) {
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localizef(context.Background(), authboss.TxtInvalid2FACode) {
t.Error("data wrong:", got)
}
})
Expand Down Expand Up @@ -486,7 +486,7 @@ func TestPostValidate(t *testing.T) {
if h.responder.Page != PageTOTPValidate {
t.Error("page wrong:", h.responder.Page)
}
if got := h.responder.Data[authboss.DataErr]; got != h.ab.Localize(context.Background(), authboss.TxtTOTP2FANotActive) {
if got := h.responder.Data[authboss.DataErr]; got != h.ab.Localizef(context.Background(), authboss.TxtTOTP2FANotActive) {
t.Error("data wrong:", got)
}
})
Expand All @@ -509,7 +509,7 @@ func TestPostValidate(t *testing.T) {
if h.responder.Page != PageTOTPValidate {
t.Error("page wrong:", h.responder.Page)
}
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localize(context.Background(), authboss.TxtInvalid2FACode) {
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localizef(context.Background(), authboss.TxtInvalid2FACode) {
t.Error("data wrong:", got)
}
})
Expand All @@ -533,7 +533,7 @@ func TestPostValidate(t *testing.T) {
if h.responder.Page != PageTOTPValidate {
t.Error("page wrong:", h.responder.Page)
}
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localize(context.Background(), authboss.TxtRepeated2FACode) {
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localizef(context.Background(), authboss.TxtRepeated2FACode) {
t.Error("data wrong:", got)
}
})
Expand Down Expand Up @@ -621,7 +621,7 @@ func TestPostValidate(t *testing.T) {
// Flush client state
w.WriteHeader(http.StatusOK)

if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localize(context.Background(), authboss.TxtInvalid2FACode) {
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != h.ab.Localizef(context.Background(), authboss.TxtInvalid2FACode) {
t.Error("data wrong:", got)
}
})
Expand Down
Loading

0 comments on commit 3abb766

Please sign in to comment.