Search in sources :

Example 81 with Callback

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

the class CleanState method go.

void go() throws Exception {
    Krb5LoginModule krb5 = new Krb5LoginModule();
    final String name = OneKDC.USER;
    final char[] password = OneKDC.PASS;
    char[] badpassword = "hellokitty".toCharArray();
    Map<String, String> map = new HashMap<>();
    map.put("useTicketCache", "false");
    map.put("doNotPrompt", "false");
    map.put("tryFirstPass", "true");
    Map<String, Object> shared = new HashMap<>();
    shared.put("javax.security.auth.login.name", name);
    shared.put("javax.security.auth.login.password", badpassword);
    krb5.initialize(new Subject(), new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) {
            for (Callback callback : callbacks) {
                if (callback instanceof NameCallback) {
                    ((NameCallback) callback).setName(name);
                }
                if (callback instanceof PasswordCallback) {
                    ((PasswordCallback) callback).setPassword(password);
                }
            }
        }
    }, shared, map);
    krb5.login();
}
Also used : Krb5LoginModule(com.sun.security.auth.module.Krb5LoginModule) CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) Subject(javax.security.auth.Subject) 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)

Example 82 with Callback

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

the class TrustedIdentityTokenLoginModule method login.

@Override
@SuppressWarnings("unchecked")
public boolean login() throws LoginException {
    // See if shared credentials exist
    if (super.login() == true) {
        // Setup our view of the user
        Object username = sharedState.get("javax.security.auth.login.name");
        if (username instanceof Principal)
            identity = (Principal) username;
        else {
            String name = username.toString();
            try {
                identity = createIdentity(name);
            } catch (Exception e) {
                LoginException le = new LoginException();
                le.initCause(e);
                throw le;
            }
        }
        return true;
    }
    super.loginOk = false;
    if (callbackHandler == null) {
        throw new LoginException();
    }
    SecurityAssociationCallback callback = new SecurityAssociationCallback();
    Callback[] callbacks = { callback };
    final String username;
    try {
        callbackHandler.handle(callbacks);
        username = callback.getPrincipal().getName();
        final Object c = callback.getCredential();
        if (c instanceof SASCurrent) {
            credential = (SASCurrent) c;
        } else {
            return false;
        }
    } catch (IOException e) {
        LoginException le = new LoginException();
        le.initCause(e);
        throw le;
    } catch (UnsupportedCallbackException e) {
        LoginException le = new LoginException();
        le.initCause(e);
        throw le;
    }
    validateCredential(username, credential);
    if (username == null) {
        return false;
    }
    if (identity == null) {
        try {
            identity = createIdentity(username);
        } catch (Exception e) {
            LoginException le = new LoginException();
            le.initCause(e);
            throw le;
        }
    }
    if (getUseFirstPass() == true) {
        // Add the principal to the shared state map
        sharedState.put("javax.security.auth.login.name", identity);
        sharedState.put("javax.security.auth.login.password", credential);
    }
    super.loginOk = true;
    return true;
}
Also used : SASCurrent(org.jboss.iiop.csiv2.SASCurrent) SecurityAssociationCallback(org.jboss.security.auth.callback.SecurityAssociationCallback) Callback(javax.security.auth.callback.Callback) LoginException(javax.security.auth.login.LoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) SecurityAssociationCallback(org.jboss.security.auth.callback.SecurityAssociationCallback) Principal(java.security.Principal) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException)

Example 83 with Callback

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

the class DefaultApplicationClientCallbackHandler method handle.

@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    final SecurityContext context = doPrivileged(SECURITY_CONTEXT);
    for (final Callback current : callbacks) {
        if (current instanceof NameCallback) {
            final NameCallback ncb = (NameCallback) current;
            if (context != null) {
                final Set<Identity> identities = getSubjectInfo(context).getIdentities();
                if (identities.isEmpty()) {
                    ncb.setName(DOLLAR_LOCAL);
                } else {
                    final Identity identity = identities.iterator().next();
                    ncb.setName(identity.getName());
                }
            } else {
                ncb.setName(DOLLAR_LOCAL);
            }
        } else if (current instanceof PasswordCallback) {
            if (context != null) {
                final PasswordCallback pcb = (PasswordCallback) current;
                final Set<Identity> identities = getSubjectInfo(context).getIdentities();
                if (identities.isEmpty()) {
                    throw new UnsupportedCallbackException(current);
                } else {
                    final Identity identity = identities.iterator().next();
                    if (identity instanceof CredentialIdentity) {
                        pcb.setPassword((char[]) ((CredentialIdentity) identity).getCredential());
                    } else {
                        throw new UnsupportedCallbackException(current);
                    }
                }
            }
        } else if (current instanceof RealmCallback) {
            final RealmCallback realmCallback = (RealmCallback) current;
            if (realmCallback.getText() == null) {
                realmCallback.setText(realmCallback.getDefaultText());
            }
        }
    }
}
Also used : CredentialIdentity(org.jboss.security.identity.extensions.CredentialIdentity) RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) Set(java.util.Set) SecurityContext(org.jboss.security.SecurityContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Identity(org.jboss.security.identity.Identity) CredentialIdentity(org.jboss.security.identity.extensions.CredentialIdentity) RealmCallback(javax.security.sasl.RealmCallback)

Example 84 with Callback

use of javax.security.auth.callback.Callback in project adempiere by adempiere.

the class EMailOAuth2SaslClient method evaluateChallenge.

@Override
public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
    if (isComplete) {
        return new byte[] {};
    }
    NameCallback nameCallback = new NameCallback("Enter name");
    Callback[] callbacks = new Callback[] { nameCallback };
    try {
        callback.handle(callbacks);
    } catch (UnsupportedCallbackException e) {
        throw new SaslException("Unsupported callback: " + e);
    } catch (IOException e) {
        throw new SaslException("Failed to execute callback: " + e);
    }
    String email = nameCallback.getName();
    byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", email, token).getBytes();
    isComplete = true;
    return response;
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) SaslException(javax.security.sasl.SaslException)

Example 85 with Callback

use of javax.security.auth.callback.Callback in project zm-mailbox by Zimbra.

the class OAuth2SaslClient method evaluateChallenge.

public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
    if (isComplete) {
        // Empty final response from server, just ignore it.
        return new byte[] {};
    }
    NameCallback nameCallback = new NameCallback("Enter name");
    Callback[] callbacks = new Callback[] { nameCallback };
    try {
        callbackHandler.handle(callbacks);
    } catch (UnsupportedCallbackException e) {
        throw new SaslException("Unsupported callback: " + e);
    } catch (IOException e) {
        throw new SaslException("Failed to execute callback: " + e);
    }
    String username = nameCallback.getName();
    byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", username, oauthToken).getBytes();
    isComplete = true;
    return response;
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) SaslException(javax.security.sasl.SaslException)

Aggregations

Callback (javax.security.auth.callback.Callback)274 NameCallback (javax.security.auth.callback.NameCallback)181 PasswordCallback (javax.security.auth.callback.PasswordCallback)173 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)127 IOException (java.io.IOException)84 LoginException (javax.security.auth.login.LoginException)54 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)49 CallbackHandler (javax.security.auth.callback.CallbackHandler)43 Test (org.testng.annotations.Test)42 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)38 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)31 Subject (javax.security.auth.Subject)31 HashMap (java.util.HashMap)28 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 Principal (java.security.Principal)23 AuthContext (com.sun.identity.authentication.AuthContext)22 FailedLoginException (javax.security.auth.login.FailedLoginException)22 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)19 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)19 LoginContext (javax.security.auth.login.LoginContext)18