Search in sources :

Example 31 with SessionID

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

the class AuthIdHelperTest method shouldCreateAuthId.

@Test
public void shouldCreateAuthId() throws SignatureException, SMSException, SSOException, RestAuthException {
    //Given
    LoginConfiguration loginConfiguration = mock(LoginConfiguration.class);
    AuthContextLocalWrapper authContext = mock(AuthContextLocalWrapper.class);
    given(authContext.getOrgDN()).willReturn("ORG_DN");
    given(authContext.getSessionID()).willReturn(new SessionID("SESSION_ID"));
    given(loginConfiguration.getIndexType()).willReturn(AuthIndexType.NONE);
    given(loginConfiguration.getIndexValue()).willReturn(null);
    mockGetSigningKey("ORG_DN", false);
    //When
    String authId = authIdHelper.createAuthId(loginConfiguration, authContext);
    //Then
    assertNotNull(authId);
    verify(jwsHeaderBuilder).alg(JwsAlgorithm.HS256);
    verify(claimsSetBuilder).claim(eq("otk"), anyString());
    ArgumentCaptor<Map> contentArgumentCaptor = ArgumentCaptor.forClass(Map.class);
    verify(claimsSetBuilder).claims(contentArgumentCaptor.capture());
    Map jwtContent = contentArgumentCaptor.getValue();
    assertTrue(jwtContent.containsKey("realm"));
    assertTrue(jwtContent.containsValue("ORG_DN"));
    assertTrue(jwtContent.containsKey("sessionId"));
    assertTrue(jwtContent.containsValue("SESSION_ID"));
    assertFalse(jwtContent.containsKey("authIndexType"));
    assertFalse(jwtContent.containsKey("authIndexValue"));
}
Also used : LoginConfiguration(org.forgerock.openam.core.rest.authn.core.LoginConfiguration) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) SessionID(com.iplanet.dpro.session.SessionID) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 32 with SessionID

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

the class GetHttpSession method doGet.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!validateRequest(request)) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    String op = request.getParameter(OP);
    if (op.equals(RECOVER_OP)) {
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) {
            if (sessionDebug.messageEnabled()) {
                sessionDebug.message("GetHttpSession.recover: Old HttpSession is obtained");
            }
            SessionID sid = new SessionID(request);
            if (!sid.isNull()) {
                sessionService.retrieveSession(sid, httpSession);
            }
        } else {
            sessionDebug.error("GetHttpSession.recover: Old  HttpSession is not obtained");
        }
    } else if (op.equals(SAVE_OP)) {
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) {
            if (sessionDebug.messageEnabled()) {
                sessionDebug.message("GetHttpSession.save: HttpSession is obtained");
            }
            SessionID sid = new SessionID(request);
            if (!sid.isNull()) {
                int status = sessionService.handleSaveSession(sid, httpSession);
                response.setStatus(status);
            }
        } else {
            sessionDebug.error("GetHttpSession.save: HttpSession is not obtained");
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    } else if (op.equals(CREATE_OP)) {
        HttpSession httpSession = request.getSession(true);
        String domain = request.getParameter(DOMAIN);
        InternalSession is = sessionService.newInternalSession(domain, httpSession, false);
        if (sessionDebug.messageEnabled()) {
            sessionDebug.message("GetHttpSession.create: Created new session=" + is.getID());
        }
        DataOutputStream out = new DataOutputStream(response.getOutputStream());
        out.writeUTF(is.getID().toString());
        out.flush();
        out.close();
    } else if (op.equals(INVALIDATE_OP)) {
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) {
            if (sessionDebug.messageEnabled()) {
                sessionDebug.message("GetHttpSession.invalidate: HttpSession is obtained");
            }
            try {
                httpSession.invalidate();
            } catch (IllegalStateException ise) {
                if (sessionDebug.messageEnabled()) {
                    sessionDebug.message("Exception:invalidateSession: the web containers session timeout could be " + "shorter than the OpenSSO session timeout", ise);
                }
            }
        } else {
            if (sessionDebug.warningEnabled()) {
                sessionDebug.warning("GetHttpSession.invalidate: session is not obtained");
            }
        }
    } else if (op.equals(RELEASE_OP)) {
        SessionID sid = new SessionID(request);
        if (!sid.isNull()) {
            if (sessionDebug.messageEnabled()) {
                sessionDebug.message("GetHttpSession.release: releasing session=" + sid);
            }
            int status = sessionService.handleReleaseSession(sid);
            response.setStatus(status);
        } else {
            if (sessionDebug.messageEnabled()) {
                sessionDebug.message("GetHttpSession.release: missing session id");
            }
        }
    } else if (op.equals(GET_RESTRICTED_TOKEN_OP)) {
        DataInputStream in = null;
        DataOutputStream out = null;
        SessionID sid = new SessionID(request);
        try {
            in = new DataInputStream(request.getInputStream());
            TokenRestriction restriction = TokenRestrictionFactory.unmarshal(in.readUTF());
            String token = sessionService.handleGetRestrictedTokenIdRemotely(sid, restriction);
            if (token != null) {
                if (sessionDebug.messageEnabled()) {
                    sessionDebug.message("GetHttpSession.get_restricted_token: Created new session=" + token);
                }
                response.setStatus(HttpServletResponse.SC_OK);
                out = new DataOutputStream(response.getOutputStream());
                out.writeUTF(token);
                out.flush();
            } else {
                sessionDebug.error("GetHttpSession.get_restricted_token: failed to create token");
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (Exception ex) {
            sessionDebug.error("GetHttpSession.get_restricted_token: exception occured while create token", ex);
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } finally {
            IOUtils.closeIfNotNull(in);
            IOUtils.closeIfNotNull(out);
        }
    } else if (op.equals(DEREFERENCE_RESTRICTED_TOKEN_ID)) {
        DataInputStream in = null;
        DataOutputStream out = null;
        String cookieValue = CookieUtils.getCookieValueFromReq(request, CookieUtils.getAmCookieName());
        if ((cookieValue != null) && (cookieValue.indexOf("%") != -1)) {
            cookieValue = URLEncDec.decode(cookieValue);
        }
        SessionID sid = new SessionID(cookieValue);
        try {
            in = new DataInputStream(request.getInputStream());
            String restrictedID = in.readUTF();
            try {
                String masterSID = sessionService.deferenceRestrictedID(sessionCache.getSession(sid), restrictedID);
                response.setStatus(HttpServletResponse.SC_OK);
                out = new DataOutputStream(response.getOutputStream());
                out.writeUTF(masterSID);
                out.flush();
                if (sessionDebug.messageEnabled()) {
                    sessionDebug.message("GetHttpSession.dereference_restricted_token_id: master sid=" + masterSID);
                }
            } catch (SessionException se) {
                sessionDebug.message("GetHttpSession.dereference_restricted_token_id: unable to find master sid", se);
                response.setStatus(HttpServletResponse.SC_OK);
                out = new DataOutputStream(response.getOutputStream());
                out.writeUTF("ERROR");
                out.flush();
            }
        } catch (Exception ex) {
            sessionDebug.error("GetHttpSession.dereference_restricted_token_id: exception occured while finding master sid", ex);
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } finally {
            IOUtils.closeIfNotNull(in);
            IOUtils.closeIfNotNull(out);
        }
    } else {
        sessionDebug.error("GetHttpSession: unknown operation requested");
        response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
    }
}
Also used : TokenRestriction(com.iplanet.dpro.session.TokenRestriction) HttpSession(javax.servlet.http.HttpSession) DataOutputStream(java.io.DataOutputStream) SessionException(com.iplanet.dpro.session.SessionException) DataInputStream(java.io.DataInputStream) SessionID(com.iplanet.dpro.session.SessionID) IOException(java.io.IOException) SessionException(com.iplanet.dpro.session.SessionException)

Example 33 with SessionID

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

the class InternalSession method setRestrictedTokensBySid.

/**
     * This setter method is used by the JSON serialization mechanism and should not be used for other purposes.
     *
     * @param restrictedTokensBySid The deserialized map of sid&lt;->restricted tokens that should be stored in a
     * ConcurrentHashMap.
     */
@JsonSetter
private void setRestrictedTokensBySid(ConcurrentMap<SessionID, TokenRestriction> restrictedTokensBySid) {
    for (Map.Entry<SessionID, TokenRestriction> entry : restrictedTokensBySid.entrySet()) {
        SessionID sid = entry.getKey();
        TokenRestriction restriction = entry.getValue();
        this.restrictedTokensBySid.put(sid, restriction);
        this.restrictedTokensByRestriction.put(restriction, sid);
    }
}
Also used : TokenRestriction(com.iplanet.dpro.session.TokenRestriction) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SessionID(com.iplanet.dpro.session.SessionID) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter)

Example 34 with SessionID

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

the class InternalSessionCache method put.

/**
     * Stores the InternalSession in the cache. This will also store any associated references
     * which have been stored on the Session:
     *
     * - Session Handle
     * - Restricted Tokens
     *
     * Synchronized: makes updates to multiple data structures atomic.
     *
     * @param session Non null InternalSession to store.
     */
public synchronized void put(InternalSession session) {
    Reject.ifNull(session);
    cache.put(session.getID(), session);
    // Session Handle
    if (session.getSessionHandle() != null) {
        handle.put(session.getSessionHandle(), session);
    }
    // Restricted Sessions
    for (SessionID restrictedID : session.getRestrictedTokens()) {
        restricted.put(restrictedID, session);
    }
}
Also used : SessionID(com.iplanet.dpro.session.SessionID)

Example 35 with SessionID

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

the class InternalSessionFactory method createSession.

/**
     * Creates InternalSession which is always coupled with Http session This is
     * only used in session failover mode to ensure that every internal session
     * is associated with Http session used as fail-over store
     *
     * @param domain authentication domain passed to newInternalSession
     */
private InternalSession createSession(String domain) {
    DataInputStream in = null;
    try {
        String query = "?" + GetHttpSession.OP + "=" + GetHttpSession.CREATE_OP;
        if (domain != null) {
            query += "&" + GetHttpSession.DOMAIN + "=" + URLEncDec.encode(domain);
        }
        String routingCookie = null;
        URL url = serverConfig.createLocalServerURL("GetHttpSession" + query);
        HttpURLConnection conn = httpConnectionFactory.createSessionAwareConnection(url, null, routingCookie);
        in = new DataInputStream(conn.getInputStream());
        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return null;
        }
        SessionID sid = new SessionID(in.readUTF());
        return cache.getBySessionID(sid);
    } catch (Exception ex) {
        sessionDebug.error("Failed to retrieve new session", ex);
    } finally {
        IOUtils.closeIfNotNull(in);
    }
    return null;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DataInputStream(java.io.DataInputStream) SessionID(com.iplanet.dpro.session.SessionID) URL(java.net.URL) 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