Search in sources :

Example 16 with NameCallback

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

use of javax.security.auth.callback.NameCallback in project jdk8u_jdk by JetBrains.

the class TestSampleLoginModule method login.

/*
   * Authenticate the user by comparing the values of the java properties
   * (username and password) against the values of the credentials.
   * */
public boolean login() throws LoginException {
    String credentials_username = null;
    String credentials_password = null;
    String authenticated_username = System.getProperty("susername");
    String authenticated_password = System.getProperty("spassword");
    System.out.println("TestSampleLoginModule::login: Start");
    // First retreive the credentials {username, password} from
    // the callback handler
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("username");
    callbacks[1] = new PasswordCallback("password", false);
    try {
        callbackHandler.handle(callbacks);
        credentials_username = ((NameCallback) callbacks[0]).getName();
        credentials_password = new String(((PasswordCallback) callbacks[1]).getPassword());
    } catch (Exception e) {
        throw new LoginException(e.toString());
    }
    System.out.println("TestSampleLoginModule::login: credentials username = " + credentials_username);
    System.out.println("TestSampleLoginModule::login: credentials password = " + credentials_password);
    System.out.println("TestSampleLoginModule::login: authenticated username = " + authenticated_username);
    System.out.println("TestSampleLoginModule::login: authenticated password = " + authenticated_password);
    if (credentials_username.equals(authenticated_username) && credentials_password.equals(authenticated_password)) {
        System.out.println("TestSampleLoginModule::login: " + "Authentication should succeed");
        return true;
    } else {
        System.out.println("TestSampleLoginModule::login: " + "Authentication should reject");
        throw new LoginException("TestSampleLoginModule throws EXCEPTION");
    }
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) LoginException(javax.security.auth.login.LoginException)

Example 18 with NameCallback

use of javax.security.auth.callback.NameCallback in project opennms by OpenNMS.

the class LoginModuleUtils method doLogin.

public static boolean doLogin(final OpenNMSLoginHandler handler, final Subject subject, final Map<String, ?> sharedState, final Map<String, ?> options) throws LoginException {
    LOG.debug("OpenNMSLoginModule: login(): handler={}, subject={}, sharedState={}, options={}", handler.getClass(), subject.getClass(), sharedState, options);
    final Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        handler.callbackHandler().handle(callbacks);
    } catch (final IOException ioe) {
        LOG.debug("IO exception while attempting to prompt for username and password.", ioe);
        throw new LoginException(ioe.getMessage());
    } catch (final UnsupportedCallbackException uce) {
        LOG.debug("Username or password prompt not supported.", uce);
        throw new LoginException(uce.getMessage() + " not available to obtain information from user.");
    }
    final String user = ((NameCallback) callbacks[0]).getName();
    handler.setUser(user);
    if (user == null) {
        final String msg = "Username can not be null.";
        LOG.debug(msg);
        throw new LoginException(msg);
    }
    // password callback get value
    if (((PasswordCallback) callbacks[1]).getPassword() == null) {
        final String msg = "Password can not be null.";
        LOG.debug(msg);
        throw new LoginException(msg);
    }
    final String password = new String(((PasswordCallback) callbacks[1]).getPassword());
    final User configUser;
    final SpringSecurityUser onmsUser;
    try {
        configUser = handler.userConfig().getUser(user);
        onmsUser = handler.springSecurityUserDao().getByUsername(user);
    } catch (final Exception e) {
        final String message = "Failed to retrieve user " + user + " from OpenNMS UserConfig.";
        LOG.debug(message, e);
        throw new LoginException(message);
    }
    if (configUser == null) {
        final String msg = "User  " + user + " does not exist.";
        LOG.debug(msg);
        throw new FailedLoginException(msg);
    }
    if (!handler.userConfig().comparePasswords(user, password)) {
        final String msg = "Login failed: passwords did not match.";
        LOG.debug(msg);
        throw new FailedLoginException(msg);
    }
    ;
    boolean allowed = true;
    final Set<Principal> principals = LoginModuleUtils.createPrincipals(handler, onmsUser.getAuthorities());
    handler.setPrincipals(principals);
    if (handler.requiresAdminRole()) {
        allowed = false;
        for (final Principal principal : principals) {
            final String name = principal.getName().toLowerCase().replaceAll("^role_", "");
            if ("admin".equals(name)) {
                allowed = true;
            }
        }
    }
    if (!allowed) {
        final String msg = "User " + user + " is not an administrator!  OSGi console access is forbidden.";
        LOG.debug(msg);
        throw new LoginException(msg);
    }
    LOG.debug("Successfully logged in {}.", user);
    return true;
}
Also used : User(org.opennms.netmgt.config.users.User) IOException(java.io.IOException) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 19 with NameCallback

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

the class DevicePrintAuthenticationServiceTest method shouldLoginSuccessfullyWhenValidMatchingStoredDevicePrintProfilesFound.

/**
     * 3) first call ISAuthConstants.LOGIN_START - device print attr populated, with a valid stored profile - should return ISAuthConstants.LOGIN_SUCCEED
     */
@Test
public void shouldLoginSuccessfullyWhenValidMatchingStoredDevicePrintProfilesFound() throws AuthLoginException {
    //Given
    Callback[] callbacks = new Callback[1];
    NameCallback devicePrintCallback = mock(NameCallback.class);
    int state = ISAuthConstants.LOGIN_START;
    DevicePrint devicePrint = mock(DevicePrint.class);
    UserProfile validStoredMatchingProfile = mock(UserProfile.class);
    UserProfile selectedUserProfile = validStoredMatchingProfile;
    callbacks[0] = devicePrintCallback;
    given(devicePrintCallback.getName()).willReturn("DEVICE_PRINT_INFO");
    given(devicePrintService.getDevicePrint(request)).willReturn(devicePrint);
    given(devicePrintService.hasRequiredAttributes(devicePrint)).willReturn(true);
    given(devicePrintService.getBestMatchingUserProfile(devicePrint)).willReturn(selectedUserProfile);
    //When
    int nextState = devicePrintAuthenticationService.process(callbacks, state);
    //Then
    assertEquals(nextState, ISAuthConstants.LOGIN_SUCCEED);
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) UserProfile(org.forgerock.openam.authentication.modules.deviceprint.model.UserProfile) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) Test(org.testng.annotations.Test)

Example 20 with NameCallback

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

the class DevicePrintAuthenticationServiceTest method shouldSendOTPWhenDevicePrintInfoNotSufficient.

/*

    1) first call ISAuthConstants.LOGIN_START - device print attr populated, device print info not sufficient - should return ISAuthConstants.LOGIN_SUCCEED
    2) first call ISAuthConstants.LOGIN_START - device print attr populated, with invalid stored profiles using OTP - should return 2
    3) first call ISAuthConstants.LOGIN_START - device print attr populated, with a valid stored profile - should return ISAuthConstants.LOGIN_SUCCEED

    4) second call, using OPT, 2 - request OPT to be sent - should return 2
    5) third call, using OPT, 2 - OPT code submitted, with correct code - should return 3
    6) third call, using OPT, 2 - OPT code submitted, with incorrect code - should throw exception

    7) fourth call, 3 - don't save profile - should return ISAuthConstants.LOGIN_SUCCEED, with no profile saved
    8) fourth call, 3 - save profile, having no valid previous profiles - should create new profile, return ISAuthConstants.LOGIN_SUCCEED
    9) fourth call, 3 - save profile, having a valid previous profile - should update previous profile, return ISAuthConstants.LOGIN_SUCCEED

     */
/**
     * 1) first call ISAuthConstants.LOGIN_START - device print attr populated, device print info not sufficient - should return 2 (SEND_OPT)
     */
@Test
public void shouldSendOTPWhenDevicePrintInfoNotSufficient() throws AuthLoginException {
    //Given
    Callback[] callbacks = new Callback[1];
    NameCallback devicePrintCallback = mock(NameCallback.class);
    int state = ISAuthConstants.LOGIN_START;
    DevicePrint devicePrint = mock(DevicePrint.class);
    callbacks[0] = devicePrintCallback;
    given(devicePrintCallback.getName()).willReturn("DEVICE_PRINT_INFO");
    given(devicePrintService.getDevicePrint(request)).willReturn(devicePrint);
    given(devicePrintService.hasRequiredAttributes(devicePrint)).willReturn(false);
    //When
    int nextState = devicePrintAuthenticationService.process(callbacks, state);
    //Then
    assertEquals(nextState, 2);
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) Test(org.testng.annotations.Test)

Aggregations

NameCallback (javax.security.auth.callback.NameCallback)185 PasswordCallback (javax.security.auth.callback.PasswordCallback)145 Callback (javax.security.auth.callback.Callback)126 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)89 IOException (java.io.IOException)53 LoginException (javax.security.auth.login.LoginException)46 CallbackHandler (javax.security.auth.callback.CallbackHandler)22 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)21 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)20 RealmCallback (javax.security.sasl.RealmCallback)20 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)17 FailedLoginException (javax.security.auth.login.FailedLoginException)17 Subject (javax.security.auth.Subject)16 LoginContext (javax.security.auth.login.LoginContext)15 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)15 Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 IdRepoException (com.sun.identity.idm.IdRepoException)13 SaslException (javax.security.sasl.SaslException)11