Search in sources :

Example 91 with NameCallback

use of javax.security.auth.callback.NameCallback in project tomee by apache.

the class ServiceProviderLoginModule method getUserData.

private UserData getUserData() throws LoginException {
    final Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        this.callbackHandler.handle(callbacks);
    } catch (final IOException ioe) {
        throw new LoginException(ioe.getMessage());
    } catch (final UnsupportedCallbackException uce) {
        throw new LoginException(uce.getMessage() + " not available to obtain information from user");
    }
    final String user = ((NameCallback) callbacks[0]).getName();
    char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
    if (tmpPassword == null) {
        tmpPassword = new char[0];
    }
    final String password = new String(tmpPassword);
    return new UserData(user, password);
}
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) FailedLoginException(javax.security.auth.login.FailedLoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 92 with NameCallback

use of javax.security.auth.callback.NameCallback in project cas by apereo.

the class MockLoginModule method login.

@Override
public boolean login() throws LoginException {
    final Callback[] callbacks = new Callback[] { new NameCallback("f"), new PasswordCallback("f", false) };
    try {
        this.callbackHandler.handle(callbacks);
    } catch (final Exception e) {
        throw new LoginException();
    }
    final String userName = ((NameCallback) callbacks[0]).getName();
    final String password = new String(((PasswordCallback) callbacks[1]).getPassword());
    if ("test".equals(userName) && "test".equals(password)) {
        this.subject.getPrincipals().add(new BasicUserPrincipal(userName));
        return true;
    }
    throw new LoginException();
}
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) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) LoginException(javax.security.auth.login.LoginException)

Example 93 with NameCallback

use of javax.security.auth.callback.NameCallback in project uPortal by Jasig.

the class JAASInlineCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
            NameCallback nc = (NameCallback) callbacks[i];
            nc.setName(username);
        } else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback pc = (PasswordCallback) callbacks[i];
            pc.setPassword(password);
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 94 with NameCallback

use of javax.security.auth.callback.NameCallback in project Payara by payara.

the class AuthenticationServiceImpl method setupPasswordCredential.

/**
 * A PasswordCredential object is needed when using the existing Realm LoginModules.
 *
 * Unless the CallbackHandler is from the AuthenticationService obtain the name
 * and password from the supplied JAAS CallbackHandler directly. Establishing the
 * PasswordCredential in the Subject is determined by service configuration.
 *
 * @throws LoginException when unable to obtain data from the CallbackHandler
 */
private void setupPasswordCredential(Subject subject, CallbackHandler callbackHandler) throws LoginException {
    String username = null;
    char[] password = null;
    // Obtain the username and password for the PasswordCredential
    if (callbackHandler instanceof AuthenticationCallbackHandler) {
        username = ((AuthenticationCallbackHandler) callbackHandler).getUsername();
        password = ((AuthenticationCallbackHandler) callbackHandler).getPassword();
    } else {
        // Use the supplied callback handler to obtain the PasswordCredential information
        // TODO - How does this impact Audit ability to get name?
        Callback[] callbacks = new Callback[2];
        callbacks[0] = new NameCallback("username: ");
        callbacks[1] = new PasswordCallback("password: ", false);
        try {
            callbackHandler.handle(callbacks);
            username = ((NameCallback) callbacks[0]).getName();
            password = ((PasswordCallback) callbacks[1]).getPassword();
        } catch (IOException ioe) {
            throw (LoginException) new LoginException("AuthenticationService unable to create PasswordCredential: " + ioe.getMessage()).initCause(ioe);
        } catch (UnsupportedCallbackException uce) {
            throw (LoginException) new LoginException("AuthenticationService unable to create PasswordCredential: " + uce.getMessage()).initCause(uce);
        }
    }
    // Add the PasswordCredential to the Subject
    final Subject s = subject;
    final PasswordCredential pc = new PasswordCredential(username, password, realmName);
    AppservAccessController.doPrivileged(new PrivilegedAction<Object>() {

        public Object run() {
            s.getPrivateCredentials().add(pc);
            return null;
        }
    });
}
Also used : PasswordCredential(com.sun.enterprise.security.auth.login.common.PasswordCredential) IOException(java.io.IOException) Subject(javax.security.auth.Subject) 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) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 95 with NameCallback

use of javax.security.auth.callback.NameCallback in project camel by apache.

the class MyLoginModule method login.

@Override
public boolean login() throws LoginException {
    // get username and password
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("username");
    callbacks[1] = new PasswordCallback("password", false);
    try {
        callbackHandler.handle(callbacks);
        String username = ((NameCallback) callbacks[0]).getName();
        char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
        String password = new String(tmpPassword);
        ((PasswordCallback) callbacks[1]).clearPassword();
        // as this is just for testing purpose
        if (!"secret".equals(password)) {
            throw new LoginException("Login denied");
        }
        // add roles
        if ("scott".equals(username)) {
            subject.getPrincipals().add(new MyRolePrincipal("admin"));
            subject.getPrincipals().add(new MyRolePrincipal("guest"));
        } else if ("guest".equals(username)) {
            subject.getPrincipals().add(new MyRolePrincipal("guest"));
        }
    } catch (IOException ioe) {
        LoginException le = new LoginException(ioe.toString());
        le.initCause(ioe);
        throw le;
    } catch (UnsupportedCallbackException uce) {
        LoginException le = new LoginException("Error: " + uce.getCallback().toString() + " not available to gather authentication information from the user");
        le.initCause(uce);
        throw le;
    }
    return true;
}
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) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

NameCallback (javax.security.auth.callback.NameCallback)203 PasswordCallback (javax.security.auth.callback.PasswordCallback)161 Callback (javax.security.auth.callback.Callback)140 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)101 IOException (java.io.IOException)60 LoginException (javax.security.auth.login.LoginException)51 CallbackHandler (javax.security.auth.callback.CallbackHandler)27 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)22 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)22 RealmCallback (javax.security.sasl.RealmCallback)22 Subject (javax.security.auth.Subject)19 FailedLoginException (javax.security.auth.login.FailedLoginException)19 LoginContext (javax.security.auth.login.LoginContext)18 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)18 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)17 Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 IdRepoException (com.sun.identity.idm.IdRepoException)13 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)11