Search in sources :

Example 36 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class UserPrivilegeTest method createSessionToken.

private SSOToken createSessionToken(String orgName, String userId, String password, String module, int level) throws Exception {
    AuthContext ac = null;
    try {
        ac = new AuthContext(orgName);
        if (module != null) {
            ac.login(AuthContext.IndexType.MODULE_INSTANCE, module);
        } else if (level != -1) {
            ac.login(AuthContext.IndexType.LEVEL, String.valueOf(level));
        } else {
            ac.login();
        }
    } catch (LoginException le) {
        le.printStackTrace();
        return null;
    }
    try {
        Callback[] callbacks = null;
        // Get the information requested by the plug-ins
        if (ac.hasMoreRequirements()) {
            callbacks = ac.getRequirements();
            if (callbacks != null) {
                addLoginCallbackMessage(callbacks, userId, password);
                ac.submitRequirements(callbacks);
                if (ac.getStatus() == AuthContext.Status.SUCCESS) {
                    //System.out.println("Auth success");
                    Subject authSubject = ac.getSubject();
                    if (authSubject != null) {
                        Iterator principals = (authSubject.getPrincipals()).iterator();
                        Principal principal;
                        while (principals.hasNext()) {
                            principal = (Principal) principals.next();
                        }
                    }
                } else if (ac.getStatus() == AuthContext.Status.FAILED) {
                //System.out.println("Authentication has FAILED");
                } else {
                }
            } else {
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //System.out.println(ac.getSSOToken().getPrincipal().getName());
    return ac.getSSOToken();
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) Iterator(java.util.Iterator) AuthContext(com.sun.identity.authentication.AuthContext) LoginException(javax.security.auth.login.LoginException) Subject(javax.security.auth.Subject) Principal(java.security.Principal) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 37 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class PolicyEvaluatorTest method login.

private SSOToken login() throws Exception {
    lc = new AuthContext("/");
    AuthContext.IndexType indexType = AuthContext.IndexType.MODULE_INSTANCE;
    lc.login(indexType, "DataStore");
    Callback[] callbacks = null;
    // get information requested from module
    while (lc.hasMoreRequirements()) {
        callbacks = lc.getRequirements();
        if (callbacks != null) {
            addLoginCallbackMessage(callbacks);
            lc.submitRequirements(callbacks);
        }
    }
    return (lc.getStatus() == AuthContext.Status.SUCCESS) ? lc.getSSOToken() : null;
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) AuthContext(com.sun.identity.authentication.AuthContext)

Example 38 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class ClientAuthenticatorImpl method authenticate.

/**
     * Perform the authentication of the client using the specified client credentials.
     *
     * @param clientId The client's id.
     * @param clientSecret The client's secret.
     * @param realm The realm the client exists in.
     * @return {@code true} if the client was authenticated successfully.
     * @throws InvalidClientException If the authentication configured for the client is not completed by the
     *          specified client credentials.
     */
private boolean authenticate(OAuth2Request request, String clientId, char[] clientSecret, String realm) throws InvalidClientException {
    try {
        AuthContext lc = new AuthContext(realm);
        lc.login(AuthContext.IndexType.MODULE_INSTANCE, "Application");
        while (lc.hasMoreRequirements()) {
            Callback[] callbacks = lc.getRequirements();
            List<Callback> missing = new ArrayList<Callback>();
            // loop through the requires setting the needs..
            for (final Callback callback : callbacks) {
                if (callback instanceof NameCallback) {
                    NameCallback nc = (NameCallback) callback;
                    nc.setName(clientId);
                } else if (callback instanceof PasswordCallback) {
                    PasswordCallback pc = (PasswordCallback) callback;
                    pc.setPassword(clientSecret);
                } else {
                    missing.add(callback);
                }
            }
            // there's missing requirements not filled by this
            if (missing.size() > 0) {
                lc.logout();
                throw failureFactory.getException(request, "Missing requirements");
            }
            lc.submitRequirements(callbacks);
        }
        // validate the password..
        if (lc.getStatus() == AuthContext.Status.SUCCESS) {
            lc.logout();
            return true;
        } else {
            throw failureFactory.getException(request, "Client authentication failed");
        }
    } catch (AuthLoginException le) {
        logger.error("ClientVerifierImpl::authContext AuthException", le);
        throw failureFactory.getException(request, "Client authentication failed");
    }
}
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) ArrayList(java.util.ArrayList) AuthContext(com.sun.identity.authentication.AuthContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException)

Example 39 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class OpenAMResourceOwnerAuthenticator method authenticate.

private ResourceOwner authenticate(String username, char[] password, String realm, String service) {
    ResourceOwner ret = null;
    AuthContext lc = null;
    try {
        lc = new AuthContext(realm);
        if (service != null) {
            lc.login(AuthContext.IndexType.SERVICE, service, null, ServletUtils.getRequest(Request.getCurrent()), ServletUtils.getResponse(Response.getCurrent()));
        } else {
            lc.login(ServletUtils.getRequest(Request.getCurrent()), ServletUtils.getResponse(Response.getCurrent()));
        }
        while (lc.hasMoreRequirements()) {
            Callback[] callbacks = lc.getRequirements();
            ArrayList missing = new ArrayList();
            // loop through the requires setting the needs..
            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);
                } else {
                    missing.add(callbacks[i]);
                }
            }
            // there's missing requirements not filled by this
            if (missing.size() > 0) {
                throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Missing requirements");
            }
            lc.submitRequirements(callbacks);
        }
        // validate the password..
        if (lc.getStatus() == AuthContext.Status.SUCCESS) {
            try {
                // package up the token for transport..
                ret = createResourceOwner(lc);
            } catch (Exception e) {
                logger.error("Unable to get SSOToken", e);
                // because the system is likely down..
                throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
            }
        }
    } catch (AuthLoginException le) {
        logger.error("AuthException", le);
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, le);
    } finally {
        if (lc != null && AuthContext.Status.SUCCESS.equals(lc.getStatus())) {
            try {
                lc.logout();
                logger.message("Logged user out.");
            } catch (AuthLoginException e) {
                logger.error("Exception caught logging out of AuthContext after successful login", e);
            }
        }
    }
    return ret;
}
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) ResourceOwner(org.forgerock.oauth2.core.ResourceOwner) ArrayList(java.util.ArrayList) AuthContext(com.sun.identity.authentication.AuthContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) ResourceException(org.restlet.resource.ResourceException) IdRepoException(com.sun.identity.idm.IdRepoException) ResourceException(org.restlet.resource.ResourceException) ParseException(java.text.ParseException) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) SSOException(com.iplanet.sso.SSOException)

Example 40 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class TokenUtils method getSessionToken.

public static SSOToken getSessionToken(String orgName, String userId, String password, String module, int level) throws Exception {
    AuthContext ac = null;
    try {
        //System.out.println("TokenUtils:orgName=" + orgName);
        ac = new AuthContext(orgName);
        if (module != null) {
            ac.login(AuthContext.IndexType.MODULE_INSTANCE, module);
        } else if (level != -1) {
            ac.login(AuthContext.IndexType.LEVEL, String.valueOf(level));
        } else {
            //System.out.println("TokenUtils:calling login()");
            ac.login();
        }
    //System.out.println("TokenUtils:after ac.login()");
    } catch (LoginException le) {
        le.printStackTrace();
        return null;
    }
    try {
        Callback[] callbacks = null;
        // Get the information requested by the plug-ins
        if (ac.hasMoreRequirements()) {
            callbacks = ac.getRequirements();
            if (callbacks != null) {
                addLoginCallbackMessage(callbacks, userId, password);
                ac.submitRequirements(callbacks);
                if (ac.getStatus() == AuthContext.Status.SUCCESS) {
                    //System.out.println("Auth success");
                    Subject authSubject = ac.getSubject();
                    if (authSubject != null) {
                        Iterator principals = (authSubject.getPrincipals()).iterator();
                        Principal principal;
                        while (principals.hasNext()) {
                            principal = (Principal) principals.next();
                        }
                    }
                } else if (ac.getStatus() == AuthContext.Status.FAILED) {
                //System.out.println("Authentication has FAILED");
                } else {
                }
            } else {
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //System.out.println(ac.getSSOToken().getPrincipal().getName());
    return ac.getSSOToken();
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) Iterator(java.util.Iterator) AuthContext(com.sun.identity.authentication.AuthContext) LoginException(javax.security.auth.login.LoginException) Subject(javax.security.auth.Subject) Principal(java.security.Principal) AuthPrincipal(com.sun.identity.authentication.internal.AuthPrincipal) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

AuthContext (com.sun.identity.authentication.AuthContext)40 Callback (javax.security.auth.callback.Callback)22 NameCallback (javax.security.auth.callback.NameCallback)21 PasswordCallback (javax.security.auth.callback.PasswordCallback)20 SSOToken (com.iplanet.sso.SSOToken)14 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)14 SSOException (com.iplanet.sso.SSOException)12 LoginException (javax.security.auth.login.LoginException)8 Iterator (java.util.Iterator)7 Set (java.util.Set)7 IdRepoException (com.sun.identity.idm.IdRepoException)6 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Subject (javax.security.auth.Subject)5 Principal (java.security.Principal)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 SSOTokenManager (com.iplanet.sso.SSOTokenManager)3 LoginState (com.sun.identity.authentication.service.LoginState)3