Search in sources :

Example 6 with SSOTokenManager

use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.

the class CommandLineSSO method main.

public static void main(String[] args) throws Exception {
    String orgName = args[0];
    System.out.println("Organization: " + orgName);
    SSOTokenManager manager = SSOTokenManager.getInstance();
    AuthContext lc = getAuthcontext(orgName);
    if (lc.getStatus() == AuthContext.Status.SUCCESS) {
        System.out.println("Successful authentication ...");
        SSOToken token = lc.getSSOToken();
        String userDN = token.getPrincipal().getName();
        System.out.println("User Name: " + userDN);
        try {
            AMIdentity userIdentity = IdUtils.getIdentity(token);
            Map attrs = userIdentity.getAttributes();
            System.out.println("User Attributes: ");
            for (Iterator i = attrs.keySet().iterator(); i.hasNext(); ) {
                String attrName = (String) i.next();
                Set values = (Set) attrs.get(attrName);
                System.out.println(attrName + "=" + values);
            }
        } catch (IdRepoException e) {
            e.printStackTrace();
        } finally {
            manager.destroyToken(token);
        }
    } else {
        System.out.println("Authentication Failed ....... ");
    }
    System.exit(0);
}
Also used : SSOTokenManager(com.iplanet.sso.SSOTokenManager) SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) AMIdentity(com.sun.identity.idm.AMIdentity) Iterator(java.util.Iterator) IdRepoException(com.sun.identity.idm.IdRepoException) AuthContext(com.sun.identity.authentication.AuthContext) Map(java.util.Map)

Example 7 with SSOTokenManager

use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.

the class SSOTokenSampleServlet method doGet.

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    ServletOutputStream out = null;
    try {
        try {
            response.setContentType("text/html");
            out = response.getOutputStream();
            // create the sso token from http request 
            SSOTokenManager manager = SSOTokenManager.getInstance();
            SSOToken token = manager.createSSOToken(request);
            if (manager.isValidToken(token)) {
                //print some of the values from the token.
                String host = token.getHostName();
                java.security.Principal principal = token.getPrincipal();
                String authType = token.getAuthType();
                int level = token.getAuthLevel();
                InetAddress ipAddress = token.getIPAddress();
                out.println("SSOToken host name: " + host);
                out.println("<br />");
                out.println("SSOToken Principal name: " + principal.getName());
                out.println("<br />");
                out.println("Authentication type used: " + authType);
                out.println("<br />");
                out.println("IPAddress of the host: " + ipAddress.getHostAddress());
                out.println("<br />");
            }
            /* Validate the token again, with another method.
                 * if token is invalid, this method throws exception
                 */
            manager.validateToken(token);
            out.println("SSO Token validation test succeeded");
            out.println("<br />");
            // Get the SSOTokenID associated with the token and print it.
            SSOTokenID tokenId = token.getTokenID();
            out.println("The token id is " + tokenId.toString());
            out.println("<br />");
            // Set and get some properties in the token.
            token.setProperty("Company", "Sun Microsystems");
            token.setProperty("Country", "USA");
            String name = token.getProperty("Company");
            String country = token.getProperty("Country");
            out.println("Property: Company: " + name);
            out.println("<br />");
            out.println("Property: Country: " + country);
            out.println("<br />");
            // Retrieve user profile and print them
            AMIdentity userIdentity = IdUtils.getIdentity(token);
            Map attrs = userIdentity.getAttributes();
            out.println("User Attributes: " + attrs);
            /* let us add a listener to the SSOToken. Whenever a token
                 * event arrives, ssoTokenChanged method of the listener will
                 * get called.
                 */
            SSOTokenListener myListener = new SampleTokenListener();
            token.addSSOTokenListener(myListener);
        } catch (SSOException e) {
            out.println("SSO Exception: " + e);
            out.println("<p>Authenticate to OpenAM server before visiting this page.</p>");
            e.printStackTrace();
        } catch (IdRepoException e) {
            out.println("IdRepo Exception: " + e);
            e.printStackTrace();
        } catch (IOException e) {
            out.println("IO Exception: " + e);
            e.printStackTrace();
        } finally {
            out.flush();
        }
    } catch (IOException e) {
    // ignored
    }
}
Also used : SSOTokenManager(com.iplanet.sso.SSOTokenManager) SSOTokenID(com.iplanet.sso.SSOTokenID) SSOToken(com.iplanet.sso.SSOToken) ServletOutputStream(javax.servlet.ServletOutputStream) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) IOException(java.io.IOException) SSOTokenListener(com.iplanet.sso.SSOTokenListener) AMIdentity(com.sun.identity.idm.AMIdentity) InetAddress(java.net.InetAddress) Map(java.util.Map)

Example 8 with SSOTokenManager

use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.

the class IdRepoSampleUtils method realmLogin.

public SSOToken realmLogin(String userid, String password, String realm) throws SSOException, AuthLoginException, Exception {
    SSOTokenManager mgr;
    String adminDN;
    String adminPassword;
    SSOToken ssoToken = null;
    AuthContext.IndexType authType = AuthContext.IndexType.MODULE_INSTANCE;
    try {
        lc = new AuthContext(realm);
    } catch (AuthLoginException le) {
        System.err.println("IdRepoSampleUtils: could not get AuthContext for realm " + realm);
        throw le;
    }
    try {
        lc.login();
    } catch (AuthLoginException le) {
        System.err.println("IdRepoSampleUtils: Failed to start login " + "for default authmodule");
        throw le;
    }
    userID = userid;
    Callback[] callbacks = null;
    Hashtable values = new Hashtable();
    values.put(AuthXMLTags.NAME_CALLBACK, userid);
    values.put(AuthXMLTags.PASSWORD_CALLBACK, password);
    while (lc.hasMoreRequirements()) {
        callbacks = lc.getRequirements();
        try {
            fillCallbacks(callbacks, values);
            lc.submitRequirements(callbacks);
        } catch (Exception e) {
            System.err.println("Failed to submit callbacks!");
            e.printStackTrace();
            return null;
        }
    }
    AuthContext.Status istat = lc.getStatus();
    if (istat == AuthContext.Status.SUCCESS) {
        System.out.println("==>Authentication SUCCESSFUL for user " + userid);
    } else if (istat == AuthContext.Status.COMPLETED) {
        System.out.println("==>Authentication Status for user " + userid + " = " + istat);
        return null;
    }
    try {
        ssoToken = lc.getSSOToken();
    } catch (Exception e) {
        System.err.println("Failed to get SSO token!  " + e.getMessage());
        throw e;
    }
    return ssoToken;
}
Also used : SSOTokenManager(com.iplanet.sso.SSOTokenManager) SSOToken(com.iplanet.sso.SSOToken) AuthContext(com.sun.identity.authentication.AuthContext) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException)

Example 9 with SSOTokenManager

use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.

the class OpenAMResourceOwnerAuthenticator method authenticate.

/**
     * {@inheritDoc}
     */
public ResourceOwner authenticate(OAuth2Request request, boolean useSession) throws NotFoundException {
    SSOToken token = null;
    try {
        SSOTokenManager mgr = SSOTokenManager.getInstance();
        token = mgr.createSSOToken(ServletUtils.getRequest(request.<Request>getRequest()));
    } catch (Exception e) {
        logger.warning("No SSO Token in request", e);
    }
    if (token == null || !useSession) {
        final String username = request.getParameter(USERNAME);
        final char[] password = request.getParameter(PASSWORD) == null ? null : request.<String>getParameter(PASSWORD).toCharArray();
        final String realm = realmNormaliser.normalise(request.<String>getParameter(OAuth2Constants.Custom.REALM));
        final String authChain = request.getParameter(AUTH_CHAIN);
        return authenticate(username, password, realm, authChain);
    } else {
        try {
            final AMIdentity id = IdUtils.getIdentity(AccessController.doPrivileged(AdminTokenAction.getInstance()), token.getProperty(Constants.UNIVERSAL_IDENTIFIER));
            long authTime = stringToDate(token.getProperty(ISAuthConstants.AUTH_INSTANT)).getTime();
            return new OpenAMResourceOwner(id.getName(), id, authTime);
        } catch (SSOException e) {
            logger.error("Unable to create ResourceOwner", e);
        } catch (ParseException e) {
            logger.error("Unable to create ResourceOwner", e);
        } catch (IdRepoException e) {
            logger.error("Unable to create ResourceOwner", e);
        }
    }
    return null;
}
Also used : SSOTokenManager(com.iplanet.sso.SSOTokenManager) SSOToken(com.iplanet.sso.SSOToken) AMIdentity(com.sun.identity.idm.AMIdentity) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) ParseException(java.text.ParseException) 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 10 with SSOTokenManager

use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.

the class RestUtils method hasPermission.

public static void hasPermission(final Context context) throws SSOException, IdRepoException, ForbiddenException {
    SSOTokenManager mgr = SSOTokenManager.getInstance();
    SSOToken ssotok = mgr.createSSOToken(getCookieFromServerContext(context));
    mgr.validateToken(ssotok);
    mgr.refreshSession(ssotok);
    AMIdentity amIdentity = new AMIdentity(ssotok);
    if (!(amIdentity.equals(AdminUserIdHolder.adminUserId))) {
        debug.error("Unauthorized user.");
        throw new ForbiddenException("Access Denied");
    }
}
Also used : SSOTokenManager(com.iplanet.sso.SSOTokenManager) ForbiddenException(org.forgerock.json.resource.ForbiddenException) SSOToken(com.iplanet.sso.SSOToken) AMIdentity(com.sun.identity.idm.AMIdentity)

Aggregations

SSOTokenManager (com.iplanet.sso.SSOTokenManager)53 SSOToken (com.iplanet.sso.SSOToken)48 SSOException (com.iplanet.sso.SSOException)39 IdRepoException (com.sun.identity.idm.IdRepoException)11 AMIdentity (com.sun.identity.idm.AMIdentity)9 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)8 IOException (java.io.IOException)7 Map (java.util.Map)6 Set (java.util.Set)6 ForbiddenException (org.forgerock.json.resource.ForbiddenException)6 SessionException (com.iplanet.dpro.session.SessionException)5 InternalSession (com.iplanet.dpro.session.service.InternalSession)5 AuthPrincipal (com.sun.identity.authentication.internal.AuthPrincipal)5 AuthException (com.sun.identity.authentication.service.AuthException)5 Iterator (java.util.Iterator)5 AuthContext (com.sun.identity.authentication.AuthContext)4 SMSException (com.sun.identity.sm.SMSException)4 Response (com.iplanet.services.comm.share.Response)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashMap (java.util.HashMap)3