Skip to content

Commit

Permalink
chore: unify and improve message context (#3462)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Aug 30, 2023
1 parent 7e05b7d commit 0941bdb
Show file tree
Hide file tree
Showing 34 changed files with 289 additions and 299 deletions.
6 changes: 3 additions & 3 deletions cmd/clidoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func init() {
"NewInfoSelfServiceSettingsUpdateUnlinkOIDC": text.NewInfoSelfServiceSettingsUpdateUnlinkOIDC("{provider}"),
"NewInfoSelfServiceRegisterWebAuthn": text.NewInfoSelfServiceSettingsRegisterWebAuthn(),
"NewInfoSelfServiceRegisterWebAuthnDisplayName": text.NewInfoSelfServiceRegisterWebAuthnDisplayName(),
"NewInfoSelfServiceRemoveWebAuthn": text.NewInfoSelfServiceRemoveWebAuthn("{name}", aSecondAgo),
"NewInfoSelfServiceRemoveWebAuthn": text.NewInfoSelfServiceRemoveWebAuthn("{display_name}", aSecondAgo),
"NewErrorValidationVerificationFlowExpired": text.NewErrorValidationVerificationFlowExpired(aSecondAgo),
"NewInfoSelfServiceVerificationSuccessful": text.NewInfoSelfServiceVerificationSuccessful(),
"NewVerificationEmailSent": text.NewVerificationEmailSent(),
Expand All @@ -83,7 +83,7 @@ func init() {
"NewErrorValidationVerificationCodeInvalidOrAlreadyUsed": text.NewErrorValidationVerificationCodeInvalidOrAlreadyUsed(),
"NewErrorSystemGeneric": text.NewErrorSystemGeneric("{reason}"),
"NewValidationErrorGeneric": text.NewValidationErrorGeneric("{reason}"),
"NewValidationErrorRequired": text.NewValidationErrorRequired("{field}"),
"NewValidationErrorRequired": text.NewValidationErrorRequired("{property}"),
"NewErrorValidationMinLength": text.NewErrorValidationMinLength(5, 3),
"NewErrorValidationMaxLength": text.NewErrorValidationMaxLength(5, 6),
"NewErrorValidationInvalidFormat": text.NewErrorValidationInvalidFormat("{pattern}"),
Expand All @@ -105,7 +105,7 @@ func init() {
"NewErrorValidationPasswordTooManyBreaches": text.NewErrorValidationPasswordTooManyBreaches(101),
"NewErrorValidationInvalidCredentials": text.NewErrorValidationInvalidCredentials(),
"NewErrorValidationDuplicateCredentials": text.NewErrorValidationDuplicateCredentials(),
"NewErrorValidationDuplicateCredentialsWithHints": text.NewErrorValidationDuplicateCredentialsWithHints("{reason}", nil, nil, ""),
"NewErrorValidationDuplicateCredentialsWithHints": text.NewErrorValidationDuplicateCredentialsWithHints([]string{"{available_credential_types_list}"}, []string{"{available_oidc_providers_list}"}, "{credential_identifier_hint}"),
"NewErrorValidationDuplicateCredentialsOnOIDCLink": text.NewErrorValidationDuplicateCredentialsOnOIDCLink(),
"NewErrorValidationTOTPVerifierWrong": text.NewErrorValidationTOTPVerifierWrong(),
"NewErrorValidationLookupAlreadyUsed": text.NewErrorValidationLookupAlreadyUsed(),
Expand Down
6 changes: 4 additions & 2 deletions identity/.snapshots/TestToNode.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"text": "Secret was used at 2021-08-17 11:32:38 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:38Z"
"used_at": "2021-08-17T11:32:38Z",
"used_at_unix": 1629199958
}
},
{
Expand All @@ -37,7 +38,8 @@
"text": "Secret was used at 2021-08-17 11:32:48 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:48Z"
"used_at": "2021-08-17T11:32:48Z",
"used_at_unix": 1629199968
}
}
]
Expand Down
39 changes: 1 addition & 38 deletions schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ package schema

import (
"fmt"
"strings"

"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/pkg/errors"

Expand Down Expand Up @@ -146,39 +142,6 @@ type DuplicateCredentialsHinter interface {

func NewDuplicateCredentialsError(err error) error {
if hinter := DuplicateCredentialsHinter(nil); errors.As(err, &hinter) && hinter.HasHints() {
oidcProviders := make([]string, 0, len(hinter.AvailableOIDCProviders()))
for _, provider := range hinter.AvailableOIDCProviders() {
oidcProviders = append(oidcProviders, cases.Title(language.English).String(provider))
}

reason := ""
if hinter.IdentifierHint() != "" {
reason = fmt.Sprintf("You tried signing with %s which is already in use by another account.", hinter.IdentifierHint())
} else {
reason = "You tried to sign up using an email, phone, or username that is already used by another account."
}

if len(hinter.AvailableCredentials()) > 0 {
humanReadable := make([]string, 0, len(hinter.AvailableCredentials()))
for _, cred := range hinter.AvailableCredentials() {
switch cred {
case "password":
humanReadable = append(humanReadable, "your password")
case "oidc":
humanReadable = append(humanReadable, "social sign in")
case "webauthn":
humanReadable = append(humanReadable, "your PassKey or a security key")
}
}

reason = fmt.Sprintf("%s You can sign in using %s.", reason, strings.Join(humanReadable, ", "))
if len(hinter.AvailableOIDCProviders()) > 0 {
reason = fmt.Sprintf("%s Use one of the following social sign in providers: %s", reason, strings.Join(oidcProviders, ", "))
}
} else if len(hinter.AvailableOIDCProviders()) > 0 {
reason = fmt.Sprintf("%s You can sign in using one of the following social sign in providers: %s.", reason, strings.Join(oidcProviders, ", "))
}

return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `an account with the same identifier (email, phone, username, ...) exists already`,
Expand All @@ -189,7 +152,7 @@ func NewDuplicateCredentialsError(err error) error {
IdentifierHint: hinter.IdentifierHint(),
},
},
Messages: new(text.Messages).Add(text.NewErrorValidationDuplicateCredentialsWithHints(reason, hinter.AvailableCredentials(), hinter.AvailableOIDCProviders(), hinter.IdentifierHint())),
Messages: new(text.Messages).Add(text.NewErrorValidationDuplicateCredentialsWithHints(hinter.AvailableCredentials(), hinter.AvailableOIDCProviders(), hinter.IdentifierHint())),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"id": 4000001,
"text": "The active recovery strategy code is not enabled. Please enable it in the configuration.",
"type": "error",
"context": {}
"context": {
"reason": "The active recovery strategy code is not enabled. Please enable it in the configuration."
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"id": 4000001,
"text": "The active recovery strategy code is not enabled. Please enable it in the configuration.",
"type": "error",
"context": {}
"context": {
"reason": "The active recovery strategy code is not enabled. Please enable it in the configuration."
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"messages": [],
"meta": {
"label": {
"context": {},
"id": 1010007,
"text": "Backup recovery code",
"type": "info"
Expand All @@ -45,7 +44,6 @@
"messages": [],
"meta": {
"label": {
"context": {},
"id": 1010010,
"text": "Use backup recovery code",
"type": "info"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"text": "Secret was used at 2021-08-17 11:32:39 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:39Z"
"used_at": "2021-08-17T11:32:39Z",
"used_at_unix": 1629199959
}
},
{
Expand All @@ -59,7 +60,8 @@
"text": "Secret was used at 2021-08-17 11:32:42 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:42Z"
"used_at": "2021-08-17T11:32:42Z",
"used_at_unix": 1629199962
}
},
{
Expand All @@ -83,7 +85,8 @@
"text": "Secret was used at 2021-08-17 11:32:45 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:45Z"
"used_at": "2021-08-17T11:32:45Z",
"used_at_unix": 1629199965
}
},
{
Expand All @@ -107,7 +110,8 @@
"text": "Secret was used at 2021-08-17 11:32:48 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:48Z"
"used_at": "2021-08-17T11:32:48Z",
"used_at_unix": 1629199968
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"text": "Secret was used at 2021-08-17 11:32:39 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:39Z"
"used_at": "2021-08-17T11:32:39Z",
"used_at_unix": 1629199959
}
},
{
Expand All @@ -59,7 +60,8 @@
"text": "Secret was used at 2021-08-17 11:32:42 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:42Z"
"used_at": "2021-08-17T11:32:42Z",
"used_at_unix": 1629199962
}
},
{
Expand All @@ -83,7 +85,8 @@
"text": "Secret was used at 2021-08-17 11:32:45 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:45Z"
"used_at": "2021-08-17T11:32:45Z",
"used_at_unix": 1629199965
}
},
{
Expand All @@ -107,7 +110,8 @@
"text": "Secret was used at 2021-08-17 11:32:48 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:48Z"
"used_at": "2021-08-17T11:32:48Z",
"used_at_unix": 1629199968
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"text": "Secret was used at 2021-08-17 11:32:39 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:39Z"
"used_at": "2021-08-17T11:32:39Z",
"used_at_unix": 1629199959
}
},
{
Expand All @@ -59,7 +60,8 @@
"text": "Secret was used at 2021-08-17 11:32:42 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:42Z"
"used_at": "2021-08-17T11:32:42Z",
"used_at_unix": 1629199962
}
},
{
Expand All @@ -83,7 +85,8 @@
"text": "Secret was used at 2021-08-17 11:32:45 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:45Z"
"used_at": "2021-08-17T11:32:45Z",
"used_at_unix": 1629199965
}
},
{
Expand All @@ -107,7 +110,8 @@
"text": "Secret was used at 2021-08-17 11:32:48 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:48Z"
"used_at": "2021-08-17T11:32:48Z",
"used_at_unix": 1629199968
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"text": "Secret was used at 2021-08-17 11:32:39 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:39Z"
"used_at": "2021-08-17T11:32:39Z",
"used_at_unix": 1629199959
}
},
{
Expand All @@ -59,7 +60,8 @@
"text": "Secret was used at 2021-08-17 11:32:42 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:42Z"
"used_at": "2021-08-17T11:32:42Z",
"used_at_unix": 1629199962
}
},
{
Expand All @@ -83,7 +85,8 @@
"text": "Secret was used at 2021-08-17 11:32:45 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:45Z"
"used_at": "2021-08-17T11:32:45Z",
"used_at_unix": 1629199965
}
},
{
Expand All @@ -107,7 +110,8 @@
"text": "Secret was used at 2021-08-17 11:32:48 +0000 UTC",
"type": "info",
"context": {
"used_at": "2021-08-17T11:32:48Z"
"used_at": "2021-08-17T11:32:48Z",
"used_at_unix": 1629199968
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions selfservice/strategy/password/registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestRegistration(t *testing.T) {
body := testhelpers.SubmitRegistrationForm(t, true, apiClient, publicTS,
applyTransform(values, transform), false, http.StatusBadRequest,
publicTS.URL+registration.RouteSubmitFlow)
assert.Contains(t, gjson.Get(body, "ui.messages.0.text").String(), "You tried signing with registration-identifier-8-api-duplicate-"+suffix+" which is already in use by another account. You can sign in using your password.", "%s", body)
assert.Contains(t, gjson.Get(body, "ui.messages.0.text").String(), "You tried signing in with registration-identifier-8-api-duplicate-"+suffix+" which is already in use by another account. You can sign in using your password.", "%s", body)
})

t.Run("type=spa", func(t *testing.T) {
Expand All @@ -314,7 +314,7 @@ func TestRegistration(t *testing.T) {

_ = expectSuccessfulLogin(t, false, true, nil, values)
body := registrationhelpers.ExpectValidationError(t, publicTS, conf, "spa", applyTransform(values, transform))
assert.Contains(t, gjson.Get(body, "ui.messages.0.text").String(), "You tried signing with registration-identifier-8-spa-duplicate-"+suffix+" which is already in use by another account. You can sign in using your password.", "%s", body)
assert.Contains(t, gjson.Get(body, "ui.messages.0.text").String(), "You tried signing in with registration-identifier-8-spa-duplicate-"+suffix+" which is already in use by another account. You can sign in using your password.", "%s", body)
})

t.Run("type=browser", func(t *testing.T) {
Expand All @@ -326,7 +326,7 @@ func TestRegistration(t *testing.T) {

_ = expectSuccessfulLogin(t, false, false, nil, values)
body := registrationhelpers.ExpectValidationError(t, publicTS, conf, "browser", applyTransform(values, transform))
assert.Contains(t, gjson.Get(body, "ui.messages.0.text").String(), "You tried signing with registration-identifier-8-browser-duplicate-"+suffix+" which is already in use by another account. You can sign in using your password.", "%s", body)
assert.Contains(t, gjson.Get(body, "ui.messages.0.text").String(), "You tried signing in with registration-identifier-8-browser-duplicate-"+suffix+" which is already in use by another account. You can sign in using your password.", "%s", body)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"messages": [],
"meta": {
"label": {
"context": {},
"id": 1010006,
"text": "Authentication code",
"type": "info"
Expand All @@ -45,7 +44,6 @@
"messages": [],
"meta": {
"label": {
"context": {},
"id": 1010009,
"text": "Use Authenticator",
"type": "info"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
"id": 4000008,
"text": "The provided authentication code is invalid, please try again.",
"type": "error",
"context": {}
"type": "error"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
"messages": [
{
"text": "Prepare your WebAuthn device (e.g. security key, biometrics scanner, ...) and press continue.",
"type": "info",
"context": {}
"type": "info"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
"messages": [
{
"text": "Prepare your WebAuthn device (e.g. security key, biometrics scanner, ...) and press continue.",
"type": "info",
"context": {}
"type": "info"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"messages": [],
"meta": {
"label": {
"context": {},
"id": 1010001,
"text": "Sign in with security key",
"type": "info"
Expand Down
Loading

0 comments on commit 0941bdb

Please sign in to comment.