From 1d68bbe87a93c124c95c6c60d05afd93c7fa269d Mon Sep 17 00:00:00 2001 From: logonoff Date: Mon, 2 Jun 2025 10:57:09 -0400 Subject: [PATCH] OCPBUGS-56915: Add i18n for `AuthenticationErrorMessage` and `GrantErrorMessage` --- pkg/server/errorpage/errorcodes.go | 17 ++++++++++------- pkg/server/errorpage/errorpage.go | 7 +++++-- pkg/server/locales/locales.go | 24 ++++++++++++++++++++++++ pkg/server/login/login.go | 2 +- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/pkg/server/errorpage/errorcodes.go b/pkg/server/errorpage/errorcodes.go index 32a60cabc..c334a8d65 100644 --- a/pkg/server/errorpage/errorcodes.go +++ b/pkg/server/errorpage/errorcodes.go @@ -1,6 +1,9 @@ package errorpage -import "github.com/openshift/oauth-server/pkg/userregistry/identitymapper" +import ( + "github.com/openshift/oauth-server/pkg/server/locales" + "github.com/openshift/oauth-server/pkg/userregistry/identitymapper" +) const ( // error occurred attempting to claim a user @@ -28,14 +31,14 @@ func AuthenticationErrorCode(err error) string { // AuthenticationErrorMessage returns an error message for the given authentication error code. // If the error code is not recognized, a generic error message is returned. -func AuthenticationErrorMessage(code string) string { +func AuthenticationErrorMessage(code string, locale locales.Localization) string { switch code { case errorCodeClaim: - return "Could not create user." + return locale["CouldNotCreateUser"] case errorCodeLookup: - return "Could not find user." + return locale["CouldNotFindUser"] default: - return "An authentication error occurred." + return locale["AnAuthenticationErrorOccurred"] } } @@ -47,6 +50,6 @@ func GrantErrorCode(err error) string { // GrantErrorMessage returns an error message for the given grant error code. // If the error is not recognized, a generic error message is returned. -func GrantErrorMessage(code string) string { - return "A grant error occurred." +func GrantErrorMessage(code string, locale locales.Localization) string { + return locale["GrantErrorOccurred"] } diff --git a/pkg/server/errorpage/errorpage.go b/pkg/server/errorpage/errorpage.go index a896c51e9..b54e31ed5 100644 --- a/pkg/server/errorpage/errorpage.go +++ b/pkg/server/errorpage/errorpage.go @@ -30,10 +30,11 @@ func (p *ErrorPage) AuthenticationError(err error, w http.ResponseWriter, req *h if !httprequest.PrefersHTML(req) { return false, err } + locale := locales.GetLocale(req.Header.Get("Accept-Language")) errorData := ErrorData{} errorData.ErrorCode = AuthenticationErrorCode(err) - errorData.Error = AuthenticationErrorMessage(errorData.ErrorCode) + errorData.Error = AuthenticationErrorMessage(errorData.ErrorCode, locale) p.render.Render(errorData, w, req) return true, nil @@ -46,9 +47,11 @@ func (p *ErrorPage) GrantError(err error, w http.ResponseWriter, req *http.Reque return false, err } + locale := locales.GetLocale(req.Header.Get("Accept-Language")) + errorData := ErrorData{} errorData.ErrorCode = GrantErrorCode(err) - errorData.Error = GrantErrorMessage(errorData.ErrorCode) + errorData.Error = GrantErrorMessage(errorData.ErrorCode, locale) p.render.Render(errorData, w, req) return true, nil diff --git a/pkg/server/locales/locales.go b/pkg/server/locales/locales.go index 8d21db5a2..2f43d0b6b 100644 --- a/pkg/server/locales/locales.go +++ b/pkg/server/locales/locales.go @@ -56,6 +56,10 @@ var locale_en = Localization{ "LoginIsRequiredPleaseTryAgain": "Login is required. Please try again.", "CouldNotCheckCSRFTokenPleaseTryAgain": "Could not check CSRF token. Please try again.", "InvalidLoginOrPasswordPleaseTryAgain": "Invalid login or password. Please try again.", + "CouldNotCreateUser": "Could not create user.", + "CouldNotFindUser": "Could not find user.", + "AnAuthenticationErrorOccurred": "An authentication error occurred.", + "GrantErrorOccurred": "A grant error occurred.", } var locale_es = Localization{ @@ -69,6 +73,10 @@ var locale_es = Localization{ "LoginIsRequiredPleaseTryAgain": "Se requiere iniciar sesión. Por favor, inténtalo de nuevo.", "CouldNotCheckCSRFTokenPleaseTryAgain": "No se pudo verificar el token CSRF. Por favor, inténtalo de nuevo.", "InvalidLoginOrPasswordPleaseTryAgain": "Usuario o contraseña inválidos. Por favor, inténtalo de nuevo.", + "CouldNotCreateUser": "No se pudo crear el usuario.", + "CouldNotFindUser": "No se pudo encontrar el usuario.", + "AnAuthenticationErrorOccurred": "Se produjo un error de autenticación.", + "GrantErrorOccurred": "Se produjo un error de concesión.", } var locale_fr = Localization{ @@ -82,6 +90,10 @@ var locale_fr = Localization{ "LoginIsRequiredPleaseTryAgain": "La connexion est requise. Veuillez réessayer.", "CouldNotCheckCSRFTokenPleaseTryAgain": "Impossible de vérifier le jeton CSRF. Veuillez réessayer.", "InvalidLoginOrPasswordPleaseTryAgain": "Identifiant ou mot de passe invalide. Veuillez réessayer.", + "CouldNotCreateUser": "Impossible de créer l'utilisateur.", + "CouldNotFindUser": "Impossible de trouver l'utilisateur.", + "AnAuthenticationErrorOccurred": "Une erreur d'authentification s'est produite.", + "GrantErrorOccurred": "Une erreur de concession s'est produite.", } var locale_zh = Localization{ @@ -95,6 +107,10 @@ var locale_zh = Localization{ "LoginIsRequiredPleaseTryAgain": "需要登录。请再次尝试。", "CouldNotCheckCSRFTokenPleaseTryAgain": "无法检查 CSRF 令牌。请重试。", "InvalidLoginOrPasswordPleaseTryAgain": "无效的登录或密码。请再次尝试。", + "CouldNotCreateUser": "无法创建用户。", + "CouldNotFindUser": "无法找到用户。", + "AnAuthenticationErrorOccurred": "发生身份验证错误。", + "GrantErrorOccurred": "发生授权错误。", } var locale_ja = Localization{ @@ -108,6 +124,10 @@ var locale_ja = Localization{ "LoginIsRequiredPleaseTryAgain": "ログインが必要です。もう一度やり直してください。", "CouldNotCheckCSRFTokenPleaseTryAgain": "CSRF トークンを確認できませんでした。もう一度やり直してください。", "InvalidLoginOrPasswordPleaseTryAgain": "無効なログインまたはパスワードです。もう一度やり直してください。", + "CouldNotCreateUser": "ユーザーを作成できませんでした。", + "CouldNotFindUser": "ユーザーが見つかりませんでした。", + "AnAuthenticationErrorOccurred": "認証エラーが発生しました。", + "GrantErrorOccurred": "許可エラーが発生しました。", } var locale_ko = Localization{ @@ -121,4 +141,8 @@ var locale_ko = Localization{ "LoginIsRequiredPleaseTryAgain": "로그인이 필요합니다. 다시 시도하십시오.", "CouldNotCheckCSRFTokenPleaseTryAgain": "CSRF 토큰을 확인할 수 없습니다. 다시 시도하십시오.", "InvalidLoginOrPasswordPleaseTryAgain": "로그인 또는 비밀번호가 잘못되었습니다. 다시 시도하십시오", + "CouldNotCreateUser": "사용자를 만들 수 없습니다.", + "CouldNotFindUser": "사용자를 찾을 수 없습니다.", + "AnAuthenticationErrorOccurred": "인증 오류가 발생했습니다.", + "GrantErrorOccurred": "권한 부여 오류가 발생했습니다.", } diff --git a/pkg/server/login/login.go b/pkg/server/login/login.go index 7f2a0e6e3..712b43105 100644 --- a/pkg/server/login/login.go +++ b/pkg/server/login/login.go @@ -137,7 +137,7 @@ func (l *Login) handleLoginForm(w http.ResponseWriter, req *http.Request) { if msg, hasMsg := form.Locale[errorMessages[form.ErrorCode]]; hasMsg { form.Error = msg } else { - form.Error = errorpage.AuthenticationErrorMessage(form.ErrorCode) + form.Error = errorpage.AuthenticationErrorMessage(form.ErrorCode, form.Locale) } }