Search in sources :

Example 1 with SASLErrorException

use of org.jivesoftware.smack.sasl.SASLErrorException in project Smack by igniterealtime.

the class LoginIntegrationTest method testInvalidLogin.

/**
     * Check that the server is returning the correct error when trying to login using an invalid
     * (i.e. non-existent) user.
     *
     * @throws InterruptedException 
     * @throws XMPPException 
     * @throws IOException 
     * @throws SmackException 
     * @throws NoSuchAlgorithmException 
     * @throws KeyManagementException 
     */
@SmackIntegrationTest
public void testInvalidLogin() throws SmackException, IOException, XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
    final String nonExistentUserString = StringUtils.insecureRandomString(24);
    XMPPTCPConnectionConfiguration conf = getConnectionConfiguration().setUsernameAndPassword(nonExistentUserString, "invalidPassword").build();
    XMPPTCPConnection connection = new XMPPTCPConnection(conf);
    connection.connect();
    try {
        connection.login();
        fail("Exception expected");
    } catch (SASLErrorException e) {
        assertEquals(SASLError.not_authorized, e.getSASLFailure().getSASLError());
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SASLErrorException(org.jivesoftware.smack.sasl.SASLErrorException) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 2 with SASLErrorException

use of org.jivesoftware.smack.sasl.SASLErrorException in project Smack by igniterealtime.

the class SASLAuthentication method authenticate.

/**
     * Performs SASL authentication of the specified user. If SASL authentication was successful
     * then resource binding and session establishment will be performed. This method will return
     * the full JID provided by the server while binding a resource to the connection.<p>
     *
     * The server may assign a full JID with a username or resource different than the requested
     * by this method.
     *
     * @param username the username that is authenticating with the server.
     * @param password the password to send to the server.
     * @param authzid the authorization identifier (typically null).
     * @param sslSession the optional SSL/TLS session (if one was established)
     * @throws XMPPErrorException
     * @throws SASLErrorException
     * @throws IOException
     * @throws SmackException
     * @throws InterruptedException
     */
public void authenticate(String username, String password, EntityBareJid authzid, SSLSession sslSession) throws XMPPErrorException, SASLErrorException, IOException, SmackException, InterruptedException {
    currentMechanism = selectMechanism(authzid);
    final CallbackHandler callbackHandler = configuration.getCallbackHandler();
    final String host = connection.getHost();
    final DomainBareJid xmppServiceDomain = connection.getXMPPServiceDomain();
    synchronized (this) {
        if (callbackHandler != null) {
            currentMechanism.authenticate(host, xmppServiceDomain, callbackHandler, authzid, sslSession);
        } else {
            currentMechanism.authenticate(username, host, xmppServiceDomain, password, authzid, sslSession);
        }
        final long deadline = System.currentTimeMillis() + connection.getReplyTimeout();
        while (!authenticationSuccessful && saslException == null) {
            final long now = System.currentTimeMillis();
            if (now >= deadline)
                break;
            // Wait until SASL negotiation finishes
            wait(deadline - now);
        }
    }
    if (saslException != null) {
        if (saslException instanceof SmackException) {
            throw (SmackException) saslException;
        } else if (saslException instanceof SASLErrorException) {
            throw (SASLErrorException) saslException;
        } else {
            throw new IllegalStateException("Unexpected exception type", saslException);
        }
    }
    if (!authenticationSuccessful) {
        throw NoResponseException.newWith(connection, "successful SASL authentication");
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) SASLErrorException(org.jivesoftware.smack.sasl.SASLErrorException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Aggregations

SASLErrorException (org.jivesoftware.smack.sasl.SASLErrorException)2 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)1 XMPPTCPConnectionConfiguration (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration)1 DomainBareJid (org.jxmpp.jid.DomainBareJid)1