Search in sources :

Example 46 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project wildfly by wildfly.

the class CustomEjbAccessingLoginModule method getUsernameAndPassword.

protected void getUsernameAndPassword() throws LoginException {
    // prompt for a username and password
    if (callbackHandler == null) {
        throw new LoginException("Error: no CallbackHandler available " + "to collect authentication information");
    }
    NameCallback nc = new NameCallback("User name: ", "guest");
    PasswordCallback pc = new PasswordCallback("Password: ", false);
    Callback[] callbacks = { nc, pc };
    try {
        callbackHandler.handle(callbacks);
        username = nc.getName();
        char[] tmpPassword = pc.getPassword();
        if (tmpPassword != null) {
            pc.clearPassword();
            password = new String(tmpPassword);
        }
    } catch (IOException e) {
        LoginException le = new LoginException("Failed to get username/password");
        le.initCause(e);
        throw le;
    } catch (UnsupportedCallbackException e) {
        LoginException le = new LoginException("CallbackHandler does not support: " + e.getCallback());
        le.initCause(e);
        throw le;
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) LoginException(javax.security.auth.login.LoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 47 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project wildfly by wildfly.

the class CustomTestLoginModule method getUsernameAndPassword.

protected String[] getUsernameAndPassword() throws LoginException {
    String[] info = { null, null };
    // prompt for a username and password
    if (callbackHandler == null) {
        throw new LoginException("Error: no CallbackHandler available " + "to collect authentication information");
    }
    NameCallback nc = new NameCallback("User name: ", "guest");
    PasswordCallback pc = new PasswordCallback("Password: ", false);
    Callback[] callbacks = { nc, pc };
    String username = null;
    String password = null;
    try {
        callbackHandler.handle(callbacks);
        username = nc.getName();
        char[] tmpPassword = pc.getPassword();
        if (tmpPassword != null) {
            pc.clearPassword();
            password = new String(tmpPassword);
        }
    } catch (IOException e) {
        LoginException le = new LoginException("Failed to get username/password");
        le.initCause(e);
        throw le;
    } catch (UnsupportedCallbackException e) {
        LoginException le = new LoginException("CallbackHandler does not support: " + e.getCallback());
        le.initCause(e);
        throw le;
    }
    info[0] = username;
    info[1] = password;
    return info;
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) LoginException(javax.security.auth.login.LoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 48 with PasswordCallback

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

the class JaasPasswordCallbackHandler method handle.

// ~ Methods
// ========================================================================================================
/**
	 * If the callback passed to the 'handle' method is an instance of PasswordCallback,
	 * the JaasPasswordCallbackHandler will call,
	 * callback.setPassword(authentication.getCredentials().toString()).
	 *
	 * @param callback
	 * @param auth
	 *
	 * @throws IOException
	 * @throws UnsupportedCallbackException
	 */
public void handle(Callback callback, Authentication auth) throws IOException, UnsupportedCallbackException {
    if (callback instanceof PasswordCallback) {
        PasswordCallback pc = (PasswordCallback) callback;
        pc.setPassword(auth.getCredentials().toString().toCharArray());
    }
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 49 with PasswordCallback

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

the class UsernameEqualsPasswordLoginModule method initialize.

public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
    this.subject = subject;
    try {
        NameCallback nameCallback = new NameCallback("prompt");
        PasswordCallback passwordCallback = new PasswordCallback("prompt", false);
        callbackHandler.handle(new Callback[] { nameCallback, passwordCallback });
        password = new String(passwordCallback.getPassword());
        username = nameCallback.getName();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException)

Example 50 with PasswordCallback

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

the class SampleConfigLoginModule method login.

@Override
public boolean login() throws LoginException {
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Name");
    callbacks[1] = new PasswordCallback("Password", false);
    try {
        handler.handle(callbacks);
    } catch (IOException e) {
        throw new LoginException(e.getMessage());
    } catch (UnsupportedCallbackException e) {
        throw new LoginException(e.getMessage());
    }
    String name = ((NameCallback) callbacks[0]).getName();
    char[] password = ((PasswordCallback) callbacks[1]).getPassword();
    boolean result = Arrays.equals(name.toCharArray(), password);
    succeeded = result;
    this.name = name;
    return result;
}
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

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