Search in sources :

Example 21 with NameCallback

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

the class RestAuthNameCallbackHandlerTest method shouldHandleCallback.

@Test
public void shouldHandleCallback() {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    JsonValue jsonPostBody = mock(JsonValue.class);
    NameCallback originalNameCallback = mock(NameCallback.class);
    //When
    NameCallback nameCallback = restAuthNameCallbackHandler.handle(request, response, jsonPostBody, originalNameCallback);
    //Then
    assertEquals(originalNameCallback, nameCallback);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NameCallback(javax.security.auth.callback.NameCallback) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test)

Example 22 with NameCallback

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

the class RestAuthNameCallbackHandlerTest method shouldUpdateCallbackFromRequest.

@Test
public void shouldUpdateCallbackFromRequest() throws RestAuthResponseException, RestAuthException {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    NameCallback nameCallback = mock(NameCallback.class);
    given(request.getHeader("X-OpenAM-Username")).willReturn("USERNAME");
    //When
    boolean updated = restAuthNameCallbackHandler.updateCallbackFromRequest(request, response, nameCallback);
    //Then
    verify(nameCallback).setName("USERNAME");
    assertTrue(updated);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NameCallback(javax.security.auth.callback.NameCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test)

Example 23 with NameCallback

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

the class RestAuthNameCallbackHandlerTest method shouldFailToConvertFromJsonWithInvalidType.

@Test(expectedExceptions = RestAuthException.class)
public void shouldFailToConvertFromJsonWithInvalidType() throws RestAuthException {
    //Given
    NameCallback nameCallback = new NameCallback("Enter username:");
    JsonValue jsonNameCallback = JsonValueBuilder.jsonValue().array("input").addLast(JsonValueBuilder.jsonValue().put("value", "USERNAME").build()).array("output").addLast(JsonValueBuilder.jsonValue().put("value", "Enter username:").build()).put("type", "PasswordCallback").build();
    //When
    restAuthNameCallbackHandler.convertFromJson(nameCallback, jsonNameCallback);
    //Then
    fail();
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 24 with NameCallback

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

the class IdServicesImpl method authenticate.

/**
    * Returns <code>true</code> if the data store has successfully
    * authenticated the identity with the provided credentials. In case the
    * data store requires additional credentials, the list would be returned
    * via the <code>IdRepoException</code> exception.
    *
    * @param orgName
    *            realm name to which the identity would be authenticated
    * @param credentials
    *            Array of callback objects containing information such as
    *            username and password.
    *
    * @return <code>true</code> if data store authenticates the identity;
    *         else <code>false</code>
    */
public boolean authenticate(String orgName, Callback[] credentials) throws IdRepoException, AuthLoginException {
    if (DEBUG.messageEnabled()) {
        DEBUG.message("IdServicesImpl.authenticate: called for org: " + orgName);
    }
    IdRepoException firstException = null;
    AuthLoginException authException = null;
    // Get the list of plugins and check if they support authN
    Set cPlugins = null;
    try {
        cPlugins = idrepoCache.getIdRepoPlugins(orgName);
    } catch (SSOException ex) {
        // Debug the message and return false
        if (DEBUG.messageEnabled()) {
            DEBUG.message("IdServicesImpl.authenticate: " + "Error obtaining " + "IdRepo plugins for the org: " + orgName);
        }
        return (false);
    } catch (IdRepoException ex) {
        // Debug the message and return false
        if (DEBUG.messageEnabled()) {
            DEBUG.message("IdServicesImpl.authenticate: " + "Error obtaining " + "IdRepo plugins for the org: " + orgName);
        }
        return (false);
    }
    // Check for internal user. If internal user, use SpecialRepo only
    String name = null;
    for (int i = 0; i < credentials.length; i++) {
        if (credentials[i] instanceof NameCallback) {
            name = ((NameCallback) credentials[i]).getName();
            if (LDAPUtils.isDN(name)) {
                // Obtain the firsr RDN
                name = LDAPUtils.rdnValueFromDn(name);
            }
            break;
        }
    }
    SSOToken token = (SSOToken) AccessController.doPrivileged(AdminTokenAction.getInstance());
    try {
        if ((name != null) && isSpecialIdentity(token, name, IdType.USER, orgName)) {
            for (Iterator tis = cPlugins.iterator(); tis.hasNext(); ) {
                IdRepo idRepo = (IdRepo) tis.next();
                if (idRepo.getClass().getName().equals(IdConstants.SPECIAL_PLUGIN)) {
                    if (idRepo.authenticate(credentials)) {
                        if (DEBUG.messageEnabled()) {
                            DEBUG.message("IdServicesImpl.authenticate: " + "AuthN success using special repo " + idRepo.getClass().getName() + " user: " + name);
                        }
                        return (true);
                    } else {
                        // Invalid password used for internal user
                        DEBUG.error("IdServicesImpl.authenticate: " + "AuthN failed using special repo " + idRepo.getClass().getName() + " user: " + name);
                        return (false);
                    }
                }
            }
        }
    } catch (SSOException ssoe) {
        // Ignore the exception
        DEBUG.error("IdServicesImpl.authenticate: AuthN failed " + "checking for special users", ssoe);
        return (false);
    }
    for (Iterator items = cPlugins.iterator(); items.hasNext(); ) {
        IdRepo idRepo = (IdRepo) items.next();
        if (idRepo.supportsAuthentication()) {
            if (DEBUG.messageEnabled()) {
                DEBUG.message("IdServicesImpl.authenticate: " + "AuthN to " + idRepo.getClass().getName() + " in org: " + orgName);
            }
            try {
                if (idRepo.authenticate(credentials)) {
                    // Successfully authenticated
                    if (DEBUG.messageEnabled()) {
                        DEBUG.message("IdServicesImpl.authenticate: " + "AuthN success for " + idRepo.getClass().getName());
                    }
                    return (true);
                }
            } catch (IdRepoException ide) {
                // all authentication calls fail
                if (firstException == null) {
                    firstException = ide;
                }
            } catch (AuthLoginException authex) {
                if (authException == null) {
                    authException = authex;
                }
            }
        } else if (DEBUG.messageEnabled()) {
            DEBUG.message("IdServicesImpl.authenticate: AuthN " + "not supported by " + idRepo.getClass().getName());
        }
    }
    if (authException != null) {
        throw (authException);
    }
    if (firstException != null) {
        throw (firstException);
    }
    return (false);
}
Also used : Set(java.util.Set) OrderedSet(com.sun.identity.shared.datastruct.OrderedSet) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) NameCallback(javax.security.auth.callback.NameCallback) SSOToken(com.iplanet.sso.SSOToken) IdRepo(com.sun.identity.idm.IdRepo) IdRepoException(com.sun.identity.idm.IdRepoException) Iterator(java.util.Iterator) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) SSOException(com.iplanet.sso.SSOException)

Example 25 with NameCallback

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

the class AuthUtils method authenticate.

public static SSOToken authenticate(String realm, String userName, String password) throws Exception {
    AuthContext lc = new AuthContext(realm);
    lc.login();
    while (lc.hasMoreRequirements()) {
        Callback[] callbacks = lc.getRequirements();
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                NameCallback nc = (NameCallback) callbacks[i];
                nc.setName(userName);
            } else if (callbacks[i] instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callbacks[i];
                pc.setPassword(password.toCharArray());
            } else {
                throw new Exception("No callback");
            }
        }
        lc.submitRequirements(callbacks);
    }
    return (lc.getStatus() != AuthContext.Status.SUCCESS) ? null : lc.getSSOToken();
}
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) AuthContext(com.sun.identity.authentication.AuthContext) PasswordCallback(javax.security.auth.callback.PasswordCallback)

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