/** * Performs ANONYMOUS SASL authentication. 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 will assign a full JID with a randomly generated resource and possibly with * no username. * * @throws SASLErrorException * @throws XMPPErrorException if an error occures while authenticating. * @throws SmackException if there was no response from the server. */ public void authenticateAnonymously() throws SASLErrorException, SmackException, XMPPErrorException { currentMechanism = (new SASLAnonymous()).instanceForAuthentication(connection); // Wait until SASL negotiation finishes synchronized (this) { currentMechanism.authenticate(null, null, null, ""); try { wait(connection.getPacketReplyTimeout()); } catch (InterruptedException e) { // Ignore } } maybeThrowException(); if (!authenticationSuccessful) { throw NoResponseException.newWith(connection); } }
/** * 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 resource the desired resource. * @param cbh the CallbackHandler used to get information from the user * @throws IOException * @throws XMPPErrorException * @throws SASLErrorException * @throws SmackException */ public void authenticate(String resource, CallbackHandler cbh) throws IOException, XMPPErrorException, SASLErrorException, SmackException { SASLMechanism selectedMechanism = selectMechanism(); if (selectedMechanism != null) { currentMechanism = selectedMechanism; synchronized (this) { currentMechanism.authenticate(connection.getHost(), connection.getServiceName(), cbh); try { // Wait until SASL negotiation finishes wait(connection.getPacketReplyTimeout()); } catch (InterruptedException e) { // Ignore } } maybeThrowException(); if (!authenticationSuccessful) { throw NoResponseException.newWith(connection); } } else { throw new SmackException( "SASL Authentication failed. No known authentication mechanisims."); } }
/** * 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 resource the desired resource. * @throws XMPPErrorException * @throws SASLErrorException * @throws IOException * @throws SmackException */ public void authenticate(String username, String password, String resource) throws XMPPErrorException, SASLErrorException, IOException, SmackException { SASLMechanism selectedMechanism = selectMechanism(); if (selectedMechanism != null) { currentMechanism = selectedMechanism; synchronized (this) { currentMechanism.authenticate(username, connection.getHost(), connection.getServiceName(), password); try { // Wait until SASL negotiation finishes wait(connection.getPacketReplyTimeout()); } catch (InterruptedException e) { // Ignore } } maybeThrowException(); if (!authenticationSuccessful) { throw NoResponseException.newWith(connection); } } else { throw new SmackException( "SASL Authentication failed. No known authentication mechanisims."); } }
private void maybeThrowException() throws SmackException, SASLErrorException { 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); } } }
/** * Notification message saying that SASL authentication has failed. The server may have * closed the connection depending on the number of possible retries. * * @param saslFailure the SASL failure as reported by the server * @see <a href="https://tools.ietf.org/html/rfc6120#section-6.5">RFC6120 6.5</a> */ public void authenticationFailed(SASLFailure saslFailure) { authenticationFailed(new SASLErrorException(currentMechanism.getName(), saslFailure)); }