Search in sources :

Example 91 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project polymap4-core by Polymap4.

the class SpnegoProvider method getUsernamePasswordHandler.

/**
 * Used by the BASIC Auth mechanism for establishing a LoginContext
 * to authenticate a client/caller/request.
 *
 * @param username client username
 * @param password client password
 * @return CallbackHandler to be used for establishing a LoginContext
 */
public static CallbackHandler getUsernamePasswordHandler(final String username, final String password) {
    LOGGER.fine("username=" + username + "; password=" + password.hashCode());
    final CallbackHandler handler = new CallbackHandler() {

        public void handle(final Callback[] callback) {
            for (int i = 0; i < callback.length; i++) {
                if (callback[i] instanceof NameCallback) {
                    final NameCallback nameCallback = (NameCallback) callback[i];
                    nameCallback.setName(username);
                } else if (callback[i] instanceof PasswordCallback) {
                    final PasswordCallback passCallback = (PasswordCallback) callback[i];
                    passCallback.setPassword(password.toCharArray());
                } else {
                    LOGGER.warning("Unsupported Callback i=" + i + "; class=" + callback[i].getClass().getName());
                }
            }
        }
    };
    return handler;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 92 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project activemq-artemis by apache.

the class LDAPLoginModule method login.

@Override
public boolean login() throws LoginException {
    if (!authenticateUser) {
        return false;
    }
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("User name");
    callbacks[1] = new PasswordCallback("Password", false);
    try {
        handler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw (LoginException) new LoginException().initCause(e);
    }
    String password;
    username = ((NameCallback) callbacks[0]).getName();
    if (username == null)
        return false;
    if (((PasswordCallback) callbacks[1]).getPassword() != null)
        password = new String(((PasswordCallback) callbacks[1]).getPassword());
    else
        password = "";
    // authenticate will throw LoginException
    // in case of failed authentication
    authenticate(username, password);
    userAuthenticated = true;
    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) FailedLoginException(javax.security.auth.login.FailedLoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 93 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project activemq-artemis by apache.

the class PropertiesLoginModule method login.

@Override
public boolean login() throws LoginException {
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        callbackHandler.handle(callbacks);
    } catch (IOException ioe) {
        throw new LoginException(ioe.getMessage());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException(uce.getMessage() + " not available to obtain information from user");
    }
    user = ((NameCallback) callbacks[0]).getName();
    char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
    if (tmpPassword == null) {
        tmpPassword = new char[0];
    }
    if (user == null) {
        throw new FailedLoginException("User is null");
    }
    String password = users.getProperty(user);
    if (password == null) {
        throw new FailedLoginException("User does not exist: " + user);
    }
    try {
        hashProcessor = PasswordMaskingUtil.getHashProcessor(password);
    } catch (Exception e) {
        throw new FailedLoginException("Failed to get hash processor");
    }
    if (!hashProcessor.compare(tmpPassword, password)) {
        throw new FailedLoginException("Password does not match for user: " + user);
    }
    loginSucceeded = true;
    if (debug) {
        logger.debug("login " + user);
    }
    return loginSucceeded;
}
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) FailedLoginException(javax.security.auth.login.FailedLoginException) 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) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) FailedLoginException(javax.security.auth.login.FailedLoginException)

Example 94 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project activemq-artemis by apache.

the class JaasCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof PasswordCallback) {
            PasswordCallback passwordCallback = (PasswordCallback) callback;
            if (password == null) {
                passwordCallback.setPassword(null);
            } else {
                passwordCallback.setPassword(password.toCharArray());
            }
        } else if (callback instanceof NameCallback) {
            NameCallback nameCallback = (NameCallback) callback;
            if (username == null) {
                nameCallback.setName(null);
            } else {
                nameCallback.setName(username);
            }
        } else if (callback instanceof CertificateCallback) {
            CertificateCallback certCallback = (CertificateCallback) callback;
            certCallback.setCertificates(getCertsFromConnection(remotingConnection));
        } else if (callback instanceof Krb5Callback) {
            Krb5Callback krb5Callback = (Krb5Callback) callback;
            Subject peerSubject = remotingConnection.getSubject();
            if (peerSubject != null) {
                for (Principal principal : peerSubject.getPrivateCredentials(KerberosPrincipal.class)) {
                    krb5Callback.setPeerPrincipal(principal);
                    return;
                }
            }
            krb5Callback.setPeerPrincipal(getPeerPrincipalFromConnection(remotingConnection));
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) 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) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Subject(javax.security.auth.Subject) Principal(java.security.Principal) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal)

Example 95 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project atlas by apache.

the class SSLAndKerberosTest method loginTestUser.

protected Subject loginTestUser() throws LoginException, IOException {
    LoginContext lc = new LoginContext(TEST_USER_JAAS_SECTION, new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof PasswordCallback) {
                    PasswordCallback passwordCallback = (PasswordCallback) callback;
                    passwordCallback.setPassword(TESTPASS.toCharArray());
                }
                if (callback instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) callback;
                    nameCallback.setName(TESTUSER);
                }
            }
        }
    });
    // attempt authentication
    lc.login();
    return lc.getSubject();
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) 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) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

PasswordCallback (javax.security.auth.callback.PasswordCallback)316 NameCallback (javax.security.auth.callback.NameCallback)255 Callback (javax.security.auth.callback.Callback)207 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)167 IOException (java.io.IOException)102 LoginException (javax.security.auth.login.LoginException)72 CallbackHandler (javax.security.auth.callback.CallbackHandler)66 LoginContext (javax.security.auth.login.LoginContext)39 FailedLoginException (javax.security.auth.login.FailedLoginException)35 RealmCallback (javax.security.sasl.RealmCallback)35 Subject (javax.security.auth.Subject)31 Test (org.junit.Test)28 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)26 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)26 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)24 Principal (java.security.Principal)21 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)18 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)18 HashMap (java.util.HashMap)16 Test (org.testng.annotations.Test)15