Search in sources :

Example 1 with AuthenticationException

use of org.apache.cassandra.exceptions.AuthenticationException in project cassandra by apache.

the class PasswordAuthenticator method legacyAuthenticate.

public AuthenticatedUser legacyAuthenticate(Map<String, String> credentials) throws AuthenticationException {
    String username = credentials.get(USERNAME_KEY);
    if (username == null)
        throw new AuthenticationException(String.format("Required key '%s' is missing", USERNAME_KEY));
    String password = credentials.get(PASSWORD_KEY);
    if (password == null)
        throw new AuthenticationException(String.format("Required key '%s' is missing for provided username %s", PASSWORD_KEY, username));
    return authenticate(username, password);
}
Also used : AuthenticationException(org.apache.cassandra.exceptions.AuthenticationException)

Example 2 with AuthenticationException

use of org.apache.cassandra.exceptions.AuthenticationException in project cassandra by apache.

the class CassandraLoginModule method login.

/**
     * Authenticate the user, obtaining credentials from the CallbackHandler
     * supplied in {@code}initialize{@code}. As long as the configured
     * {@code}IAuthenticator{@code} supports the optional
     * {@code}legacyAuthenticate{@code} method, it can be used here.
     *
     * @return true in all cases since this {@code}LoginModule{@code}
     *         should not be ignored.
     * @exception FailedLoginException if the authentication fails.
     * @exception LoginException if this {@code}LoginModule{@code} is unable to
     * perform the authentication.
     */
@Override
public boolean login() throws LoginException {
    // prompt for a user name and password
    if (callbackHandler == null) {
        logger.info("No CallbackHandler available for authentication");
        throw new LoginException("Authentication failed");
    }
    NameCallback nc = new NameCallback("username: ");
    PasswordCallback pc = new PasswordCallback("password: ", false);
    try {
        callbackHandler.handle(new Callback[] { nc, pc });
        username = nc.getName();
        char[] tmpPassword = pc.getPassword();
        if (tmpPassword == null)
            tmpPassword = new char[0];
        password = new char[tmpPassword.length];
        System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
        pc.clearPassword();
    } catch (IOException | UnsupportedCallbackException e) {
        logger.info("Unexpected exception processing authentication callbacks", e);
        throw new LoginException("Authentication failed");
    }
    // verify the credentials
    try {
        authenticate();
    } catch (AuthenticationException e) {
        // authentication failed -- clean up
        succeeded = false;
        cleanUpInternalState();
        throw new FailedLoginException(e.getMessage());
    }
    succeeded = true;
    return true;
}
Also used : FailedLoginException(javax.security.auth.login.FailedLoginException) AuthenticationException(org.apache.cassandra.exceptions.AuthenticationException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) IOException(java.io.IOException)

Example 3 with AuthenticationException

use of org.apache.cassandra.exceptions.AuthenticationException in project cassandra by apache.

the class CassandraLoginModule method authenticate.

private void authenticate() {
    if (!StorageService.instance.isAuthSetupComplete())
        throw new AuthenticationException("Cannot login as server authentication setup is not yet completed");
    IAuthenticator authenticator = DatabaseDescriptor.getAuthenticator();
    Map<String, String> credentials = new HashMap<>();
    credentials.put(PasswordAuthenticator.USERNAME_KEY, username);
    credentials.put(PasswordAuthenticator.PASSWORD_KEY, String.valueOf(password));
    AuthenticatedUser user = authenticator.legacyAuthenticate(credentials);
    // Only actual users should be allowed to authenticate for JMX
    if (user.isAnonymous() || user.isSystem())
        throw new AuthenticationException(String.format("Invalid user %s", user.getName()));
    // The LOGIN privilege is required to authenticate - c.f. ClientState::login
    if (!DatabaseDescriptor.getRoleManager().canLogin(user.getPrimaryRole()))
        throw new AuthenticationException(user.getName() + " is not permitted to log in");
}
Also used : AuthenticationException(org.apache.cassandra.exceptions.AuthenticationException) HashMap(java.util.HashMap)

Example 4 with AuthenticationException

use of org.apache.cassandra.exceptions.AuthenticationException in project cassandra by apache.

the class AuthResponse method execute.

@Override
public Response execute(QueryState queryState, long queryStartNanoTime) {
    try {
        IAuthenticator.SaslNegotiator negotiator = ((ServerConnection) connection).getSaslNegotiator(queryState);
        byte[] challenge = negotiator.evaluateResponse(token);
        if (negotiator.isComplete()) {
            AuthenticatedUser user = negotiator.getAuthenticatedUser();
            queryState.getClientState().login(user);
            AuthMetrics.instance.markSuccess();
            // authentication is complete, send a ready message to the client
            return new AuthSuccess(challenge);
        } else {
            return new AuthChallenge(challenge);
        }
    } catch (AuthenticationException e) {
        AuthMetrics.instance.markFailure();
        return ErrorMessage.fromException(e);
    }
}
Also used : AuthenticationException(org.apache.cassandra.exceptions.AuthenticationException) IAuthenticator(org.apache.cassandra.auth.IAuthenticator) AuthenticatedUser(org.apache.cassandra.auth.AuthenticatedUser)

Example 5 with AuthenticationException

use of org.apache.cassandra.exceptions.AuthenticationException in project cassandra by apache.

the class CredentialsMessage method execute.

public Message.Response execute(QueryState state, long queryStartNanoTime) {
    try {
        AuthenticatedUser user = DatabaseDescriptor.getAuthenticator().legacyAuthenticate(credentials);
        state.getClientState().login(user);
        AuthMetrics.instance.markSuccess();
    } catch (AuthenticationException e) {
        AuthMetrics.instance.markFailure();
        return ErrorMessage.fromException(e);
    }
    return new ReadyMessage();
}
Also used : AuthenticationException(org.apache.cassandra.exceptions.AuthenticationException) AuthenticatedUser(org.apache.cassandra.auth.AuthenticatedUser)

Aggregations

AuthenticationException (org.apache.cassandra.exceptions.AuthenticationException)6 AuthenticatedUser (org.apache.cassandra.auth.AuthenticatedUser)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 LoginException (javax.security.auth.login.LoginException)1 IAuthenticator (org.apache.cassandra.auth.IAuthenticator)1 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)1 SelectStatement (org.apache.cassandra.cql3.statements.SelectStatement)1 RequestExecutionException (org.apache.cassandra.exceptions.RequestExecutionException)1 ResultMessage (org.apache.cassandra.transport.messages.ResultMessage)1