Search in sources :

Example 1 with SessionExtenderClientException

use of org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException in project carbon-identity-framework by wso2.

the class SessionExtenderProcessor method getSessionKey.

private String getSessionKey(SessionExtenderRequest sessionExtenderRequest) throws SessionExtenderClientException {
    String sessionKeyFromParam = getSessionKeyFromParameters(sessionExtenderRequest);
    String sessionKeyFromCookie = getSessionKeyFromCookie(sessionExtenderRequest);
    // When both the cookie and parameter are present, check whether they match.
    if (sessionKeyFromParam != null && sessionKeyFromCookie != null) {
        if (!sessionKeyFromParam.equals(sessionKeyFromCookie)) {
            throw new SessionExtenderClientException(SessionExtenderConstants.Error.CONFLICT.getCode(), SessionExtenderConstants.Error.CONFLICT.getMessage(), "Session key mismatch between cookie and parameter values.");
        }
    }
    if (sessionKeyFromParam != null) {
        if (log.isDebugEnabled()) {
            log.debug("SessionExtenderProcessor proceeding with the sessionKey in the request. Identified session: " + sessionKeyFromParam);
        }
        return sessionKeyFromParam;
    } else if (sessionKeyFromCookie != null) {
        if (log.isDebugEnabled()) {
            log.debug("SessionExtenderProcessor proceeding with the sessionCookie in the request. Identified " + "session: " + sessionKeyFromCookie);
        }
        return sessionKeyFromCookie;
    } else {
        throw new SessionExtenderClientException(SessionExtenderConstants.Error.INVALID_REQUEST.getCode(), SessionExtenderConstants.Error.INVALID_REQUEST.getMessage(), "No session key or cookie available for processing.");
    }
}
Also used : SessionExtenderClientException(org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException)

Example 2 with SessionExtenderClientException

use of org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException in project carbon-identity-framework by wso2.

the class SessionExtenderRequestFactory method handleException.

@Override
public HttpIdentityResponse.HttpIdentityResponseBuilder handleException(FrameworkClientException exception, HttpServletRequest request, HttpServletResponse response) {
    HttpIdentityResponse.HttpIdentityResponseBuilder errorResponseBuilder = new HttpIdentityResponse.HttpIdentityResponseBuilder();
    SessionExtenderErrorResponse.SessionExtenderErrorResponseBuilder sessionErrorResponseBuilder = new SessionExtenderErrorResponse.SessionExtenderErrorResponseBuilder();
    sessionErrorResponseBuilder.setErrorCode(exception.getErrorCode());
    String traceId = FrameworkUtils.getCorrelation();
    String errorLogDescription;
    if (exception instanceof SessionExtenderClientException) {
        sessionErrorResponseBuilder.setErrorMessage(((SessionExtenderClientException) exception).getErrorMessage());
        sessionErrorResponseBuilder.setErrorDescription(((SessionExtenderClientException) exception).getDescription());
        sessionErrorResponseBuilder.setTraceId(traceId);
        errorLogDescription = ((SessionExtenderClientException) exception).getDescription();
    } else {
        sessionErrorResponseBuilder.setErrorMessage(exception.getMessage());
        errorLogDescription = exception.getMessage();
    }
    if (log.isDebugEnabled()) {
        String clientErrorLog = String.format(ERROR_LOG_TEMPLATE, traceId, SessionExtenderRequestFactory.class.getName(), errorLogDescription);
        log.debug(clientErrorLog);
    }
    SessionExtenderErrorResponse sessionExtenderErrorResponse = sessionErrorResponseBuilder.build();
    errorResponseBuilder.setStatusCode(SC_BAD_REQUEST);
    errorResponseBuilder.setContentType(APPLICATION_JSON);
    errorResponseBuilder.setBody(sessionExtenderErrorResponse.getResponse());
    return errorResponseBuilder;
}
Also used : SessionExtenderErrorResponse(org.wso2.carbon.identity.application.authentication.framework.session.extender.response.SessionExtenderErrorResponse) SessionExtenderClientException(org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException) HttpIdentityResponse(org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse)

Example 3 with SessionExtenderClientException

use of org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException in project carbon-identity-framework by wso2.

the class SessionExtenderRequestFactoryTest method testHandleException.

@Test
public void testHandleException() {
    SessionExtenderClientException exception = mock(SessionExtenderClientException.class);
    when(exception.getErrorCode()).thenReturn(EXCEPTION_ERROR_CODE);
    when(exception.getErrorMessage()).thenReturn(EXCEPTION_MESSAGE);
    when(exception.getDescription()).thenReturn(EXCEPTION_DESCRIPTION);
    ThreadContext.put("Correlation-ID", TRACE_ID);
    HttpIdentityResponse.HttpIdentityResponseBuilder responseBuilder = sessionExtenderRequestFactory.handleException(exception, mockedHttpRequest, mockedHttpResponse);
    HttpIdentityResponse response = responseBuilder.build();
    assertEquals(response.getBody(), ERROR_RESPONSE_BODY);
    assertEquals(response.getStatusCode(), 400);
}
Also used : SessionExtenderClientException(org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException) HttpIdentityResponse(org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse) Test(org.testng.annotations.Test)

Example 4 with SessionExtenderClientException

use of org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException in project carbon-identity-framework by wso2.

the class SessionExtenderProcessor method process.

@Override
public IdentityResponse.IdentityResponseBuilder process(IdentityRequest identityRequest) throws SessionExtenderClientException {
    if (log.isDebugEnabled()) {
        log.debug("Request processing started by SessionExtenderProcessor.");
    }
    SessionExtenderRequest sessionExtenderRequest = (SessionExtenderRequest) identityRequest;
    String tenantDomain = sessionExtenderRequest.getTenantDomain();
    String sessionKey = getSessionKey(sessionExtenderRequest);
    SessionContextCacheKey sessionContextCacheKey = new SessionContextCacheKey(sessionKey);
    SessionContextCacheEntry sessionContextCacheEntry = SessionContextCache.getInstance().getSessionContextCacheEntry(sessionContextCacheKey, tenantDomain);
    if (sessionContextCacheEntry == null) {
        if (log.isDebugEnabled()) {
            log.debug("No session available for requested session identifier: " + sessionKey);
        }
        throw new SessionExtenderClientException(SessionExtenderConstants.Error.SESSION_NOT_AVAILABLE.getCode(), SessionExtenderConstants.Error.SESSION_NOT_AVAILABLE.getMessage(), "No session available for requested session identifier.");
    }
    SessionContext sessionContext = sessionContextCacheEntry.getContext();
    long currentTime = System.currentTimeMillis();
    FrameworkUtils.updateSessionLastAccessTimeMetadata(sessionKey, currentTime);
    FrameworkUtils.addSessionContextToCache(sessionKey, sessionContext, tenantDomain, tenantDomain);
    String traceId = FrameworkUtils.getCorrelation();
    fireEvent(sessionKey, sessionContext, tenantDomain, traceId);
    addAuditLogs(sessionKey, tenantDomain, traceId);
    SessionExtenderResponse.SessionExtenderResponseBuilder responseBuilder = new SessionExtenderResponse.SessionExtenderResponseBuilder();
    responseBuilder.setTraceId(traceId);
    return responseBuilder;
}
Also used : SessionExtenderResponse(org.wso2.carbon.identity.application.authentication.framework.session.extender.response.SessionExtenderResponse) SessionExtenderClientException(org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException) SessionContextCacheEntry(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheEntry) SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) SessionContextCacheKey(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey) SessionExtenderRequest(org.wso2.carbon.identity.application.authentication.framework.session.extender.request.SessionExtenderRequest)

Example 5 with SessionExtenderClientException

use of org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException in project carbon-identity-framework by wso2.

the class SessionExtenderRequestFactory method create.

@Override
public IdentityRequest.IdentityRequestBuilder create(HttpServletRequest request, HttpServletResponse response) throws FrameworkClientException {
    if (log.isDebugEnabled()) {
        log.debug("SessionExtenderRequest creation initiated by the factory.");
    }
    SessionExtenderRequest.SessionExtenderRequestBuilder builder = new SessionExtenderRequest.SessionExtenderRequestBuilder(request, response);
    super.create(builder, request, response);
    String sessionKeyValue = request.getParameter(SESSION_ID_PARAM_NAME);
    if (sessionKeyValue != null) {
        builder.setSessionKey(sessionKeyValue);
    }
    Cookie commonAuthCookie = FrameworkUtils.getAuthCookie(request);
    if (commonAuthCookie != null) {
        builder.setSessionCookie(commonAuthCookie);
    }
    if (sessionKeyValue == null && commonAuthCookie == null) {
        throw new SessionExtenderClientException(SessionExtenderConstants.Error.INVALID_REQUEST.getCode(), SessionExtenderConstants.Error.INVALID_REQUEST.getMessage(), "No session identifier parameter or cookie present in request.");
    }
    return builder;
}
Also used : Cookie(javax.servlet.http.Cookie) SessionExtenderClientException(org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException)

Aggregations

SessionExtenderClientException (org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException)5 HttpIdentityResponse (org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponse)2 Cookie (javax.servlet.http.Cookie)1 Test (org.testng.annotations.Test)1 SessionContextCacheEntry (org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheEntry)1 SessionContextCacheKey (org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey)1 SessionContext (org.wso2.carbon.identity.application.authentication.framework.context.SessionContext)1 SessionExtenderRequest (org.wso2.carbon.identity.application.authentication.framework.session.extender.request.SessionExtenderRequest)1 SessionExtenderErrorResponse (org.wso2.carbon.identity.application.authentication.framework.session.extender.response.SessionExtenderErrorResponse)1 SessionExtenderResponse (org.wso2.carbon.identity.application.authentication.framework.session.extender.response.SessionExtenderResponse)1