Search in sources :

Example 1 with RealmCallback

use of javax.security.sasl.RealmCallback 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 RealmCallback

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

the class ClientCallbackHandler 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 3 with RealmCallback

use of javax.security.sasl.RealmCallback 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 4 with RealmCallback

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

the class SampleCallbackHandler method handle.

public void handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
            NameCallback cb = (NameCallback) callbacks[i];
            cb.setName(getInput(cb.getPrompt()));
        } else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback cb = (PasswordCallback) callbacks[i];
            String pw = getInput(cb.getPrompt());
            char[] passwd = new char[pw.length()];
            pw.getChars(0, passwd.length, passwd, 0);
            cb.setPassword(passwd);
        } else if (callbacks[i] instanceof RealmCallback) {
            RealmCallback cb = (RealmCallback) callbacks[i];
            cb.setText(getInput(cb.getPrompt()));
        } else {
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback)

Example 5 with RealmCallback

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

the class PropertiesFileCallbackHandler method handle.

public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    NameCallback ncb = null;
    PasswordCallback pcb = null;
    AuthorizeCallback acb = null;
    RealmCallback rcb = null;
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
            ncb = (NameCallback) callbacks[i];
        } else if (callbacks[i] instanceof PasswordCallback) {
            pcb = (PasswordCallback) callbacks[i];
        } else if (callbacks[i] instanceof AuthorizeCallback) {
            acb = (AuthorizeCallback) callbacks[i];
        } else if (callbacks[i] instanceof RealmCallback) {
            rcb = (RealmCallback) callbacks[i];
        } else {
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
    if (pcb != null && ncb != null) {
        String username = ncb.getDefaultName();
        String pw = pwDb.getProperty(username);
        if (pw != null) {
            char[] pwchars = pw.toCharArray();
            pcb.setPassword(pwchars);
            // Clear pw
            for (int i = 0; i < pwchars.length; i++) {
                pwchars[i] = 0;
            }
            // Set canonicalized username if any
            String canonAuthid = (namesDb != null ? namesDb.getProperty(username) : null);
            if (canonAuthid != null) {
                ncb.setName(canonAuthid);
            }
        }
    }
    if (acb != null) {
        String authid = acb.getAuthenticationID();
        String authzid = acb.getAuthorizationID();
        if (authid.equals(authzid)) {
            // Self is always authorized
            acb.setAuthorized(true);
        } else {
            // Check db for allowed authzids
            String authzes = (proxyDb != null ? proxyDb.getProperty(authid) : null);
            if (authzes != null && authzes.indexOf(authzid) >= 0) {
                // XXX need to search for subtrings or use StringTokenizer
                // to avoid incorrectly matching subnames
                acb.setAuthorized(true);
            }
        }
        if (acb.isAuthorized()) {
            // Set canonicalized name
            String canonAuthzid = (namesDb != null ? namesDb.getProperty(authzid) : null);
            if (canonAuthzid != null) {
                acb.setAuthorizedID(canonAuthzid);
            }
        }
    }
}
Also used : AuthorizeCallback(javax.security.sasl.AuthorizeCallback) RealmCallback(javax.security.sasl.RealmCallback)

Aggregations

RealmCallback (javax.security.sasl.RealmCallback)17 NameCallback (javax.security.auth.callback.NameCallback)14 PasswordCallback (javax.security.auth.callback.PasswordCallback)13 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)11 Callback (javax.security.auth.callback.Callback)10 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)7 IOException (java.io.IOException)3 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 Set (java.util.Set)1 ConsoleHandler (java.util.logging.ConsoleHandler)1 Handler (java.util.logging.Handler)1 LoginException (javax.security.auth.login.LoginException)1 SaslException (javax.security.sasl.SaslException)1 SaslServer (javax.security.sasl.SaslServer)1