Search in sources :

Example 76 with Callback

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

the class PersistModuleProcesserTest method shouldReturnLoginSucceedWithoutSavingProfileWhenUserChooseToNotSaveProfile.

@Test
public void shouldReturnLoginSucceedWithoutSavingProfileWhenUserChooseToNotSaveProfile() throws AuthLoginException {
    //Given
    processor = new PersistModuleProcessor(devicePrintProfile, false, profilePersister);
    ChoiceCallback choiceCallback = mock(ChoiceCallback.class);
    Callback[] callbacks = new Callback[] { choiceCallback };
    int state = SAVE_PROFILE_STATE;
    given(choiceCallback.getSelectedIndexes()).willReturn(new int[] { 1 });
    //When
    int newState = processor.process(callbacks, state);
    //Then
    assertThat(newState).isEqualTo(ISAuthConstants.LOGIN_SUCCEED);
    verifyZeroInteractions(profilePersister);
}
Also used : ChoiceCallback(javax.security.auth.callback.ChoiceCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) Test(org.testng.annotations.Test)

Example 77 with Callback

use of javax.security.auth.callback.Callback 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 78 with Callback

use of javax.security.auth.callback.Callback 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)

Example 79 with Callback

use of javax.security.auth.callback.Callback in project jdk8u_jdk by JetBrains.

the class TestSampleLoginModule method login.

/*
   * Authenticate the user by comparing the values of the java properties
   * (username and password) against the values of the credentials.
   * */
public boolean login() throws LoginException {
    String credentials_username = null;
    String credentials_password = null;
    String authenticated_username = System.getProperty("susername");
    String authenticated_password = System.getProperty("spassword");
    System.out.println("TestSampleLoginModule::login: Start");
    // First retreive the credentials {username, password} from
    // the callback handler
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("username");
    callbacks[1] = new PasswordCallback("password", false);
    try {
        callbackHandler.handle(callbacks);
        credentials_username = ((NameCallback) callbacks[0]).getName();
        credentials_password = new String(((PasswordCallback) callbacks[1]).getPassword());
    } catch (Exception e) {
        throw new LoginException(e.toString());
    }
    System.out.println("TestSampleLoginModule::login: credentials username = " + credentials_username);
    System.out.println("TestSampleLoginModule::login: credentials password = " + credentials_password);
    System.out.println("TestSampleLoginModule::login: authenticated username = " + authenticated_username);
    System.out.println("TestSampleLoginModule::login: authenticated password = " + authenticated_password);
    if (credentials_username.equals(authenticated_username) && credentials_password.equals(authenticated_password)) {
        System.out.println("TestSampleLoginModule::login: " + "Authentication should succeed");
        return true;
    } else {
        System.out.println("TestSampleLoginModule::login: " + "Authentication should reject");
        throw new LoginException("TestSampleLoginModule throws EXCEPTION");
    }
}
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) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) LoginException(javax.security.auth.login.LoginException)

Example 80 with Callback

use of javax.security.auth.callback.Callback in project jdk8u_jdk by JetBrains.

the class AuthRealmChoices method main.

public static void main(String[] args) throws Exception {
    Map props = new HashMap();
    props.put("com.sun.security.sasl.digest.realm", "IMC.ORG foo.bar machineX");
    SaslClient clnt = Sasl.createSaslClient(new String[] { MECH }, null, PROTOCOL, SERVER_FQDN, null, new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback cb : callbacks) {
                if (cb instanceof RealmChoiceCallback) {
                    // 2. No index set at all
                    if (args[0].equals("1")) {
                        ((RealmChoiceCallback) cb).setSelectedIndex(10);
                    }
                }
            }
        }
    });
    SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, props, new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback cb : callbacks) {
                System.out.println(cb);
            }
        }
    });
    byte[] challenge = srv.evaluateResponse(EMPTY);
    try {
        clnt.evaluateChallenge(challenge);
        throw new Exception();
    } catch (SaslException se) {
        System.out.println(se);
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) Callback(javax.security.auth.callback.Callback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Callback (javax.security.auth.callback.Callback)274 NameCallback (javax.security.auth.callback.NameCallback)181 PasswordCallback (javax.security.auth.callback.PasswordCallback)173 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)127 IOException (java.io.IOException)84 LoginException (javax.security.auth.login.LoginException)54 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)49 CallbackHandler (javax.security.auth.callback.CallbackHandler)43 Test (org.testng.annotations.Test)42 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)38 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)31 Subject (javax.security.auth.Subject)31 HashMap (java.util.HashMap)28 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 Principal (java.security.Principal)23 AuthContext (com.sun.identity.authentication.AuthContext)22 FailedLoginException (javax.security.auth.login.FailedLoginException)22 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)19 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)19 LoginContext (javax.security.auth.login.LoginContext)18