Search in sources :

Example 91 with CallbackHandler

use of javax.security.auth.callback.CallbackHandler in project drill by axbaretto.

the class PlainFactory method createSaslClient.

@Override
public SaslClient createSaslClient(final UserGroupInformation ugi, final Map<String, ?> properties) throws SaslException {
    final String userName = (String) properties.get(DrillProperties.USER);
    final String password = (String) properties.get(DrillProperties.PASSWORD);
    return FastSaslClientFactory.getInstance().createSaslClient(new String[] { SIMPLE_NAME }, null, /**
     * authorization ID
     */
    null, null, properties, new CallbackHandler() {

        @Override
        public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (final Callback callback : callbacks) {
                if (callback instanceof NameCallback) {
                    NameCallback.class.cast(callback).setName(userName);
                    continue;
                }
                if (callback instanceof PasswordCallback) {
                    PasswordCallback.class.cast(callback).setPassword(password.toCharArray());
                    continue;
                }
                throw new UnsupportedCallbackException(callback);
            }
        }
    });
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) 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) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 92 with CallbackHandler

use of javax.security.auth.callback.CallbackHandler in project polymap4-core by Polymap4.

the class SpnegoProvider method getUsernamePasswordHandler.

/**
 * Used by the BASIC Auth mechanism for establishing a LoginContext
 * to authenticate a client/caller/request.
 *
 * @param username client username
 * @param password client password
 * @return CallbackHandler to be used for establishing a LoginContext
 */
public static CallbackHandler getUsernamePasswordHandler(final String username, final String password) {
    LOGGER.fine("username=" + username + "; password=" + password.hashCode());
    final CallbackHandler handler = new CallbackHandler() {

        public void handle(final Callback[] callback) {
            for (int i = 0; i < callback.length; i++) {
                if (callback[i] instanceof NameCallback) {
                    final NameCallback nameCallback = (NameCallback) callback[i];
                    nameCallback.setName(username);
                } else if (callback[i] instanceof PasswordCallback) {
                    final PasswordCallback passCallback = (PasswordCallback) callback[i];
                    passCallback.setPassword(password.toCharArray());
                } else {
                    LOGGER.warning("Unsupported Callback i=" + i + "; class=" + callback[i].getClass().getName());
                }
            }
        }
    };
    return handler;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 93 with CallbackHandler

use of javax.security.auth.callback.CallbackHandler in project activemq-artemis by apache.

the class GSSAPIServerSASL method processSASL.

@Override
public byte[] processSASL(byte[] bytes) {
    try {
        if (jaasId == null) {
            // populate subject with acceptor private credentials
            LoginContext loginContext = new LoginContext(loginConfigScope);
            loginContext.login();
            jaasId = loginContext.getSubject();
        }
        if (saslServer == null) {
            saslServer = Subject.doAs(jaasId, (PrivilegedExceptionAction<SaslServer>) () -> Sasl.createSaslServer(NAME, null, null, new HashMap<String, String>(), new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    for (Callback callback : callbacks) {
                        if (callback instanceof AuthorizeCallback) {
                            AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
                            // only ok to authenticate as self
                            authorizeCallback.setAuthorized(authorizeCallback.getAuthenticationID().equals(authorizeCallback.getAuthorizationID()));
                        }
                    }
                }
            }));
        }
        byte[] challenge = Subject.doAs(jaasId, (PrivilegedExceptionAction<byte[]>) () -> saslServer.evaluateResponse(bytes));
        if (saslServer.isComplete()) {
            result = new PrincipalSASLResult(true, new KerberosPrincipal(saslServer.getAuthorizationID()));
        }
        return challenge;
    } catch (Exception outOfHere) {
        log.info("Error on sasl input: " + outOfHere.toString(), outOfHere);
        result = new PrincipalSASLResult(false, null);
    }
    return null;
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) Callback(javax.security.auth.callback.Callback) HashMap(java.util.HashMap) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) SaslException(javax.security.sasl.SaslException)

Example 94 with CallbackHandler

use of javax.security.auth.callback.CallbackHandler in project atlas by apache.

the class SSLAndKerberosTest method loginTestUser.

protected Subject loginTestUser() throws LoginException, IOException {
    LoginContext lc = new LoginContext(TEST_USER_JAAS_SECTION, new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof PasswordCallback) {
                    PasswordCallback passwordCallback = (PasswordCallback) callback;
                    passwordCallback.setPassword(TESTPASS.toCharArray());
                }
                if (callback instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) callback;
                    nameCallback.setName(TESTUSER);
                }
            }
        }
    });
    // attempt authentication
    lc.login();
    return lc.getSubject();
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) 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) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 95 with CallbackHandler

use of javax.security.auth.callback.CallbackHandler in project atlas by apache.

the class AtlasAuthenticationKerberosFilterTest method loginTestUser.

protected Subject loginTestUser() throws LoginException, IOException {
    LoginContext lc = new LoginContext(TEST_USER_JAAS_SECTION, new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof PasswordCallback) {
                    PasswordCallback passwordCallback = (PasswordCallback) callback;
                    passwordCallback.setPassword(TESTPASS.toCharArray());
                }
                if (callback instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) callback;
                    nameCallback.setName(TESTUSER);
                }
            }
        }
    });
    // attempt authentication
    lc.login();
    return lc.getSubject();
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) 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) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

CallbackHandler (javax.security.auth.callback.CallbackHandler)274 Crypto (org.apache.wss4j.common.crypto.Crypto)82 IOException (java.io.IOException)75 Callback (javax.security.auth.callback.Callback)73 Element (org.w3c.dom.Element)70 Subject (javax.security.auth.Subject)68 PasswordCallback (javax.security.auth.callback.PasswordCallback)63 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)61 PasswordCallbackHandler (org.apache.cxf.sts.common.PasswordCallbackHandler)60 Document (org.w3c.dom.Document)60 LoginContext (javax.security.auth.login.LoginContext)55 NameCallback (javax.security.auth.callback.NameCallback)51 LoginException (javax.security.auth.login.LoginException)44 Principal (java.security.Principal)42 ReceivedToken (org.apache.cxf.sts.request.ReceivedToken)42 TokenValidator (org.apache.cxf.sts.token.validator.TokenValidator)38 SAMLTokenValidator (org.apache.cxf.sts.token.validator.SAMLTokenValidator)36 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)34 Test (org.junit.Test)34 ArrayList (java.util.ArrayList)32