Java Code Examples for org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext#setRequestType()

The following examples show how to use org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext#setRequestType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultAuthenticationRequestHandlerTest.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "errorInfoDataProvider")
public void testPopulateErrorInformation(String errorCode,
                                         String errorMessage,
                                         String errorUri,
                                         String requestType) throws Exception {

    AuthenticationResult authenticationResult = new AuthenticationResult();
    doReturn(authenticationResult).when(request).getAttribute(FrameworkConstants.RequestAttribute.AUTH_RESULT);

    // Populate the context with error details
    AuthenticationContext context = new AuthenticationContext();
    context.setProperty(FrameworkConstants.AUTH_ERROR_CODE, errorCode);
    context.setProperty(FrameworkConstants.AUTH_ERROR_MSG, errorMessage);
    context.setProperty(FrameworkConstants.AUTH_ERROR_URI, errorUri);

    // request type is does not cache authentication result
    context.setRequestType(requestType);
    response = spy(new CommonAuthResponseWrapper(response));

    // if request type caches authentication result we need to mock required dependent objects
    AuthenticationResultCacheEntry cacheEntry = spy(new AuthenticationResultCacheEntry());
    when(cacheEntry.getResult()).thenReturn(authenticationResult);
    mockStatic(FrameworkUtils.class);
    when(FrameworkUtils.getAuthenticationResultFromCache(anyString())).thenReturn(cacheEntry);

    authenticationRequestHandler.populateErrorInformation(request, response, context);

    // Assert stuff
    AuthenticationResult modifiedAuthenticationResult =
            (AuthenticationResult) request.getAttribute(FrameworkConstants.RequestAttribute.AUTH_RESULT);

    assertNotNull(modifiedAuthenticationResult);
    assertEquals(modifiedAuthenticationResult.getProperty(FrameworkConstants.AUTH_ERROR_CODE), errorCode);
    assertEquals(modifiedAuthenticationResult.getProperty(FrameworkConstants.AUTH_ERROR_MSG), errorMessage);
    assertEquals(modifiedAuthenticationResult.getProperty(FrameworkConstants.AUTH_ERROR_URI), errorUri);
}
 
Example 2
Source File: DefaultRequestCoordinator.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Handles the initial request (from the calling servlet)
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 * @throws
 */
protected AuthenticationContext initializeFlow(HttpServletRequest request, HttpServletResponse response)
        throws FrameworkException {

    if (log.isDebugEnabled()) {
        log.debug("Initializing the flow");
    }

    // "sessionDataKey" - calling servlet maintains its state information
    // using this
    String callerSessionDataKey = request.getParameter(FrameworkConstants.SESSION_DATA_KEY);

    // "commonAuthCallerPath" - path of the calling servlet. This is the url
    // response should be sent to
    String callerPath = getCallerPath(request);

    // "type" - type of the request. e.g. samlsso, openid, oauth, passivests
    String requestType = request.getParameter(FrameworkConstants.RequestParams.TYPE);

    // "relyingParty"
    String relyingParty = request.getParameter(FrameworkConstants.RequestParams.ISSUER);

    // tenant domain
    String tenantDomain = getTenantDomain(request);

    // Store the request data sent by the caller
    AuthenticationContext context = new AuthenticationContext();
    context.setCallerSessionKey(callerSessionDataKey);
    context.setCallerPath(callerPath);
    context.setRequestType(requestType);
    context.setRelyingParty(relyingParty);
    context.setTenantDomain(tenantDomain);

    // generate a new key to hold the context data object
    String contextId = UUIDGenerator.generateUUID();
    context.setContextIdentifier(contextId);

    if (log.isDebugEnabled()) {
        log.debug("Framework contextId: " + contextId);
    }

    // if this a logout request from the calling servlet
    if (request.getParameter(FrameworkConstants.RequestParams.LOGOUT) != null) {

        if (log.isDebugEnabled()) {
            log.debug("Starting a logout flow");
        }

        context.setLogoutRequest(true);

        if (context.getRelyingParty() == null || context.getRelyingParty().trim().length() == 0) {

            if (log.isDebugEnabled()) {
                log.debug("relyingParty param is null. This is a possible logout scenario.");
            }

            Cookie cookie = FrameworkUtils.getAuthCookie(request);

            String sessionContextKey = null;
            if (cookie != null) {
                sessionContextKey = DigestUtils.sha256Hex(cookie.getValue());
            } else {
                sessionContextKey = request.getParameter(SESSION_ID);
            }
            context.setSessionIdentifier(sessionContextKey);
            return context;
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Starting an authentication flow");
        }
    }

    List<ClaimMapping> requestedClaimsInRequest = (List<ClaimMapping>) request.getAttribute(REQUESTED_ATTRIBUTES);
    context.setProperty(FrameworkConstants.SP_REQUESTED_CLAIMS_IN_REQUEST, requestedClaimsInRequest);

    associateTransientRequestData(request, response, context);
    findPreviousAuthenticatedSession(request, context);
    buildOutboundQueryString(request, context);

    return context;
}
 
Example 3
Source File: DefaultRequestCoordinator.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Handles the initial request (from the calling servlet)
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 * @throws
 */
protected AuthenticationContext initializeFlow(HttpServletRequest request,
                                               HttpServletResponse response) throws FrameworkException {

    if (log.isDebugEnabled()) {
        log.debug("Initializing the flow");
    }


    // "sessionDataKey" - calling servlet maintains its state information
    // using this
    String callerSessionDataKey = request.getParameter(FrameworkConstants.SESSION_DATA_KEY);

    // "commonAuthCallerPath" - path of the calling servlet. This is the url
    // response should be sent to
    String callerPath = getCallerPath(request);

    // "type" - type of the request. e.g. samlsso, openid, oauth, passivests
    String requestType = request.getParameter(FrameworkConstants.RequestParams.TYPE);

    // "relyingParty"
    String relyingParty = request.getParameter(FrameworkConstants.RequestParams.ISSUER);

    // tenant domain
    String tenantDomain = getTenantDomain(request);

    // Store the request data sent by the caller
    AuthenticationContext context = new AuthenticationContext();
    context.setCallerSessionKey(callerSessionDataKey);
    context.setCallerPath(callerPath);
    context.setRequestType(requestType);
    context.setRelyingParty(relyingParty);
    context.setTenantDomain(tenantDomain);

    // generate a new key to hold the context data object
    String contextId = UUIDGenerator.generateUUID();
    context.setContextIdentifier(contextId);

    if (log.isDebugEnabled()) {
        log.debug("Framework contextId: " + contextId);
    }


    // if this a logout request from the calling servlet
    if (request.getParameter(FrameworkConstants.RequestParams.LOGOUT) != null) {

        if (log.isDebugEnabled()) {
            log.debug("Starting a logout flow");
        }

        context.setLogoutRequest(true);

        if (context.getRelyingParty() == null || context.getRelyingParty().trim().length() == 0) {

            if (log.isDebugEnabled()) {
                log.debug("relyingParty param is null. This is a possible logout scenario.");
            }

            Cookie cookie = FrameworkUtils.getAuthCookie(request);

            if (cookie != null) {
                context.setSessionIdentifier(cookie.getValue());
            }

            return context;
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Starting an authentication flow");
        }
    }

    findPreviousAuthenticatedSession(request, context);
    buildOutboundQueryString(request, context);

    return context;
}