Search in sources :

Example 56 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project simba-os by cegeka.

the class WsPlainTextCallbackHandlerTest method testHandle_NameCallback_passwordIsSet.

@Test
public void testHandle_NameCallback_passwordIsSet() throws Exception {
    Callback[] callbacks = new Callback[1];
    PasswordCallback passwordCallback = new PasswordCallback("password", false);
    callbacks[0] = passwordCallback;
    handler.handle(callbacks);
    assertEquals(password, new String(passwordCallback.getPassword()));
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) LanguageCallback(javax.security.auth.callback.LanguageCallback) Callback(javax.security.auth.callback.Callback) PasswordCallback(javax.security.auth.callback.PasswordCallback) Test(org.junit.Test)

Example 57 with PasswordCallback

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

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

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

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

the class CustomLoginModule method login.

/*
     * Authenticate the user.
     */
@Override
public boolean login() throws LoginException {
    // prompt for a user name and password
    if (callbackHandler == null) {
        throw new LoginException("No CallbackHandler available");
    }
    // standard callbacks
    NameCallback name = new NameCallback("username: ", "default");
    PasswordCallback passwd = new PasswordCallback("password: ", false);
    LanguageCallback language = new LanguageCallback();
    TextOutputCallback error = new TextOutputCallback(TextOutputCallback.ERROR, "This is an error");
    TextOutputCallback warning = new TextOutputCallback(TextOutputCallback.WARNING, "This is a warning");
    TextOutputCallback info = new TextOutputCallback(TextOutputCallback.INFORMATION, "This is a FYI");
    TextInputCallback text = new TextInputCallback("Please type " + HELLO, "Bye");
    ChoiceCallback choice = new ChoiceCallback("Choice: ", new String[] { "pass", "fail" }, 1, true);
    ConfirmationCallback confirmation = new ConfirmationCallback("confirmation: ", ConfirmationCallback.INFORMATION, ConfirmationCallback.YES_NO_OPTION, ConfirmationCallback.NO);
    CustomCallback custom = new CustomCallback();
    Callback[] callbacks = new Callback[] { choice, info, warning, error, name, passwd, text, language, confirmation, custom };
    boolean uce = false;
    try {
        callbackHandler.handle(callbacks);
    } catch (UnsupportedCallbackException e) {
        Callback callback = e.getCallback();
        if (custom.equals(callback)) {
            uce = true;
            System.out.println("CustomLoginModule: " + "custom callback not supported as expected");
        } else {
            throw new LoginException("Unsupported callback: " + callback);
        }
    } catch (IOException ioe) {
        throw new LoginException(ioe.toString());
    }
    if (!uce) {
        throw new RuntimeException("UnsupportedCallbackException " + "not thrown");
    }
    if (!HELLO.equals(text.getText())) {
        System.out.println("Text: " + text.getText());
        throw new FailedLoginException("No hello");
    }
    if (!Locale.GERMANY.equals(language.getLocale())) {
        System.out.println("Selected locale: " + language.getLocale());
        throw new FailedLoginException("Achtung bitte");
    }
    String readUsername = name.getName();
    char[] readPassword = passwd.getPassword();
    if (readPassword == null) {
        // treat a NULL password as an empty password
        readPassword = new char[0];
    }
    passwd.clearPassword();
    // verify the username/password
    if (!username.equals(readUsername) || !Arrays.equals(password, readPassword)) {
        loginSucceeded = false;
        throw new FailedLoginException("Username/password is not correct");
    }
    // check chosen option
    int[] selected = choice.getSelectedIndexes();
    if (selected == null || selected.length == 0) {
        throw new FailedLoginException("Nothing selected");
    }
    if (selected[0] != 0) {
        throw new FailedLoginException("Wrong choice: " + selected[0]);
    }
    // check confirmation
    if (confirmation.getSelectedIndex() != ConfirmationCallback.YES) {
        throw new FailedLoginException("Not confirmed: " + confirmation.getSelectedIndex());
    }
    loginSucceeded = true;
    System.out.println("CustomLoginModule: authentication succeeded");
    return true;
}
Also used : ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) IOException(java.io.IOException) LanguageCallback(javax.security.auth.callback.LanguageCallback) TextInputCallback(javax.security.auth.callback.TextInputCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) TextInputCallback(javax.security.auth.callback.TextInputCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) LanguageCallback(javax.security.auth.callback.LanguageCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) FailedLoginException(javax.security.auth.login.FailedLoginException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) 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