Search in sources :

Example 71 with NameCallback

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

the class RealmDirectLoginModule method getUsersPassword.

/**
     * @see org.jboss.security.auth.spi.UsernamePasswordLoginModule#getUsersPassword()
     */
@Override
protected String getUsersPassword() throws LoginException {
    if (validationMode == ValidationMode.VALIDATION) {
        return null;
    }
    RealmCallback rcb = new RealmCallback("Realm", securityRealm.getName());
    NameCallback ncb = new NameCallback("User Name", getUsername());
    String password = null;
    switch(validationMode) {
        case DIGEST:
            CredentialCallback cc = new CredentialCallback(PasswordCredential.class, ALGORITHM_DIGEST_MD5);
            handle(new Callback[] { rcb, ncb, cc });
            PasswordCredential passwordCredential = (PasswordCredential) cc.getCredential();
            DigestPassword digestPassword = passwordCredential.getPassword(DigestPassword.class);
            password = ByteIterator.ofBytes(digestPassword.getDigest()).hexEncode().drainToString();
            break;
        case PASSWORD:
            PasswordCallback pcb = new PasswordCallback("Password", false);
            handle(new Callback[] { rcb, ncb, pcb });
            password = String.valueOf(pcb.getPassword());
            break;
    }
    return password;
}
Also used : DigestPassword(org.wildfly.security.password.interfaces.DigestPassword) NameCallback(javax.security.auth.callback.NameCallback) PasswordCredential(org.wildfly.security.credential.PasswordCredential) PasswordCallback(javax.security.auth.callback.PasswordCallback) CredentialCallback(org.wildfly.security.auth.callback.CredentialCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 72 with NameCallback

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

the class RealmDirectLoginModule method validatePassword.

@Override
protected boolean validatePassword(String inputPassword, String expectedPassword) {
    if (digestCredential != null) {
        return digestCredential.verifyHA1(expectedPassword.getBytes(UTF_8));
    }
    switch(validationMode) {
        case DIGEST:
            String inputHashed = hashUtil.generateHashedHexURP(getUsername(), securityRealm.getName(), inputPassword.toCharArray());
            return expectedPassword.equals(inputHashed);
        case PASSWORD:
            return expectedPassword.equals(inputPassword);
        case VALIDATION:
            RealmCallback rcb = new RealmCallback("Realm", securityRealm.getName());
            NameCallback ncb = new NameCallback("User Name", getUsername());
            EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(inputPassword.toCharArray()));
            try {
                handle(new Callback[] { rcb, ncb, evc });
                return evc.isVerified();
            } catch (LoginException e) {
                return false;
            }
        default:
            return false;
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordGuessEvidence(org.wildfly.security.evidence.PasswordGuessEvidence) LoginException(javax.security.auth.login.LoginException) EvidenceVerifyCallback(org.wildfly.security.auth.callback.EvidenceVerifyCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 73 with NameCallback

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

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

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

the class GuestDelegationLoginModule method login.

// Public methods --------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
public boolean login() throws LoginException {
    if (super.login() == true) {
        log.debug("super.login()==true");
        return true;
    }
    // Time to see if this is a delegation request.
    NameCallback ncb = new NameCallback("Username:");
    ObjectCallback ocb = new ObjectCallback("Password:");
    try {
        callbackHandler.handle(new Callback[] { ncb, ocb });
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        // If the CallbackHandler can not handle the required callbacks then no chance.
        return false;
    }
    String name = ncb.getName();
    Object credential = ocb.getCredential();
    if (credential instanceof CurrentUserCredential) {
        // This credential type will only be seen for a delegation request, if not seen then the request is not for us.
        final CurrentUserCredential cuCredential = (CurrentUserCredential) credential;
        // only the "guest" can be switched to another identity
        if ("guest".equals(cuCredential.getUser())) {
            identity = new SimplePrincipal(name);
            if (getUseFirstPass()) {
                String userName = identity.getName();
                if (log.isDebugEnabled())
                    log.debug("Storing username '" + userName + "' and empty password");
                // Add the username and an empty password to the shared state map
                sharedState.put("javax.security.auth.login.name", identity);
                sharedState.put("javax.security.auth.login.password", "");
            }
            loginOk = true;
            return true;
        }
    }
    // Attempted login but not successful.
    return false;
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) ObjectCallback(org.jboss.security.auth.callback.ObjectCallback) LoginException(javax.security.auth.login.LoginException) SimplePrincipal(org.jboss.security.SimplePrincipal)

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