Search in sources :

Example 6 with SessionID

use of com.iplanet.dpro.session.SessionID in project OpenAM by OpenRock.

the class AuthUtils method getAuthContext.

/**
     * Returns the authentication context for a request.
     *
     * @param request HTTP Servlet Request.
     * @param response HTTP Servlet Response.
     * @param sid SessionID for this request.
     * @param isSessionUpgrade <code>true</code> if session upgrade.
     * @param isBackPost <code>true</code> if back posting.
     * @param isLogout <code>true</code> for logout.
     * @return authentication context.
     */
public static AuthContextLocal getAuthContext(HttpServletRequest request, HttpServletResponse response, SessionID sid, boolean isSessionUpgrade, boolean isBackPost, boolean isLogout) throws AuthException {
    utilDebug.message("In AuthUtils:getAuthContext");
    Hashtable dataHash;
    AuthContextLocal authContext = null;
    LoginState loginState = null;
    // initialize auth service.
    AuthD ad = AuthD.getAuth();
    try {
        dataHash = parseRequestParameters(request);
        authContext = retrieveAuthContext(request, sid);
        if (utilDebug.messageEnabled()) {
            utilDebug.message("AuthUtil:getAuthContext:sid is.. .: " + sid);
            utilDebug.message("AuthUtil:getAuthContext:authContext is..: " + authContext);
        }
        if (!sid.isNull() && authContext == null && !isSessionUpgrade) {
            String authCookieValue = getAuthCookieValue(request);
            if ((authCookieValue != null) && (!authCookieValue.isEmpty()) && (!authCookieValue.equalsIgnoreCase("LOGOUT"))) {
                String cookieURL = null;
                try {
                    SessionID sessionID = new SessionID(authCookieValue);
                    URL sessionServerURL = SESSION_SERVICE_URL_SERVICE.getSessionServiceURL(sessionID);
                    cookieURL = sessionServerURL.getProtocol() + "://" + sessionServerURL.getHost() + ":" + Integer.toString(sessionServerURL.getPort()) + serviceURI;
                } catch (SessionException e) {
                    if (utilDebug.messageEnabled()) {
                        utilDebug.message("AuthUtils:getAuthContext():" + e.toString());
                    }
                }
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("AuthUtils:getAuthContext():" + "cookieURL : " + cookieURL);
                }
                if ((cookieURL != null) && (!cookieURL.isEmpty()) && (isLocalServer(cookieURL, true))) {
                    utilDebug.error("AuthUtils:getAuthContext(): " + "Invalid Session Timed out");
                    clearAllCookies(request, response);
                    throw new AuthException(AMAuthErrorCode.AUTH_TIMEOUT, null);
                }
            }
        }
        if (utilDebug.messageEnabled()) {
            utilDebug.message("isSessionUpgrade  :" + isSessionUpgrade);
            utilDebug.message("BACK with Request method POST : " + isBackPost);
        }
        if ((authContext == null) && (isLogout)) {
            return null;
        }
        if ((authContext == null) || (isSessionUpgrade) || (isBackPost)) {
            try {
                loginState = new LoginState();
                InternalSession oldSession = null;
                if (sid != null) {
                    oldSession = AuthD.getSession(sid);
                    loginState.setOldSession(oldSession);
                }
                if (isSessionUpgrade) {
                    loginState.setOldSession(oldSession);
                    loginState.setSessionUpgrade(isSessionUpgrade);
                } else if (isBackPost) {
                    loginState.setOldSession(oldSession);
                }
                authContext = loginState.createAuthContext(request, response, sid, dataHash);
                loginState.setForceAuth(Boolean.parseBoolean(request.getParameter(FORCE_AUTH)));
                authContext.setLoginState(loginState);
                String queryOrg = getQueryOrgName(request, getOrgParam(dataHash));
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("query org is .. : " + queryOrg);
                }
                loginState.setQueryOrg(queryOrg);
            } catch (AuthException ae) {
                utilDebug.message("Error creating AuthContextLocal : ");
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("Exception ", ae);
                }
                throw new AuthException(ae);
            }
        } else {
            utilDebug.message("getAuthContext: found existing request.");
            authContext = processAuthContext(authContext, request, response, dataHash, sid);
            loginState = getLoginState(authContext);
            loginState.setNewRequest(false);
        }
    } catch (Exception ee) {
        if (utilDebug.messageEnabled()) {
            utilDebug.message("Error creating AuthContextLocal : " + ee.getMessage());
        }
        throw new AuthException(ee);
    }
    return authContext;
}
Also used : Hashtable(java.util.Hashtable) InternalSession(com.iplanet.dpro.session.service.InternalSession) SessionException(com.iplanet.dpro.session.SessionException) SessionID(com.iplanet.dpro.session.SessionID) URL(java.net.URL) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) SSOException(com.iplanet.sso.SSOException) SMSException(com.sun.identity.sm.SMSException) SessionException(com.iplanet.dpro.session.SessionException) AuthContextLocal(com.sun.identity.authentication.server.AuthContextLocal)

Example 7 with SessionID

use of com.iplanet.dpro.session.SessionID in project OpenAM by OpenRock.

the class AuthUtils method getSidString.

// retrieve the sid from the LoginState object
public static String getSidString(AuthContextLocal authContext) throws AuthException {
    com.iplanet.dpro.session.service.InternalSession sess = null;
    String sidString = null;
    try {
        if (authContext != null) {
            LoginState loginState = authContext.getLoginState();
            if (loginState != null) {
                SessionID sid = loginState.getSid();
                if (sid != null) {
                    sidString = sid.toString();
                }
            }
        }
    } catch (Exception e) {
        if (utilDebug.messageEnabled()) {
            utilDebug.message("Error retreiving sid.. :" + e.getMessage());
        }
        // generates AUTH_ERROR
        throw new AuthException("noSid", new Object[] { e.getMessage() });
    }
    return sidString;
}
Also used : InternalSession(com.iplanet.dpro.session.service.InternalSession) SessionID(com.iplanet.dpro.session.SessionID) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) SSOException(com.iplanet.sso.SSOException) SMSException(com.sun.identity.sm.SMSException) SessionException(com.iplanet.dpro.session.SessionException)

Example 8 with SessionID

use of com.iplanet.dpro.session.SessionID in project OpenAM by OpenRock.

the class AuthUtils method clearAllCookies.

public static void clearAllCookies(HttpServletRequest request, HttpServletResponse response) {
    SessionID sid = new SessionID(request);
    Set<String> cookieDomainSet = getCookieDomainsForRequest(request);
    if (cookieDomainSet.isEmpty()) {
        clearAllCookiesByDomain(sid, null, request, response);
    } else {
        Iterator iter = cookieDomainSet.iterator();
        while (iter.hasNext()) {
            clearAllCookiesByDomain(sid, (String) iter.next(), request, response);
        }
    }
    clearlbCookie(request, response);
    clearHostUrlCookie(response);
}
Also used : Iterator(java.util.Iterator) SessionID(com.iplanet.dpro.session.SessionID)

Example 9 with SessionID

use of com.iplanet.dpro.session.SessionID in project OpenAM by OpenRock.

the class AuthUtils method getAuthContext.

/* create auth context for org  and sid, if sessionupgrade then
     * save the previous authcontext and create new authcontext
     * orgName - organization name to login too
     * sessionId - sessionID of the request - "0" if new request
     * isLogout - is this a logout request - if yes then no session
     * upgrade  - this is the case where session is VALID so need
     * to use this flag to determine if session upgrade is needed.
     * this is used mainly for Logout/Abort.
     *  @param orgName OrganizationName in request
     *  @param sessionID Session ID for this request
     *  @param isLogout a boolean which is true if it is a Logout request
     *  @param req HttpServletRequest
     *  @param indexType Index Type
     *  @param indexName Index Name
     *  @param forceAuth force auth flag
     *  @return AuthContextLocal object
     */
public static AuthContextLocal getAuthContext(String orgName, String sessionID, boolean isLogout, HttpServletRequest req, String indexType, AuthXMLRequest xmlReq, boolean forceAuth) throws AuthException {
    AuthContextLocal authContext = null;
    SessionID sid = null;
    com.iplanet.dpro.session.service.InternalSession sess = null;
    LoginState loginState = null;
    boolean sessionUpgrade = false;
    AuthD ad = AuthD.getAuth();
    int sessionState = -1;
    SSOToken ssot = null;
    String indexName = null;
    if (xmlReq != null) {
        indexName = xmlReq.getIndexName();
    }
    if (utilDebug.messageEnabled()) {
        utilDebug.message("orgName : " + orgName);
        utilDebug.message("sessionID is " + sessionID);
        utilDebug.message("sessionID is " + sessionID.length());
        utilDebug.message("isLogout : " + isLogout);
    }
    try {
        if ((sessionID != null) && (!sessionID.equals("0"))) {
            sid = new SessionID(sessionID);
            authContext = retrieveAuthContext(req, sid);
            // check if this sesson id is active, if yes then it
            // is a session upgrade case.
            loginState = getLoginState(authContext);
            if (loginState != null) {
                sess = loginState.getSession();
            } else {
                sess = AuthD.getSession(sessionID);
            }
            if (sess == null) {
                sessionUpgrade = false;
            } else {
                sessionState = sess.getState();
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("sid from sess is : " + sess.getID());
                    utilDebug.message("sess is : " + sessionState);
                }
                if (!((sessionState == INVALID) || (isLogout))) {
                    ssot = AuthUtils.getExistingValidSSOToken(sid);
                    if ((indexType != null) && (indexName != null)) {
                        Hashtable indexTable = new Hashtable();
                        indexTable.put(indexType, indexName);
                        if (forceAuth) {
                            sessionUpgrade = true;
                        } else {
                            sessionUpgrade = checkSessionUpgrade(ssot, indexTable);
                        }
                    } else {
                        sessionUpgrade = true;
                    }
                }
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("session upgrade is : " + sessionUpgrade);
                }
            }
        }
        if (utilDebug.messageEnabled()) {
            utilDebug.message("AuthUtil:getAuthContext:sid is.. .: " + sid);
            utilDebug.message("AuthUtil:getAuthContext:authContext is.. .: " + authContext);
            utilDebug.message("AuthUtil:getAuthContext:sessionUpgrade is.. .: " + sessionUpgrade);
            utilDebug.message("AuthUtil:getAuthContext:ForceAuth is.. .: " + forceAuth);
        }
        if ((orgName == null) && (sess == null)) {
            utilDebug.error("Cannot create authcontext with null org ");
            throw new AuthException(AMAuthErrorCode.AUTH_TIMEOUT, null);
        } else if (orgName == null) {
            orgName = sess.getClientDomain();
        }
        if ((ssot != null) && !(sessionUpgrade)) {
            xmlReq.setValidSessionNoUpgrade(true);
            return null;
        }
        if (((ssot == null) && (loginState == null)) || (sessionUpgrade)) {
            try {
                loginState = new LoginState();
                InternalSession oldSession = null;
                if (sid != null) {
                    oldSession = AuthD.getSession(sid);
                    loginState.setOldSession(oldSession);
                }
                if (sessionUpgrade) {
                    loginState.setOldSession(oldSession);
                    loginState.setSessionUpgrade(sessionUpgrade);
                }
                authContext = loginState.createAuthContext(sid, orgName, req);
                authContext.setLoginState(loginState);
                String queryOrg = getQueryOrgName(null, orgName);
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("query org is .. : " + queryOrg);
                }
                loginState.setQueryOrg(queryOrg);
            } catch (AuthException ae) {
                utilDebug.message("Error creating AuthContextLocal 2: ");
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("Exception ", ae);
                }
                throw new AuthException(ae);
            }
        } else {
            // update loginState
            try {
                com.iplanet.dpro.session.service.InternalSession requestSess = ad.getSession(sessionID);
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("AuthUtil :Session is .. : " + requestSess);
                }
                loginState = getLoginState(authContext);
                if (loginState != null) {
                    loginState.setSession(requestSess);
                    loginState.setNewRequest(false);
                }
            } catch (Exception ae) {
                utilDebug.message("Error Retrieving AuthContextLocal");
                if (utilDebug.messageEnabled()) {
                    utilDebug.message("Exception ", ae);
                }
                throw new AuthException(AMAuthErrorCode.AUTH_ERROR, null);
            }
        }
        if (forceAuth) {
            loginState.setForceAuth(forceAuth);
        }
    } catch (Exception ee) {
        if (utilDebug.messageEnabled()) {
            utilDebug.message("Creating AuthContextLocal 2: ", ee);
        }
        throw new AuthException(ee);
    }
    return authContext;
}
Also used : SSOToken(com.iplanet.sso.SSOToken) Hashtable(java.util.Hashtable) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) SSOException(com.iplanet.sso.SSOException) SMSException(com.sun.identity.sm.SMSException) SessionException(com.iplanet.dpro.session.SessionException) InternalSession(com.iplanet.dpro.session.service.InternalSession) InternalSession(com.iplanet.dpro.session.service.InternalSession) SessionID(com.iplanet.dpro.session.SessionID) AuthContextLocal(com.sun.identity.authentication.server.AuthContextLocal)

Example 10 with SessionID

use of com.iplanet.dpro.session.SessionID in project OpenAM by OpenRock.

the class AuthUtils method getSidValue.

/**
     * Returns the SessionID . This is required to added the
     * session server , port , protocol info to the Logout Cookie.
     * SessionID is retrieved from Auth service if a handle on
     * the authcontext object is there otherwise retrieve from
     * the request object.
     *
     * @param authContext  is the AuthContext which is
     * 	    handle to the auth service
     * @param request is the HttpServletRequest object
     * @return returns the SessionID
     */
public static SessionID getSidValue(AuthContextLocal authContext, HttpServletRequest request) {
    SessionID sessionId = null;
    if (authContext != null) {
        utilDebug.message("AuthContext is not null");
        try {
            String sid = getSidString(authContext);
            if (sid != null) {
                sessionId = new SessionID(sid);
            }
        } catch (Exception e) {
            utilDebug.message("Exception getting sid", e);
        }
    }
    if (sessionId == null) {
        utilDebug.message("Sid from AuthContext is null");
        sessionId = new SessionID(request);
    }
    if (utilDebug.messageEnabled()) {
        utilDebug.message("sid is : " + sessionId);
    }
    return sessionId;
}
Also used : SessionID(com.iplanet.dpro.session.SessionID) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) SSOException(com.iplanet.sso.SSOException) SMSException(com.sun.identity.sm.SMSException) SessionException(com.iplanet.dpro.session.SessionException)

Aggregations

SessionID (com.iplanet.dpro.session.SessionID)105 Test (org.testng.annotations.Test)44 SessionException (com.iplanet.dpro.session.SessionException)31 SSOToken (com.iplanet.sso.SSOToken)23 InternalSession (com.iplanet.dpro.session.service.InternalSession)18 SSOException (com.iplanet.sso.SSOException)18 AuthContextLocalWrapper (org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper)17 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 Session (com.iplanet.dpro.session.Session)14 URL (java.net.URL)9 Map (java.util.Map)9 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)8 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 SMSException (com.sun.identity.sm.SMSException)4 Token (org.forgerock.openam.cts.api.tokens.Token)4 SessionIDExtensions (com.iplanet.dpro.session.SessionIDExtensions)3 TokenRestriction (com.iplanet.dpro.session.TokenRestriction)3 SessionInfo (com.iplanet.dpro.session.share.SessionInfo)3