Search in sources :

Example 81 with NameCallback

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

the class DigestMD5Client method processChallenge.

/**
    * Record information from the challengeVal array into variables/fields.
    * Check directive values that are multi-valued and ensure that mandatory
    * directives not missing from the digest-challenge.
    *
    * @throws SaslException if a sasl is a the mechanism cannot
    * correcly handle a callbacks or if a violation in the
    * digest challenge format is detected.
    */
private void processChallenge(byte[][] challengeVal, List<byte[]> realmChoices) throws SaslException, UnsupportedEncodingException {
    /* CHARSET: optional atmost once */
    if (challengeVal[CHARSET] != null) {
        if (!"utf-8".equals(new String(challengeVal[CHARSET], encoding))) {
            throw new SaslException("DIGEST-MD5: digest-challenge format " + "violation. Unrecognised charset value: " + new String(challengeVal[CHARSET]));
        } else {
            encoding = "UTF8";
            useUTF8 = true;
        }
    }
    /* ALGORITHM: required exactly once */
    if (challengeVal[ALGORITHM] == null) {
        throw new SaslException("DIGEST-MD5: Digest-challenge format " + "violation: algorithm directive missing");
    } else if (!"md5-sess".equals(new String(challengeVal[ALGORITHM], encoding))) {
        throw new SaslException("DIGEST-MD5: Digest-challenge format " + "violation. Invalid value for 'algorithm' directive: " + challengeVal[ALGORITHM]);
    }
    /* NONCE: required exactly once */
    if (challengeVal[NONCE] == null) {
        throw new SaslException("DIGEST-MD5: Digest-challenge format " + "violation: nonce directive missing");
    } else {
        nonce = challengeVal[NONCE];
    }
    try {
        /* REALM: optional, if multiple, stored in realmChoices */
        String[] realmTokens = null;
        if (challengeVal[REALM] != null) {
            if (realmChoices == null || realmChoices.size() <= 1) {
                // Only one realm specified
                negotiatedRealm = new String(challengeVal[REALM], encoding);
            } else {
                realmTokens = new String[realmChoices.size()];
                for (int i = 0; i < realmTokens.length; i++) {
                    realmTokens[i] = new String(realmChoices.get(i), encoding);
                }
            }
        }
        NameCallback ncb = authzid == null ? new NameCallback("DIGEST-MD5 authentication ID: ") : new NameCallback("DIGEST-MD5 authentication ID: ", authzid);
        PasswordCallback pcb = new PasswordCallback("DIGEST-MD5 password: ", false);
        if (realmTokens == null) {
            // Server specified <= 1 realm
            // If 0, RFC 2831: the client SHOULD solicit a realm from the user.
            RealmCallback tcb = (negotiatedRealm == null ? new RealmCallback("DIGEST-MD5 realm: ") : new RealmCallback("DIGEST-MD5 realm: ", negotiatedRealm));
            cbh.handle(new Callback[] { tcb, ncb, pcb });
            /* Acquire realm from RealmCallback */
            negotiatedRealm = tcb.getText();
            if (negotiatedRealm == null) {
                negotiatedRealm = "";
            }
        } else {
            RealmChoiceCallback ccb = new RealmChoiceCallback("DIGEST-MD5 realm: ", realmTokens, 0, false);
            cbh.handle(new Callback[] { ccb, ncb, pcb });
            // Acquire realm from RealmChoiceCallback
            int[] selected = ccb.getSelectedIndexes();
            if (selected == null || selected[0] < 0 || selected[0] >= realmTokens.length) {
                throw new SaslException("DIGEST-MD5: Invalid realm chosen");
            }
            negotiatedRealm = realmTokens[selected[0]];
        }
        passwd = pcb.getPassword();
        pcb.clearPassword();
        username = ncb.getName();
    } catch (SaslException se) {
        throw se;
    } catch (UnsupportedCallbackException e) {
        throw new SaslException("DIGEST-MD5: Cannot perform callback to " + "acquire realm, authentication ID or password", e);
    } catch (IOException e) {
        throw new SaslException("DIGEST-MD5: Error acquiring realm, authentication ID or password", e);
    }
    if (username == null || passwd == null) {
        throw new SaslException("DIGEST-MD5: authentication ID and password must be specified");
    }
    /* MAXBUF: optional atmost once */
    int srvMaxBufSize = (challengeVal[MAXBUF] == null) ? DEFAULT_MAXBUF : Integer.parseInt(new String(challengeVal[MAXBUF], encoding));
    sendMaxBufSize = (sendMaxBufSize == 0) ? srvMaxBufSize : Math.min(sendMaxBufSize, srvMaxBufSize);
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException)

Example 82 with NameCallback

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

the class NegotiateCallbackHandler method handle.

public void handle(Callback[] callbacks) throws UnsupportedCallbackException, IOException {
    for (int i = 0; i < callbacks.length; i++) {
        Callback callBack = callbacks[i];
        if (callBack instanceof NameCallback) {
            getAnswer();
            ((NameCallback) callBack).setName(username);
        } else if (callBack instanceof PasswordCallback) {
            getAnswer();
            ((PasswordCallback) callBack).setPassword(password);
            if (password != null)
                Arrays.fill(password, ' ');
        } else {
            throw new UnsupportedCallbackException(callBack, "Call back not supported");
        }
    }
}
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) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 83 with NameCallback

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

the class PlainTextLoginModule method login.

@Override
public boolean login() throws LoginException {
    if (callbackHandler == null) {
        throw new LoginException("CallbackHandler not registered");
    }
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username");
    callbacks[1] = new PasswordCallback("Password", false);
    try {
        callbackHandler.handle(callbacks);
    } catch (IOException ie) {
        throw new LoginException("IOException: " + ie.toString());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException("UnsupportedCallbackException: " + uce.getCallback().toString());
    }
    String username = ((NameCallback) callbacks[0]).getName();
    if (options.containsKey(PLAIN_TEXT_USER_PREFIX + username)) {
        String expectedPassword = (String) options.get(PLAIN_TEXT_USER_PREFIX + username);
        String password = new String(((PasswordCallback) callbacks[1]).getPassword());
        if (!expectedPassword.equals(password)) {
            throw new LoginException("Incorrect password for: " + username);
        }
    } else {
        throw new LoginException("User: " + username + " not found");
    }
    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) LoginException(javax.security.auth.login.LoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 84 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 85 with NameCallback

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

the class TestLoginModule method login.

public boolean login() throws LoginException {
    final Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username:");
    callbacks[1] = new PasswordCallback("Password:", false);
    try {
        callbackHandler.handle(callbacks);
    } catch (final Exception e) {
        throw new LoginException("Failed to perform emulated login: " + e.getMessage());
    }
    user = ((NameCallback) callbacks[0]).getName();
    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) LoginException(javax.security.auth.login.LoginException)

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