Search in sources :

Example 1 with LanguageCallback

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;
}
Also used : ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) IOException(java.io.IOException) LanguageCallback(javax.security.auth.callback.LanguageCallback) TextInputCallback(javax.security.auth.callback.TextInputCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) TextInputCallback(javax.security.auth.callback.TextInputCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) LanguageCallback(javax.security.auth.callback.LanguageCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) FailedLoginException(javax.security.auth.login.FailedLoginException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 2 with LanguageCallback

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;
}
Also used : Node(org.w3c.dom.Node) LanguageCallback(javax.security.auth.callback.LanguageCallback)

Example 3 with 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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) LanguageCallback(javax.security.auth.callback.LanguageCallback) Test(org.testng.annotations.Test)

Example 4 with LanguageCallback

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());
}
Also used : JsonValue(org.forgerock.json.JsonValue) LanguageCallback(javax.security.auth.callback.LanguageCallback) Test(org.testng.annotations.Test)

Example 5 with LanguageCallback

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();
}
Also used : JsonValue(org.forgerock.json.JsonValue) LanguageCallback(javax.security.auth.callback.LanguageCallback) Test(org.testng.annotations.Test)

Aggregations

LanguageCallback (javax.security.auth.callback.LanguageCallback)11 Test (org.testng.annotations.Test)7 JsonValue (org.forgerock.json.JsonValue)6 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)3 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)3 NameCallback (javax.security.auth.callback.NameCallback)3 PasswordCallback (javax.security.auth.callback.PasswordCallback)3 TextInputCallback (javax.security.auth.callback.TextInputCallback)3 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)3 HiddenValueCallback (com.sun.identity.authentication.callbacks.HiddenValueCallback)2 ScriptTextOutputCallback (com.sun.identity.authentication.callbacks.ScriptTextOutputCallback)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Node (org.w3c.dom.Node)2 DSAMECallbackInterface (com.sun.identity.authentication.spi.DSAMECallbackInterface)1 HttpCallback (com.sun.identity.authentication.spi.HttpCallback)1 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)1 RedirectCallback (com.sun.identity.authentication.spi.RedirectCallback)1 X509CertificateCallback (com.sun.identity.authentication.spi.X509CertificateCallback)1 IOException (java.io.IOException)1