Search in sources :

Example 6 with AuthHistory

use of org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory in project carbon-identity-framework by wso2.

the class GraphBasedSequenceHandlerCustomFunctionsTest method testHandleDynamicJavascriptSerialization.

@Test
public void testHandleDynamicJavascriptSerialization() throws Exception {
    JsFunctionRegistry jsFunctionRegistrar = new JsFunctionRegistryImpl();
    FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistrar);
    jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn1", (Function<JsAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest::customFunction1);
    ServiceProvider sp1 = getTestServiceProvider("js-sp-dynamic-1.xml");
    AuthenticationContext context = getAuthenticationContext(sp1);
    SequenceConfig sequenceConfig = configurationLoader.getSequenceConfig(context, Collections.<String, String[]>emptyMap(), sp1);
    context.setSequenceConfig(sequenceConfig);
    byte[] serialized = SerializationUtils.serialize(context);
    AuthenticationContext deseralizedContext = (AuthenticationContext) SerializationUtils.deserialize(serialized);
    assertNotNull(deseralizedContext);
    HttpServletRequest req = mock(HttpServletRequest.class);
    addMockAttributes(req);
    HttpServletResponse resp = mock(HttpServletResponse.class);
    UserCoreUtil.setDomainInThreadLocal("test_domain");
    graphBasedSequenceHandler.handle(req, resp, deseralizedContext);
    List<AuthHistory> authHistories = deseralizedContext.getAuthenticationStepHistory();
    assertNotNull(authHistories);
    assertEquals(3, authHistories.size());
    assertEquals(authHistories.get(0).getAuthenticatorName(), "BasicMockAuthenticator");
    assertEquals(authHistories.get(1).getAuthenticatorName(), "HwkMockAuthenticator");
    assertEquals(authHistories.get(2).getAuthenticatorName(), "FptMockAuthenticator");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JsFunctionRegistry(org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry) JsAuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) JsAuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext) JsFunctionRegistryImpl(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsFunctionRegistryImpl) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) SequenceConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) Test(org.testng.annotations.Test)

Example 7 with AuthHistory

use of org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory in project carbon-identity-framework by wso2.

the class GraphBasedSequenceHandlerFailTest method handleFailMethodWithParamsOnFailTest.

@Test
public void handleFailMethodWithParamsOnFailTest() throws Exception {
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
    ServiceProvider sp1 = getTestServiceProvider("js-sp-fail-method-with-params-onFail.xml");
    AuthenticationContext context = getAuthenticationContext(sp1);
    SequenceConfig sequenceConfig = configurationLoader.getSequenceConfig(context, Collections.<String, String[]>emptyMap(), sp1);
    context.setSequenceConfig(sequenceConfig);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getAttribute(FrameworkConstants.RequestParams.FLOW_STATUS)).thenReturn(AuthenticatorFlowStatus.FAIL_COMPLETED);
    HttpServletResponse resp = mock(HttpServletResponse.class);
    UserCoreUtil.setDomainInThreadLocal("test_domain");
    graphBasedSequenceHandler.handle(req, resp, context);
    List<AuthHistory> authHistories = context.getAuthenticationStepHistory();
    assertNotNull(authHistories);
    assertFalse(context.isRequestAuthenticated());
    assertEquals(context.getProperty(FrameworkConstants.AUTH_ERROR_CODE), "access_denied");
    assertEquals(context.getProperty(FrameworkConstants.AUTH_ERROR_MSG), "login could not be completed");
    assertEquals(context.getProperty(FrameworkConstants.AUTH_ERROR_URI), "https://wso2.com/");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) SequenceConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) Test(org.testng.annotations.Test)

Example 8 with AuthHistory

use of org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory in project carbon-identity-framework by wso2.

the class GraphBasedSequenceHandlerFailTest method handleFailMethodWithParamsTest.

@Test
public void handleFailMethodWithParamsTest() throws Exception {
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
    ServiceProvider sp1 = getTestServiceProvider("js-sp-fail-method-with-params-onSuccess.xml");
    AuthenticationContext context = getAuthenticationContext(sp1);
    SequenceConfig sequenceConfig = configurationLoader.getSequenceConfig(context, Collections.<String, String[]>emptyMap(), sp1);
    context.setSequenceConfig(sequenceConfig);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getAttribute(FrameworkConstants.RequestParams.FLOW_STATUS)).thenReturn(AuthenticatorFlowStatus.SUCCESS_COMPLETED);
    HttpServletResponse resp = mock(HttpServletResponse.class);
    UserCoreUtil.setDomainInThreadLocal("test_domain");
    graphBasedSequenceHandler.handle(req, resp, context);
    List<AuthHistory> authHistories = context.getAuthenticationStepHistory();
    assertNotNull(authHistories);
    assertFalse(context.isRequestAuthenticated());
    assertEquals(context.getProperty(FrameworkConstants.AUTH_ERROR_CODE), "access_denied");
    assertEquals(context.getProperty(FrameworkConstants.AUTH_ERROR_MSG), "login could not be completed");
    assertEquals(context.getProperty(FrameworkConstants.AUTH_ERROR_URI), "https://wso2.com/");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) SequenceConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) Test(org.testng.annotations.Test)

Example 9 with AuthHistory

use of org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory in project carbon-identity-framework by wso2.

the class UserSessionStore method updateFederatedAuthSessionInfo.

/**
 * Update session details of a given session context key to map the current session context key with
 * the federated IdP's session ID.
 *
 * @param sessionContextKey Session Context Key.
 * @param authHistory       History of the authentication flow.
 * @throws UserSessionException Error while storing session details.
 */
public void updateFederatedAuthSessionInfo(String sessionContextKey, AuthHistory authHistory) throws UserSessionException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(SQLQueries.SQL_UPDATE_FEDERATED_AUTH_SESSION_INFO, preparedStatement -> {
            preparedStatement.setString(1, sessionContextKey);
            preparedStatement.setString(2, authHistory.getIdpSessionIndex());
        });
    } catch (DataAccessException e) {
        throw new UserSessionException("Error while updating " + sessionContextKey + " of session:" + authHistory.getIdpSessionIndex() + " in table " + IDN_AUTH_SESSION_META_DATA_TABLE + ".", e);
    }
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 10 with AuthHistory

use of org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory in project carbon-identity-framework by wso2.

the class DefaultRequestPathBasedSequenceHandler method handle.

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AuthenticationContext context) throws FrameworkException {
    if (log.isDebugEnabled()) {
        log.debug("Executing the Request Path Authentication...");
    }
    SequenceConfig seqConfig = context.getSequenceConfig();
    List<AuthenticatorConfig> reqPathAuthenticators = seqConfig.getReqPathAuthenticators();
    for (AuthenticatorConfig reqPathAuthenticator : reqPathAuthenticators) {
        ApplicationAuthenticator authenticator = reqPathAuthenticator.getApplicationAuthenticator();
        if (log.isDebugEnabled()) {
            log.debug("Executing " + authenticator.getName());
        }
        if (authenticator.canHandle(request)) {
            if (log.isDebugEnabled()) {
                log.debug(authenticator.getName() + " can handle the request");
            }
            try {
                AuthenticatorFlowStatus status = authenticator.process(request, response, context);
                request.setAttribute(FrameworkConstants.RequestParams.FLOW_STATUS, status);
                if (log.isDebugEnabled()) {
                    log.debug(authenticator.getName() + ".authenticate() returned: " + status.toString());
                }
                AuthenticatedUser authenticatedUser = context.getSubject();
                seqConfig.setAuthenticatedUser(authenticatedUser);
                if (log.isDebugEnabled()) {
                    if (authenticatedUser != null) {
                        log.debug("Authenticated User: " + authenticatedUser.getAuthenticatedSubjectIdentifier());
                        log.debug("Authenticated User Tenant Domain: " + authenticatedUser.getTenantDomain());
                    } else {
                        log.debug("Authenticated User is NULL.");
                    }
                }
                AuthenticatedIdPData authenticatedIdPData = new AuthenticatedIdPData();
                // store authenticated user
                authenticatedIdPData.setUser(authenticatedUser);
                // store authenticated idp
                authenticatedIdPData.setIdpName(FrameworkConstants.LOCAL_IDP_NAME);
                reqPathAuthenticator.setAuthenticatorStateInfo(context.getStateInfo());
                authenticatedIdPData.setAuthenticator(reqPathAuthenticator);
                seqConfig.setAuthenticatedReqPathAuthenticator(reqPathAuthenticator);
                context.getCurrentAuthenticatedIdPs().put(FrameworkConstants.LOCAL_IDP_NAME, authenticatedIdPData);
                handlePostAuthentication(request, response, context, authenticatedIdPData);
                context.addAuthenticationStepHistory(new AuthHistory(authenticator.getName(), authenticatedIdPData.getIdpName()));
            } catch (InvalidCredentialsException e) {
                if (log.isDebugEnabled()) {
                    log.debug("A login attempt was failed due to invalid credentials", e);
                }
                context.setRequestAuthenticated(false);
            } catch (AuthenticationFailedException e) {
                log.error(e.getMessage(), e);
                context.setRequestAuthenticated(false);
            } catch (LogoutFailedException e) {
                throw new FrameworkException(e.getMessage(), e);
            }
            context.getSequenceConfig().setCompleted(true);
            return;
        }
    }
}
Also used : AuthenticatorConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig) ApplicationAuthenticator(org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) InvalidCredentialsException(org.wso2.carbon.identity.application.authentication.framework.exception.InvalidCredentialsException) AuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException) SequenceConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig) LogoutFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.LogoutFailedException) AuthenticatorFlowStatus(org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) AuthenticatedIdPData(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedIdPData)

Aggregations

AuthHistory (org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory)16 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)11 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)11 Test (org.testng.annotations.Test)10 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 JsFunctionRegistryImpl (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsFunctionRegistryImpl)5 JsAuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext)5 UserSessionException (org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException)5 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)4 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)4 ApplicationAuthenticator (org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator)3 AuthenticatorFlowStatus (org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus)3 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)3 SessionContext (org.wso2.carbon.identity.application.authentication.framework.context.SessionContext)3 AuthenticationFailedException (org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException)3 LogoutFailedException (org.wso2.carbon.identity.application.authentication.framework.exception.LogoutFailedException)3 ArrayList (java.util.ArrayList)2 JsFunctionRegistry (org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry)2