Search in sources :

Example 46 with NameCallback

use of javax.security.auth.callback.NameCallback in project AuthMeReloaded by AuthMe.

the class OAuth2SaslClient method evaluateChallenge.

public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
    if (isComplete) {
        // Empty final response from server, just ignore it.
        return new byte[] {};
    }
    NameCallback nameCallback = new NameCallback("Enter name");
    Callback[] callbacks = new Callback[] { nameCallback };
    try {
        callbackHandler.handle(callbacks);
    } catch (UnsupportedCallbackException e) {
        throw new SaslException("Unsupported callback: " + e);
    } catch (IOException e) {
        throw new SaslException("Failed to execute callback: " + e);
    }
    String email = nameCallback.getName();
    byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", email, oauthToken).getBytes();
    isComplete = true;
    return response;
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) SaslException(javax.security.sasl.SaslException)

Example 47 with NameCallback

use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.

the class Anonymous method process.

public int process(Callback[] callbacks, int state) throws AuthLoginException {
    if (errorMsg != null) {
        throw new AuthLoginException(amAuthAnonymous, errorMsg, null);
    }
    useSharedstate = isSharedStateEnabled();
    try {
        if (useSharedstate) {
            usernameParam = (String) sharedState.get(getUserKey());
            if (processAnonUser(usernameParam)) {
                setAuthLevel(authLevel);
                return ISAuthConstants.LOGIN_SUCCEED;
            }
        }
        if (callbacks != null && callbacks.length > 0) {
            if (callbacks[0] instanceof NameCallback) {
                usernameParam = ((NameCallback) callbacks[0]).getName();
                if (debug.messageEnabled()) {
                    debug.message("Anonymous:process received NameCallback " + usernameParam);
                }
                if (processAnonUser(usernameParam)) {
                    setAuthLevel(authLevel);
                    return ISAuthConstants.LOGIN_SUCCEED;
                }
            }
        }
        if (validAnonUsernames != null && !(validAnonUsernames.isEmpty())) {
            usernameParam = sendCallback();
        } else {
            usernameParam = defaultAnonUser;
        }
        storeUsernamePasswd(usernameParam, null);
        processAnonUser(usernameParam);
        setAuthLevel(authLevel);
        if (debug.messageEnabled()) {
            debug.message("Set auth level: " + authLevel + "\nAnonymous userid: " + userTokenId);
        }
    } catch (Exception e) {
        debug.error("login: User not found in valid Anon List");
        setFailureID(usernameParam);
        throw new AuthLoginException(amAuthAnonymous, "AnonValidateEx", null);
    }
    return ISAuthConstants.LOGIN_SUCCEED;
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException)

Example 48 with NameCallback

use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.

the class Application method authenticateToDatastore.

/**
     * Authenticates to the datastore using idRepo API
     *
     * @param userName User Name
     * @param userPassword User Password
     * @return <code>true</code> if success. <code>false</code> if failure
     * @throws <code> AuthLoginException </code> 
     */
private boolean authenticateToDatastore(String userName, String userPassword) throws AuthLoginException {
    boolean retval = false;
    Callback[] callbacks = new Callback[2];
    NameCallback nameCallback = new NameCallback("NamePrompt");
    nameCallback.setName(userName);
    callbacks[0] = nameCallback;
    PasswordCallback passwordCallback = new PasswordCallback("PasswordPrompt", false);
    passwordCallback.setPassword(userPassword.toCharArray());
    callbacks[1] = passwordCallback;
    try {
        AMIdentityRepository idrepo = getAMIdentityRepository(getRequestOrg());
        retval = idrepo.authenticate(callbacks);
    } catch (IdRepoException idrepoExp) {
        if (debug.messageEnabled()) {
            debug.message("Application.authenticateToDatastore:  " + "IdRepo Exception", idrepoExp);
        }
    }
    return retval;
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) IdRepoException(com.sun.identity.idm.IdRepoException) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 49 with NameCallback

use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.

the class DataStore method process.

public int process(Callback[] callbacks, int state) throws AuthLoginException {
    currentState = state;
    int retVal = 0;
    Callback[] idCallbacks = new Callback[2];
    try {
        if (currentState == ISAuthConstants.LOGIN_START) {
            if (callbacks != null && callbacks.length == 0) {
                userName = (String) sharedState.get(getUserKey());
                userPassword = (String) sharedState.get(getPwdKey());
                if (userName == null || userPassword == null) {
                    return ISAuthConstants.LOGIN_START;
                }
                NameCallback nameCallback = new NameCallback("dummy");
                nameCallback.setName(userName);
                idCallbacks[0] = nameCallback;
                PasswordCallback passwordCallback = new PasswordCallback("dummy", false);
                passwordCallback.setPassword(userPassword.toCharArray());
                idCallbacks[1] = passwordCallback;
            } else {
                idCallbacks = callbacks;
                //callbacks is not null
                userName = ((NameCallback) callbacks[0]).getName();
                char[] password = ((PasswordCallback) callbacks[1]).getPassword();
                userPassword = password == null ? null : String.valueOf(password);
            }
            if (userName == null) {
                debug.message("DataStore.process: Username is null/empty");
                throw new UserNamePasswordValidationException("amAuth", "InvalidUP", null);
            }
            if (userPassword == null || userPassword.length() == 0) {
                debug.message("DataStore.process: Password is null/empty");
                throw new InvalidPasswordException("amAuth", "invalidPasswd", null);
            }
            //store username password both in success and failure case
            storeUsernamePasswd(userName, userPassword);
            /*
                Fix for OPENAM-1872. Reject usernames with illegal characters (e.g. * or ! or ) or ( or & ), just
                like the LDAP LoginModule does. List of invalid characters comes from a new configuration entry (though
                the list of illegal characters does not seem to be processed in validateUserName). I want the invocation
                to be just like the LDAP LoginModule, and to handle the case in which the username format validator
                cannot be successfully loaded in validateUserName.
                 */
            validateUserName(userName, CollectionHelper.getMapAttr(currentConfig, INVALID_CHARS));
            AMIdentityRepository idrepo = getAMIdentityRepository(getRequestOrg());
            boolean success = idrepo.authenticate(idCallbacks);
            if (success) {
                retVal = ISAuthConstants.LOGIN_SUCCEED;
                validatedUserID = userName;
            } else {
                throw new AuthLoginException(amAuthDataStore, "authFailed", null);
            }
        } else {
            setFailureID(userName);
            throw new AuthLoginException(amAuthDataStore, "authFailed", null);
        }
    } catch (IdRepoException ex) {
        debug.message("idRepo Exception");
        setFailureID(userName);
        throw new AuthLoginException(amAuthDataStore, "authFailed", null, ex);
    }
    return retVal;
}
Also used : UserNamePasswordValidationException(com.sun.identity.authentication.spi.UserNamePasswordValidationException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) IdRepoException(com.sun.identity.idm.IdRepoException) PasswordCallback(javax.security.auth.callback.PasswordCallback) InvalidPasswordException(com.sun.identity.authentication.spi.InvalidPasswordException) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException)

Example 50 with NameCallback

use of javax.security.auth.callback.NameCallback in project OpenAM by OpenRock.

the class Anonymous method sendCallback.

private String sendCallback() throws AuthLoginException {
    if (callbackHandler == null) {
        throw new AuthLoginException(amAuthAnonymous, "NoCallbackHandler", null);
    }
    String username = null;
    try {
        Callback[] callbacks = new Callback[2];
        String header = bundle.getString("moduleHeader");
        PagePropertiesCallback ppc = new PagePropertiesCallback(null, header, null, 0, null, false, null);
        callbacks[0] = ppc;
        callbacks[1] = new NameCallback(bundle.getString("username"));
        if (debug.messageEnabled()) {
            debug.message("Callback 0 is.. :" + callbacks[0]);
            debug.message("Callback 1 is.. :" + callbacks[1]);
        }
        callbackHandler.handle(callbacks);
        username = ((NameCallback) callbacks[1]).getName();
        return username;
    } catch (IllegalArgumentException ill) {
        debug.message("message type missing");
        throw new AuthLoginException(amAuthAnonymous, "IllegalArgs", null);
    } catch (java.io.IOException ioe) {
        throw new AuthLoginException(ioe);
    } catch (UnsupportedCallbackException uce) {
        throw new AuthLoginException(amAuthAnonymous, "NoCallbackHandler", null);
    }
}
Also used : PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

NameCallback (javax.security.auth.callback.NameCallback)203 PasswordCallback (javax.security.auth.callback.PasswordCallback)161 Callback (javax.security.auth.callback.Callback)140 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)101 IOException (java.io.IOException)60 LoginException (javax.security.auth.login.LoginException)51 CallbackHandler (javax.security.auth.callback.CallbackHandler)27 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)22 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)22 RealmCallback (javax.security.sasl.RealmCallback)22 Subject (javax.security.auth.Subject)19 FailedLoginException (javax.security.auth.login.FailedLoginException)19 LoginContext (javax.security.auth.login.LoginContext)18 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)18 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)17 Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 IdRepoException (com.sun.identity.idm.IdRepoException)13 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)11