Search in sources :

Example 1 with TextInputCallback

use of javax.security.auth.callback.TextInputCallback in project tomcat by apache.

the class JAASMemoryLoginModule method getCatalinaBase.

private String getCatalinaBase() {
    if (callbackHandler == null) {
        return null;
    }
    Callback[] callbacks = new Callback[1];
    callbacks[0] = new TextInputCallback("catalinaBase");
    String result = null;
    try {
        callbackHandler.handle(callbacks);
        result = ((TextInputCallback) callbacks[0]).getText();
    } catch (IOException | UnsupportedCallbackException e) {
        return null;
    }
    return result;
}
Also used : TextInputCallback(javax.security.auth.callback.TextInputCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TextInputCallback(javax.security.auth.callback.TextInputCallback)

Example 2 with TextInputCallback

use of javax.security.auth.callback.TextInputCallback in project spring-security by spring-projects.

the class JaasApiIntegrationFilterTests method onBeforeTests.

// ~ Methods
// ========================================================================================================
@Before
public void onBeforeTests() throws Exception {
    this.filter = new JaasApiIntegrationFilter();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    authenticatedSubject = new Subject();
    authenticatedSubject.getPrincipals().add(new Principal() {

        public String getName() {
            return "principal";
        }
    });
    authenticatedSubject.getPrivateCredentials().add("password");
    authenticatedSubject.getPublicCredentials().add("username");
    callbackHandler = new CallbackHandler() {

        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof NameCallback) {
                    ((NameCallback) callback).setName("user");
                } else if (callback instanceof PasswordCallback) {
                    ((PasswordCallback) callback).setPassword("password".toCharArray());
                } else if (callback instanceof TextInputCallback) {
                // ignore
                } else {
                    throw new UnsupportedCallbackException(callback, "Unrecognized Callback " + callback);
                }
            }
        }
    };
    testConfiguration = new Configuration() {

        public void refresh() {
        }

        public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
            return new AppConfigurationEntry[] { new AppConfigurationEntry(TestLoginModule.class.getName(), LoginModuleControlFlag.REQUIRED, new HashMap<String, String>()) };
        }
    };
    LoginContext ctx = new LoginContext("SubjectDoAsFilterTest", authenticatedSubject, callbackHandler, testConfiguration);
    ctx.login();
    token = new JaasAuthenticationToken("username", "password", AuthorityUtils.createAuthorityList("ROLE_ADMIN"), ctx);
    // just in case someone forgot to clear the context
    SecurityContextHolder.clearContext();
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) Configuration(javax.security.auth.login.Configuration) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) IOException(java.io.IOException) Subject(javax.security.auth.Subject) TextInputCallback(javax.security.auth.callback.TextInputCallback) AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) TextInputCallback(javax.security.auth.callback.TextInputCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) LoginContext(javax.security.auth.login.LoginContext) JaasAuthenticationToken(org.springframework.security.authentication.jaas.JaasAuthenticationToken) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Principal(java.security.Principal) Before(org.junit.Before)

Example 3 with TextInputCallback

use of javax.security.auth.callback.TextInputCallback 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 4 with TextInputCallback

use of javax.security.auth.callback.TextInputCallback in project OpenAM by OpenRock.

the class RestAuthTextInputCallbackHandlerTest method shouldNotUpdateCallbackFromRequest.

@Test
public void shouldNotUpdateCallbackFromRequest() throws RestAuthResponseException, RestAuthException {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    TextInputCallback textInputCallback = mock(TextInputCallback.class);
    //When
    boolean updated = restAuthTextInputCallbackHandler.updateCallbackFromRequest(request, response, textInputCallback);
    //Then
    assertFalse(updated);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) TextInputCallback(javax.security.auth.callback.TextInputCallback) Test(org.testng.annotations.Test)

Example 5 with TextInputCallback

use of javax.security.auth.callback.TextInputCallback in project OpenAM by OpenRock.

the class RestAuthTextInputCallbackHandlerTest method shouldNotFailToConvertFromJsonWithTypeLowerCase.

@Test
public void shouldNotFailToConvertFromJsonWithTypeLowerCase() throws RestAuthException {
    //Given
    TextInputCallback textInputCallback = new TextInputCallback("Enter text:", "DEFAULT_VALUE");
    JsonValue jsonTextInputCallback = JsonValueBuilder.jsonValue().array("input").addLast(JsonValueBuilder.jsonValue().put("value", "TEXT_VALUE").build()).array("output").add(JsonValueBuilder.jsonValue().put("value", "Enter text:").build()).addLast(JsonValueBuilder.jsonValue().put("value", "DEFAULT_VALUE").build()).put("type", "tExtinpuTcallback").build();
    //When
    TextInputCallback convertedTextInputCallback = restAuthTextInputCallbackHandler.convertFromJson(textInputCallback, jsonTextInputCallback);
    //Then
    assertEquals(textInputCallback, convertedTextInputCallback);
    assertEquals("Enter text:", convertedTextInputCallback.getPrompt());
    assertEquals("DEFAULT_VALUE", convertedTextInputCallback.getDefaultText());
    assertEquals("TEXT_VALUE", convertedTextInputCallback.getText());
}
Also used : JsonValue(org.forgerock.json.JsonValue) TextInputCallback(javax.security.auth.callback.TextInputCallback) Test(org.testng.annotations.Test)

Aggregations

TextInputCallback (javax.security.auth.callback.TextInputCallback)15 NameCallback (javax.security.auth.callback.NameCallback)7 PasswordCallback (javax.security.auth.callback.PasswordCallback)7 Test (org.testng.annotations.Test)6 Callback (javax.security.auth.callback.Callback)5 JsonValue (org.forgerock.json.JsonValue)5 IOException (java.io.IOException)4 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)4 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)4 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)4 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)4 HiddenValueCallback (com.sun.identity.authentication.callbacks.HiddenValueCallback)3 ScriptTextOutputCallback (com.sun.identity.authentication.callbacks.ScriptTextOutputCallback)3 LanguageCallback (javax.security.auth.callback.LanguageCallback)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 FailedLoginException (javax.security.auth.login.FailedLoginException)2 LoginException (javax.security.auth.login.LoginException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2