Search in sources :

Example 1 with RealmChoiceCallback

use of javax.security.sasl.RealmChoiceCallback in project Smack by igniterealtime.

the class SASLJavaXMechanism method authenticateInternal.

@Override
protected void authenticateInternal() throws SmackException {
    String[] mechanisms = { getName() };
    Map<String, String> props = getSaslProps();
    String authzid = null;
    if (authorizationId != null) {
        authzid = authorizationId.toString();
    }
    try {
        sc = Sasl.createSaslClient(mechanisms, authzid, "xmpp", getServerName().toString(), props, new CallbackHandler() {

            @Override
            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        NameCallback ncb = (NameCallback) callbacks[i];
                        ncb.setName(authenticationId);
                    } else if (callbacks[i] instanceof PasswordCallback) {
                        PasswordCallback pcb = (PasswordCallback) callbacks[i];
                        pcb.setPassword(password.toCharArray());
                    } else if (callbacks[i] instanceof RealmCallback) {
                        RealmCallback rcb = (RealmCallback) callbacks[i];
                        // Retrieve the REALM from the challenge response that
                        // the server returned when the client initiated the
                        // authentication exchange. If this value is not null or
                        // empty, *this value* has to be sent back to the server
                        // in the client's response to the server's challenge
                        String text = rcb.getDefaultText();
                        // The SASL client (sc) created in smack uses
                        // rcb.getText when creating the negotiatedRealm to send
                        // it back to the server. Make sure that this value
                        // matches the server's realm
                        rcb.setText(text);
                    } else if (callbacks[i] instanceof RealmChoiceCallback) {
                    // unused, prevents UnsupportedCallbackException
                    // RealmChoiceCallback rccb =
                    // (RealmChoiceCallback)callbacks[i];
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
    } catch (SaslException e) {
        throw new SmackException(e);
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) NameCallback(javax.security.auth.callback.NameCallback) SmackException(org.jivesoftware.smack.SmackException) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) SaslException(javax.security.sasl.SaslException) RealmChoiceCallback(javax.security.sasl.RealmChoiceCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 2 with RealmChoiceCallback

use of javax.security.sasl.RealmChoiceCallback in project jdk8u_jdk by JetBrains.

the class ClientCallbackHandler method handle.

public void handle(Callback[] callbacks) throws UnsupportedCallbackException, IOException {
    NameCallback ncb = null;
    PasswordCallback pcb = null;
    RealmChoiceCallback rccb = null;
    List namePw = new ArrayList(3);
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
            if (auto) {
                ((NameCallback) callbacks[i]).setName(username);
            } else {
                // To be processed by TextCallbackHandler
                namePw.add(callbacks[i]);
            }
        } else if (callbacks[i] instanceof PasswordCallback) {
            if (auto) {
                ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
            } else {
                // To be processed by TextCallbackHandler
                namePw.add(callbacks[i]);
            }
        } else if (callbacks[i] instanceof RealmChoiceCallback) {
            RealmChoiceCallback rcb = (RealmChoiceCallback) callbacks[i];
            if (!auto) {
                System.err.println(rcb.getPrompt());
            }
            String[] choices = rcb.getChoices();
            if (!auto) {
                for (int j = 0; j < choices.length; j++) {
                    System.err.println(j + ":" + choices[j]);
                }
            }
            int selection;
            if (auto) {
                selection = 0;
            } else {
                System.err.print("Enter choice number: ");
                String result = readLine();
                if (result.equals("")) {
                    selection = rcb.getDefaultChoice();
                } else {
                    selection = Integer.parseInt(result);
                }
            }
            rcb.setSelectedIndex(selection);
        } else if (callbacks[i] instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) callbacks[i];
            String realm = rcb.getDefaultText();
            if (auto) {
                if (realm != null) {
                    rcb.setText(realm);
                }
            } else {
                if (realm == null) {
                    System.err.print(rcb.getPrompt());
                } else {
                    System.err.print(rcb.getPrompt() + " [" + realm + "] ");
                }
                System.err.flush();
                String result = readLine();
                if (result.equals("")) {
                    result = realm;
                }
                rcb.setText(result);
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
    // Process name/password callbacks using superclass
    if (namePw.size() > 0) {
        Callback[] np = new Callback[namePw.size()];
        namePw.toArray(np);
        super.handle(np);
    }
}
Also used : RealmCallback(javax.security.sasl.RealmCallback) RealmChoiceCallback(javax.security.sasl.RealmChoiceCallback) RealmChoiceCallback(javax.security.sasl.RealmChoiceCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 3 with RealmChoiceCallback

use of javax.security.sasl.RealmChoiceCallback 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)3 RealmChoiceCallback (javax.security.sasl.RealmChoiceCallback)3 NameCallback (javax.security.auth.callback.NameCallback)2 PasswordCallback (javax.security.auth.callback.PasswordCallback)2 Callback (javax.security.auth.callback.Callback)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 SaslException (javax.security.sasl.SaslException)1 SaslClientCallbackHandler (org.apache.hadoop.hbase.security.AbstractHBaseSaslRpcClient.SaslClientCallbackHandler)1 SmackException (org.jivesoftware.smack.SmackException)1 Test (org.junit.Test)1