Search in sources :

Example 11 with RealmCallback

use of javax.security.sasl.RealmCallback in project apex-core by apache.

the class DefaultCallbackHandlerTest method testHandler.

@Test
public void testHandler() {
    DefaultCallbackHandler handler = new DefaultCallbackHandler();
    SecurityContext context = new SecurityContext();
    handler.setup(context);
    Callback[] callbacks = new Callback[3];
    callbacks[0] = new NameCallback("UserName:");
    callbacks[1] = new PasswordCallback("Password:", false);
    callbacks[2] = new RealmCallback("Realm:");
    try {
        handler.handle(callbacks);
        Assert.assertEquals("Username", "user1", ((NameCallback) callbacks[0]).getName());
        Assert.assertEquals("Password", "pass", new String(((PasswordCallback) callbacks[1]).getPassword()));
        Assert.assertEquals("Realm", "default", ((RealmCallback) callbacks[2]).getText());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    } catch (UnsupportedCallbackException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : 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) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback) Test(org.junit.Test)

Example 12 with RealmCallback

use of javax.security.sasl.RealmCallback in project Openfire by igniterealtime.

the class XMPPCallbackHandler method handle.

@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    String realm;
    String name = null;
    for (Callback callback : callbacks) {
        if (callback instanceof RealmCallback) {
            ((RealmCallback) callback).setText(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
        } else if (callback instanceof NameCallback) {
            name = ((NameCallback) callback).getName();
            if (name == null) {
                name = ((NameCallback) callback).getDefaultName();
            }
        //Log.debug("XMPPCallbackHandler: NameCallback: " + name);
        } else if (callback instanceof PasswordCallback) {
            try {
                // Get the password from the UserProvider. Some UserProviders may not support
                // this operation
                ((PasswordCallback) callback).setPassword(AuthFactory.getPassword(name).toCharArray());
            //Log.debug("XMPPCallbackHandler: PasswordCallback");
            } catch (UserNotFoundException | UnsupportedOperationException e) {
                throw new IOException(e.toString());
            }
        } else if (callback instanceof VerifyPasswordCallback) {
            //Log.debug("XMPPCallbackHandler: VerifyPasswordCallback");
            VerifyPasswordCallback vpcb = (VerifyPasswordCallback) callback;
            try {
                AuthToken at = AuthFactory.authenticate(name, new String(vpcb.getPassword()));
                vpcb.setVerified((at != null));
            } catch (Exception e) {
                vpcb.setVerified(false);
            }
        } else if (callback instanceof AuthorizeCallback) {
            //Log.debug("XMPPCallbackHandler: AuthorizeCallback");
            AuthorizeCallback authCallback = ((AuthorizeCallback) callback);
            // Principal that authenticated
            String principal = authCallback.getAuthenticationID();
            // Username requested (not full JID)
            String username = authCallback.getAuthorizationID();
            // a lot of users to fail to log in if their clients is sending an incorrect value
            if (username != null && username.contains("@")) {
                username = username.substring(0, username.lastIndexOf("@"));
            }
            if (principal.equals(username)) {
                //client perhaps made no request, get default username
                username = AuthorizationManager.map(principal);
                if (Log.isDebugEnabled()) {
                //Log.debug("XMPPCallbackHandler: no username requested, using " + username);
                }
            }
            if (AuthorizationManager.authorize(username, principal)) {
                if (Log.isDebugEnabled()) {
                //Log.debug("XMPPCallbackHandler: " + principal + " authorized to " + username);
                }
                authCallback.setAuthorized(true);
                authCallback.setAuthorizedID(username);
            } else {
                if (Log.isDebugEnabled()) {
                //Log.debug("XMPPCallbackHandler: " + principal + " not authorized to " + username);
                }
                authCallback.setAuthorized(false);
            }
        } else {
            if (Log.isDebugEnabled()) {
            //Log.debug("XMPPCallbackHandler: Callback: " + callback.getClass().getSimpleName());
            }
            throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) VerifyPasswordCallback(org.jivesoftware.openfire.sasl.VerifyPasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) VerifyPasswordCallback(org.jivesoftware.openfire.sasl.VerifyPasswordCallback) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) VerifyPasswordCallback(org.jivesoftware.openfire.sasl.VerifyPasswordCallback) AuthToken(org.jivesoftware.openfire.auth.AuthToken) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback)

Example 13 with RealmCallback

use of javax.security.sasl.RealmCallback in project storm by apache.

the class AbstractSaslClientCallbackHandler method handle.

/**
     * This method is invoked by SASL for authentication challenges
     * @param callbacks a collection of challenge callbacks
     */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback c : callbacks) {
        if (c instanceof NameCallback) {
            LOG.debug("name callback");
            NameCallback nc = (NameCallback) c;
            nc.setName(_username);
        } else if (c instanceof PasswordCallback) {
            LOG.debug("password callback");
            PasswordCallback pc = (PasswordCallback) c;
            if (_password != null) {
                pc.setPassword(_password.toCharArray());
            }
        } else if (c instanceof AuthorizeCallback) {
            LOG.debug("authorization callback");
            AuthorizeCallback ac = (AuthorizeCallback) c;
            String authid = ac.getAuthenticationID();
            String authzid = ac.getAuthorizationID();
            if (authid.equals(authzid)) {
                ac.setAuthorized(true);
            } else {
                ac.setAuthorized(false);
            }
            if (ac.isAuthorized()) {
                ac.setAuthorizedID(authzid);
            }
        } else if (c instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) c;
            ((RealmCallback) c).setText(rc.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(c);
        }
    }
}
Also used : RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 14 with RealmCallback

use of javax.security.sasl.RealmCallback in project kafka by apache.

the class SaslClientCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            if (!isKerberos && subject != null && !subject.getPublicCredentials(String.class).isEmpty()) {
                nc.setName(subject.getPublicCredentials(String.class).iterator().next());
            } else
                nc.setName(nc.getDefaultName());
        } else if (callback instanceof PasswordCallback) {
            if (!isKerberos && subject != null && !subject.getPrivateCredentials(String.class).isEmpty()) {
                char[] password = subject.getPrivateCredentials(String.class).iterator().next().toCharArray();
                ((PasswordCallback) callback).setPassword(password);
            } else {
                String errorMessage = "Could not login: the client is being asked for a password, but the Kafka" + " client code does not currently support obtaining a password from the user.";
                if (isKerberos) {
                    errorMessage += " Make sure -Djava.security.auth.login.config property passed to JVM and" + " the client is configured to use a ticket cache (using" + " the JAAS configuration setting 'useTicketCache=true)'. Make sure you are using" + " FQDN of the Kafka broker you are trying to connect to.";
                }
                throw new UnsupportedCallbackException(callback, errorMessage);
            }
        } else if (callback instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) callback;
            rc.setText(rc.getDefaultText());
        } else if (callback instanceof AuthorizeCallback) {
            AuthorizeCallback ac = (AuthorizeCallback) callback;
            String authId = ac.getAuthenticationID();
            String authzId = ac.getAuthorizationID();
            ac.setAuthorized(authId.equals(authzId));
            if (ac.isAuthorized())
                ac.setAuthorizedID(authzId);
        } else {
            throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
        }
    }
}
Also used : RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 15 with RealmCallback

use of javax.security.sasl.RealmCallback in project hbase by apache.

the class TestHBaseSaslRpcClient method testSaslClientCallbackHandler.

@Test
public void testSaslClientCallbackHandler() throws UnsupportedCallbackException {
    final Token<? extends TokenIdentifier> token = createTokenMock();
    when(token.getIdentifier()).thenReturn(DEFAULT_USER_NAME.getBytes());
    when(token.getPassword()).thenReturn(DEFAULT_USER_PASSWORD.getBytes());
    final NameCallback nameCallback = mock(NameCallback.class);
    final PasswordCallback passwordCallback = mock(PasswordCallback.class);
    final RealmCallback realmCallback = mock(RealmCallback.class);
    final RealmChoiceCallback realmChoiceCallback = mock(RealmChoiceCallback.class);
    Callback[] callbackArray = { nameCallback, passwordCallback, realmCallback, realmChoiceCallback };
    final SaslClientCallbackHandler saslClCallbackHandler = new SaslClientCallbackHandler(token);
    saslClCallbackHandler.handle(callbackArray);
    verify(nameCallback).setName(anyString());
    verify(realmCallback).setText(anyString());
    verify(passwordCallback).setPassword(any(char[].class));
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) RealmChoiceCallback(javax.security.sasl.RealmChoiceCallback) Callback(javax.security.auth.callback.Callback) PasswordCallback(javax.security.auth.callback.PasswordCallback) SaslClientCallbackHandler(org.apache.hadoop.hbase.security.AbstractHBaseSaslRpcClient.SaslClientCallbackHandler) RealmChoiceCallback(javax.security.sasl.RealmChoiceCallback) RealmCallback(javax.security.sasl.RealmCallback) Test(org.junit.Test)

Aggregations

RealmCallback (javax.security.sasl.RealmCallback)19 NameCallback (javax.security.auth.callback.NameCallback)16 PasswordCallback (javax.security.auth.callback.PasswordCallback)15 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)13 Callback (javax.security.auth.callback.Callback)12 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)9 IOException (java.io.IOException)4 CallbackHandler (javax.security.auth.callback.CallbackHandler)3 RealmChoiceCallback (javax.security.sasl.RealmChoiceCallback)3 Test (org.junit.Test)3 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 ConsoleHandler (java.util.logging.ConsoleHandler)1 Handler (java.util.logging.Handler)1 LoginException (javax.security.auth.login.LoginException)1