Search in sources :

Example 16 with ChoiceCallback

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

the class DevicePrintAuthenticationServiceTest method shouldCreateUserProfileWhenNoValidPreviousProfiles.

/**
     * 8) fourth call, 3 - save profile, having no valid previous profiles - should create new profile, return ISAuthConstants.LOGIN_SUCCEED
     */
@Test
public void shouldCreateUserProfileWhenNoValidPreviousProfiles() throws AuthLoginException {
    //Given
    NameCallback devicePrintCallback = mock(NameCallback.class);
    Callback[] callbacks = new Callback[] { devicePrintCallback };
    int state = ISAuthConstants.LOGIN_START;
    DevicePrint devicePrint = mock(DevicePrint.class);
    given(devicePrintService.getDevicePrint(request)).willReturn(devicePrint);
    given(devicePrintService.hasRequiredAttributes(devicePrint)).willReturn(false);
    devicePrintAuthenticationService.process(callbacks, state);
    callbacks = new Callback[1];
    ChoiceCallback choiceCallback = mock(ChoiceCallback.class);
    state = 3;
    callbacks[0] = choiceCallback;
    given(choiceCallback.getSelectedIndexes()).willReturn(new int[] { 0 });
    //When
    int nextState = devicePrintAuthenticationService.process(callbacks, state);
    //Then
    verify(devicePrintService).createNewProfile(devicePrint);
    assertEquals(nextState, ISAuthConstants.LOGIN_SUCCEED);
}
Also used : ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) Test(org.testng.annotations.Test)

Example 17 with ChoiceCallback

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

the class DevicePrintAuthenticationServiceTest method shouldNotSaveUsersProfile.

/**
     * 7) fourth call, 3 - don't save profile - should return ISAuthConstants.LOGIN_SUCCEED, with no profile saved
     */
@Test
public void shouldNotSaveUsersProfile() throws AuthLoginException {
    //Given
    Callback[] callbacks = new Callback[1];
    ChoiceCallback choiceCallback = mock(ChoiceCallback.class);
    int state = 3;
    callbacks[0] = choiceCallback;
    given(choiceCallback.getSelectedIndexes()).willReturn(new int[] { 1 });
    //When
    int nextState = devicePrintAuthenticationService.process(callbacks, state);
    //Then
    verifyZeroInteractions(devicePrintService);
    assertEquals(nextState, ISAuthConstants.LOGIN_SUCCEED);
}
Also used : ChoiceCallback(javax.security.auth.callback.ChoiceCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) DevicePrint(org.forgerock.openam.authentication.modules.deviceprint.model.DevicePrint) Test(org.testng.annotations.Test)

Example 18 with ChoiceCallback

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

the class Membership method getAndCheckRegistrationFields.

/**
     * Returns and checks registration fields. Returns error state for none 
     * recoverable errors, REGISTRATION for recoverable errors and DISCLAIMER if
     * completed.
     */
private ModuleState getAndCheckRegistrationFields(Callback[] callbacks) throws AuthLoginException {
    // callback[0] is for user name
    // callback[1] is for new password
    // callback[2] is for confirm password        
    Map<String, Set<String>> attrs = new HashMap<String, Set<String>>();
    // get the value of the user name from the input form
    userID = getCallbackFieldValue(callbacks[0]);
    // check user name
    if ((userID == null) || userID.length() == 0) {
        // no user name was entered, this is required to
        // create the user's profile
        updateRegistrationCallbackFields(callbacks);
        setErrorMessage(RegistrationResult.NO_USER_NAME_ERROR, 0);
        return ModuleState.REGISTRATION;
    }
    //validate username using plugin if any
    validateUserName(userID, regEx);
    // get the passwords from the input form
    String password = getPassword((PasswordCallback) callbacks[1]);
    String confirmPassword = getPassword((PasswordCallback) callbacks[2]);
    // check passwords
    RegistrationResult checkPasswdResult = checkPassword(password, confirmPassword);
    if (debug.messageEnabled()) {
        debug.message("state returned from checkPassword(): " + checkPasswdResult);
    }
    if (!checkPasswdResult.equals(RegistrationResult.NO_ERROR)) {
        // the next state to display is returned from checkPassword
        updateRegistrationCallbackFields(callbacks);
        setErrorMessage(checkPasswdResult, 1);
        return ModuleState.REGISTRATION;
    }
    // validate password using validation plugin if any
    validatePassword(confirmPassword);
    if (password.equals(userID)) {
        // the user name and password are the same. these fields
        // must be different
        updateRegistrationCallbackFields(callbacks);
        setErrorMessage(RegistrationResult.USER_PASSWORD_SAME_ERROR, 1);
        return ModuleState.REGISTRATION;
    }
    // get the registration fields, also check required fields
    for (int i = 0; i < callbacks.length; i++) {
        String attrName = getAttribute(ModuleState.REGISTRATION.intValue(), i);
        Set<String> values = getCallbackFieldValues(callbacks[i]);
        if (isRequired(ModuleState.REGISTRATION.intValue(), i)) {
            if (values.isEmpty()) {
                if (debug.messageEnabled()) {
                    debug.message("Empty value for required field :" + attrName);
                }
                updateRegistrationCallbackFields(callbacks);
                setErrorMessage(RegistrationResult.MISSING_REQ_FIELD_ERROR, i);
                return ModuleState.REGISTRATION;
            }
        }
        if (attrName != null && attrName.length() != 0) {
            attrs.put(attrName, values);
        }
    }
    userAttrs = attrs;
    // check user ID uniqueness
    try {
        if (userExists(userID)) {
            if (debug.messageEnabled()) {
                debug.message("user ID " + userID + " already exists");
            }
            // get a list of user IDs from the generator
            Set generatedUserIDs = getNewUserIDs(attrs, 0);
            if (generatedUserIDs == null) {
                // user name generator is disable
                updateRegistrationCallbackFields(callbacks);
                setErrorMessage(RegistrationResult.USER_EXISTS_ERROR, 0);
                return ModuleState.REGISTRATION;
            }
            // get a list of user IDs that are not yet being used
            List<String> nonExistingUserIDs = getNonExistingUserIDs(generatedUserIDs);
            resetCallback(ModuleState.CHOOSE_USERNAMES.intValue(), 0);
            Callback[] origCallbacks = getCallback(ModuleState.CHOOSE_USERNAMES.intValue());
            ChoiceCallback origCallback = (ChoiceCallback) origCallbacks[0];
            String prompt = origCallback.getPrompt();
            createMyOwn = origCallback.getChoices()[0];
            nonExistingUserIDs.add(createMyOwn);
            String[] choices = ((String[]) nonExistingUserIDs.toArray(new String[0]));
            ChoiceCallback callback = new ChoiceCallback(prompt, choices, 0, false);
            callback.setSelectedIndex(0);
            replaceCallback(ModuleState.CHOOSE_USERNAMES.intValue(), 0, callback);
            return ModuleState.CHOOSE_USERNAMES;
        }
    } catch (SSOException pe) {
        debug.error("profile exception occured: ", pe);
        return ModuleState.PROFILE_ERROR;
    } catch (IdRepoException pe) {
        debug.error("profile exception occured: ", pe);
        return ModuleState.PROFILE_ERROR;
    }
    return ModuleState.DISCLAIMER;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback)

Example 19 with ChoiceCallback

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

the class RestAuthChoiceCallbackHandlerTest method shouldNotFailToConvertFromJsonWithTypeLowerCase.

@Test
public void shouldNotFailToConvertFromJsonWithTypeLowerCase() throws RestAuthException {
    //Given
    ChoiceCallback choiceCallback = new ChoiceCallback("Select choice:", new String[] { "1", "34", "66", "93" }, 0, true);
    JsonValue jsonNameCallback = json(object(field("input", array(object(field("value", 1)))), field("output", array(object(field("value", "Select choice:")), object(field("value", array("1", "34", "66", "93"))), object(field("value", "0")))), field("type", "choicecallback")));
    System.out.println("shouldNotFailToConvertFromJsonWithTypeLowerCase");
    System.out.println(jsonNameCallback.toString());
    //When
    ChoiceCallback convertedChoiceCallback = restAuthChoiceCallbackHandler.convertFromJson(choiceCallback, jsonNameCallback);
    //Then
    assertEquals(choiceCallback, convertedChoiceCallback);
    assertEquals("Select choice:", convertedChoiceCallback.getPrompt());
    assertEquals(new String[] { "1", "34", "66", "93" }, convertedChoiceCallback.getChoices());
    assertEquals(0, convertedChoiceCallback.getDefaultChoice());
    assertEquals(new int[] { 1 }, convertedChoiceCallback.getSelectedIndexes());
}
Also used : ChoiceCallback(javax.security.auth.callback.ChoiceCallback) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 20 with ChoiceCallback

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

the class RestAuthChoiceCallbackHandlerTest method shouldFailToConvertFromJsonWithInvalidType.

@Test(expectedExceptions = RestAuthException.class)
public void shouldFailToConvertFromJsonWithInvalidType() throws RestAuthException {
    //Given
    ChoiceCallback choiceCallback = new ChoiceCallback("Select choice:", new String[] { "1", "34", "66", "93" }, 0, true);
    JsonValue jsonNameCallback = json(object(field("input", array(object(field("value", 1)))), field("output", array(object(field("value", "Select choice:")), object(field("value", array("1", "34", "66", "93"))), object(field("value", "0")))), field("type", "PasswordCallback")));
    System.out.println("shouldFailToConvertFromJsonWithInvalidType");
    System.out.println(jsonNameCallback.toString());
    //When
    restAuthChoiceCallbackHandler.convertFromJson(choiceCallback, jsonNameCallback);
    //Then
    fail();
}
Also used : ChoiceCallback(javax.security.auth.callback.ChoiceCallback) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Aggregations

ChoiceCallback (javax.security.auth.callback.ChoiceCallback)32 Callback (javax.security.auth.callback.Callback)19 Test (org.testng.annotations.Test)17 NameCallback (javax.security.auth.callback.NameCallback)15 PasswordCallback (javax.security.auth.callback.PasswordCallback)13 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)12 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)9 JsonValue (org.forgerock.json.JsonValue)8 HttpCallback (com.sun.identity.authentication.spi.HttpCallback)5 RedirectCallback (com.sun.identity.authentication.spi.RedirectCallback)5 TextInputCallback (javax.security.auth.callback.TextInputCallback)5 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)5 SSOException (com.iplanet.sso.SSOException)4 HiddenValueCallback (com.sun.identity.authentication.callbacks.HiddenValueCallback)4 ScriptTextOutputCallback (com.sun.identity.authentication.callbacks.ScriptTextOutputCallback)4 ArrayList (java.util.ArrayList)4 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 LanguageCallback (javax.security.auth.callback.LanguageCallback)3