-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from mapidentity/translations
Fixes internationalization
- Loading branch information
Showing
5 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...e-provider.resources/src/main/resources/theme/phone/login/messages/messages_de.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
updatePhoneNumber=Telefonnummer \u00E4ndern | ||
configSms2Fa=SMS-2FA konfigurieren | ||
authCodePhoneNumber=Authentifizierungscode | ||
updatePhoneNumberInfo=Bitte Mobilnummer eingeben und auf Senden klicken, um einen Code zu erhalten. Dieser Code beweist, dass Sie die Nummer besitzen. | ||
configSms2FaInfo=Bitte Mobilnummer eingeben und auf Senden klicken, um einen Code zu erhalten. Dieser Code beweist, dass Sie die Nummer besitzen. | ||
authCodeInfo=Geben Sie den Authentifizierungscode ein, den Sie auf Ihrem Mobilger\u00E4t erhalten haben. | ||
phoneNumber=Telefonnummer | ||
requiredPhoneNumber=Telefonnummer ist erforderlich. | ||
sendVerificationCode=Code senden | ||
verificationCode=Best\u00E4tigungscode | ||
authenticationCode=Authentifizierungscode | ||
noOngoingVerificationProcess=Es l\u00E4uft aktuell kein Verifikationsprozess. | ||
verificationCodeDoesNotMatch=Der eingegebene Best\u00E4tigungscode stimmt nicht mit unserem \u00FCberein. | ||
phoneTokenCodeDoesNotMatch=Der eingegebene Authentifizierungscode stimmt nicht mit unserem \u00FCberein. | ||
abusedMessageService=Zu viele Code-Anfragen f\u00FCr Ihre Mobilnummer. | ||
sendVerificationCodeFail=Fehler beim Senden des Best\u00E4tigungscodes! | ||
invalidPhoneNumber=Telefonnummer ist ung\u00FCltig | ||
invalidPhoneNumberCountryCode=Die angegebene L\u00E4ndervorwahl geh\u00F6rt nicht zu einem unterst\u00FCtzten Land oder einer nicht-geografischen Entit\u00E4t. | ||
invalidPhoneNumberTooLong=Telefonnummer ist ung\u00FCltig, zu lang! | ||
invalidPhoneNumberMustNumber=Telefonnummer ist ung\u00FCltig, muss eine Nummer sein! | ||
invalidPhoneNumberTooShort=Telefonnummer ist ung\u00FCltig, zu kurz! | ||
invalidPhoneNumberNotSupported=Telefonnummer ist ung\u00FCltig, wird nicht unterst\u00FCtzt! | ||
phoneNumberExists=Telefonnummer ist bereits registriert. | ||
requiredVerificationCode=Best\u00E4tigungscode ist erforderlich. | ||
phoneInstruction=Bitte den Telefon-Best\u00E4tigungscode verwenden, um das Passwort zur\u00FCckzusetzen. | ||
phoneUserNotFound=Benutzer existiert nicht. | ||
codeSent=OTP-Code wurde an {0} gesendet. | ||
duplicatePhoneAllowedCantLogin = Telefon-Duplikat ist erlaubt, daher kann die Anmeldung per Telefon nicht verwendet werden. \u00C4ndere --spi-phone-support-default-<$realm>-duplicate-phone-allowed auf "false". | ||
loginByPhone=Telefon | ||
loginByPassword=Passwort | ||
usernameOrPhoneNumber=Benutzername oder Telefon | ||
usernameOrEmailOrPhoneNumber=Benutzername oder E-Mail oder Telefon | ||
emailOrPhoneNumber=E-Mail oder Telefon | ||
smsCodeMessage=\u005B{0}\u005D - {1} code: {2}, l\u00E4uft ab in {3} Minuten |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 74 additions & 6 deletions
80
...rc/main/java/cc/coopersoft/keycloak/phone/providers/spi/FullSmsSenderAbstractService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,92 @@ | ||
package cc.coopersoft.keycloak.phone.providers.spi; | ||
|
||
import cc.coopersoft.keycloak.phone.Utils; | ||
import cc.coopersoft.keycloak.phone.providers.constants.TokenCodeType; | ||
import cc.coopersoft.keycloak.phone.providers.exception.MessageSendException; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
|
||
import org.jboss.logging.Logger; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.theme.Theme; | ||
|
||
public abstract class FullSmsSenderAbstractService implements MessageSenderService{ | ||
public abstract class FullSmsSenderAbstractService implements MessageSenderService { | ||
private static final Logger logger = Logger.getLogger(FullSmsSenderAbstractService.class); | ||
|
||
private final String realmDisplay; | ||
|
||
private final KeycloakSession session; | ||
|
||
@Deprecated | ||
public FullSmsSenderAbstractService(String realmDisplay) { | ||
this.realmDisplay = realmDisplay; | ||
this.session = null; | ||
} | ||
|
||
public abstract void sendMessage(String phoneNumber, String message) throws MessageSendException; | ||
public FullSmsSenderAbstractService(KeycloakSession session) { | ||
this.session = session; | ||
this.realmDisplay = session.getContext().getRealm().getDisplayName(); | ||
} | ||
|
||
public abstract void sendMessage(String phoneNumber, String message) throws MessageSendException; | ||
|
||
@Override | ||
public void sendSmsMessage(TokenCodeType type, String phoneNumber, String code , int expires, String kind) throws MessageSendException{ | ||
//TODO template from keycloak message bundle | ||
final String MESSAGE = String.format("[%s] - " + type.label + " code: %s, expires: %s minute ",realmDisplay , code, expires / 60); | ||
sendMessage(phoneNumber,MESSAGE); | ||
public void sendSmsMessage(TokenCodeType type, String phoneNumber, String code, int expires, String kind) | ||
throws MessageSendException { | ||
final String defaultMessage = String.format("[%s] - " + type.label + " code: %s, expires: %s minute ", | ||
realmDisplay, code, expires / 60); | ||
final String MESSAGE = localizeMessage(type, phoneNumber, code, expires).orElse(defaultMessage); | ||
sendMessage(phoneNumber, MESSAGE); | ||
} | ||
|
||
/** | ||
* Localizes sms code message template from login theme. | ||
* | ||
* @param type the type of code sent | ||
* @param phoneNumber the user's phone number (if applicable) | ||
* @param code the verification code | ||
* @param expires code expiration in seconds | ||
* @return The localized string, else empty. | ||
*/ | ||
private Optional<String> localizeMessage(TokenCodeType type, String phoneNumber, String code, int expires) { | ||
if (this.session != null) { | ||
try { | ||
// Get login theme | ||
final String loginThemeName = session.getContext().getRealm().getLoginTheme(); | ||
final Theme loginTheme = session.theme().getTheme(loginThemeName, Theme.Type.LOGIN); | ||
|
||
// Try get locale from user associated with phone number (if any) | ||
final Optional<UserModel> user = Utils.findUserByPhone(session, session.getContext().getRealm(), | ||
phoneNumber); | ||
final Optional<String> userLocale = user.map(u -> u.getFirstAttribute(UserModel.LOCALE)); | ||
|
||
// Use locale from user or default to realm locale | ||
final String localeName = userLocale.isPresent() ? userLocale.get() | ||
: session.getContext().getRealm().getDefaultLocale(); | ||
final Locale locale = Locale.forLanguageTag(localeName); | ||
|
||
// Get message template from login theme bundle | ||
final Properties messages = loginTheme.getMessages(locale); | ||
final String messageTemplate = messages.getProperty("smsCodeMessage"); | ||
|
||
// Return nothing when template can't be found | ||
if (messageTemplate == null || messageTemplate.isBlank()) { | ||
return Optional.empty(); | ||
} | ||
|
||
// Format message | ||
MessageFormat mf = new MessageFormat(messageTemplate, locale); | ||
return Optional.of(mf.format(new Object[] { realmDisplay, type.label, code, expires / 60 })); | ||
} catch (Exception ex) { | ||
logger.error("Error while trying to localize message", ex); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters