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.");
}
}
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;
}
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);
}
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;
}
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;
}
Aggregations