use of javax.security.auth.callback.LanguageCallback in project jdk8u_jdk by JetBrains.
the class CustomLoginModule method login.
/*
* Authenticate the user.
*/
@Override
public boolean login() throws LoginException {
// prompt for a user name and password
if (callbackHandler == null) {
throw new LoginException("No CallbackHandler available");
}
// standard callbacks
NameCallback name = new NameCallback("username: ", "default");
PasswordCallback passwd = new PasswordCallback("password: ", false);
LanguageCallback language = new LanguageCallback();
TextOutputCallback error = new TextOutputCallback(TextOutputCallback.ERROR, "This is an error");
TextOutputCallback warning = new TextOutputCallback(TextOutputCallback.WARNING, "This is a warning");
TextOutputCallback info = new TextOutputCallback(TextOutputCallback.INFORMATION, "This is a FYI");
TextInputCallback text = new TextInputCallback("Please type " + HELLO, "Bye");
ChoiceCallback choice = new ChoiceCallback("Choice: ", new String[] { "pass", "fail" }, 1, true);
ConfirmationCallback confirmation = new ConfirmationCallback("confirmation: ", ConfirmationCallback.INFORMATION, ConfirmationCallback.YES_NO_OPTION, ConfirmationCallback.NO);
CustomCallback custom = new CustomCallback();
Callback[] callbacks = new Callback[] { choice, info, warning, error, name, passwd, text, language, confirmation, custom };
boolean uce = false;
try {
callbackHandler.handle(callbacks);
} catch (UnsupportedCallbackException e) {
Callback callback = e.getCallback();
if (custom.equals(callback)) {
uce = true;
System.out.println("CustomLoginModule: " + "custom callback not supported as expected");
} else {
throw new LoginException("Unsupported callback: " + callback);
}
} catch (IOException ioe) {
throw new LoginException(ioe.toString());
}
if (!uce) {
throw new RuntimeException("UnsupportedCallbackException " + "not thrown");
}
if (!HELLO.equals(text.getText())) {
System.out.println("Text: " + text.getText());
throw new FailedLoginException("No hello");
}
if (!Locale.GERMANY.equals(language.getLocale())) {
System.out.println("Selected locale: " + language.getLocale());
throw new FailedLoginException("Achtung bitte");
}
String readUsername = name.getName();
char[] readPassword = passwd.getPassword();
if (readPassword == null) {
// treat a NULL password as an empty password
readPassword = new char[0];
}
passwd.clearPassword();
// verify the username/password
if (!username.equals(readUsername) || !Arrays.equals(password, readPassword)) {
loginSucceeded = false;
throw new FailedLoginException("Username/password is not correct");
}
// check chosen option
int[] selected = choice.getSelectedIndexes();
if (selected == null || selected.length == 0) {
throw new FailedLoginException("Nothing selected");
}
if (selected[0] != 0) {
throw new FailedLoginException("Wrong choice: " + selected[0]);
}
// check confirmation
if (confirmation.getSelectedIndex() != ConfirmationCallback.YES) {
throw new FailedLoginException("Not confirmed: " + confirmation.getSelectedIndex());
}
loginSucceeded = true;
System.out.println("CustomLoginModule: authentication succeeded");
return true;
}
use of javax.security.auth.callback.LanguageCallback in project OpenAM by OpenRock.
the class AuthXMLUtils method createLanguageCallback.
static LanguageCallback createLanguageCallback(Node childNode, Callback callback) {
LanguageCallback languageCallback = null;
if (callback != null) {
if (callback instanceof LanguageCallback) {
languageCallback = (LanguageCallback) callback;
}
}
if (languageCallback == null) {
languageCallback = new LanguageCallback();
}
Node localeNode = XMLUtils.getChildNode(childNode, "Locale");
String language = XMLUtils.getNodeAttributeValue(localeNode, AuthXMLTags.ATTRIBUTE_LANG);
String country = XMLUtils.getNodeAttributeValue(localeNode, AuthXMLTags.ATTRIBUTE_COUNTRY);
String variant = XMLUtils.getNodeAttributeValue(localeNode, AuthXMLTags.ATTRIBUTE_VARIANT);
if (debug.messageEnabled()) {
debug.message("Language is " + language);
debug.message("Country is " + country);
debug.message("Variant is " + variant);
}
if ((language != null) && (country != null)) {
java.util.Locale locale = null;
if (variant != null) {
locale = new java.util.Locale(language, country, variant);
} else {
locale = new java.util.Locale(language, country);
}
languageCallback.setLocale(locale);
}
return languageCallback;
}
use of javax.security.auth.callback.LanguageCallback in project OpenAM by OpenRock.
the class RestAuthLanguageCallbackHandlerTest method shouldNotUpdateCallbackFromRequest.
@Test
public void shouldNotUpdateCallbackFromRequest() throws RestAuthResponseException, RestAuthException {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
LanguageCallback languageCallback = mock(LanguageCallback.class);
//When
boolean updated = restAuthLanguageCallbackHandler.updateCallbackFromRequest(request, response, languageCallback);
//Then
assertFalse(updated);
}
use of javax.security.auth.callback.LanguageCallback in project OpenAM by OpenRock.
the class RestAuthLanguageCallbackHandlerTest method shouldConvertToJsonWhenLocaleNotSet.
@Test
public void shouldConvertToJsonWhenLocaleNotSet() throws RestAuthException {
//Given
LanguageCallback languageCallback = new LanguageCallback();
//When
JsonValue jsonObject = restAuthLanguageCallbackHandler.convertToJson(languageCallback, 1);
//Then
assertEquals(1, jsonObject.size());
assertEquals("LanguageCallback", jsonObject.get("type").asString());
}
use of javax.security.auth.callback.LanguageCallback in project OpenAM by OpenRock.
the class RestAuthLanguageCallbackHandlerTest method shouldFailToConvertFromJsonWithInvalidType.
@Test(expectedExceptions = RestAuthException.class)
public void shouldFailToConvertFromJsonWithInvalidType() throws RestAuthException {
//Given
LanguageCallback languageCallback = new LanguageCallback();
JsonValue jsonLanguageCallback = JsonValueBuilder.jsonValue().array("input").add(JsonValueBuilder.jsonValue().put("value", "language").build()).addLast(JsonValueBuilder.jsonValue().put("value", "COUNTRY").build()).put("type", "PasswordCallback").build();
//When
restAuthLanguageCallbackHandler.convertFromJson(languageCallback, jsonLanguageCallback);
//Then
fail();
}
Aggregations