Search in sources :

Example 26 with RequestContext

use of org.springframework.webflow.execution.RequestContext in project cas by apereo.

the class VerifySecurityQuestionsAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
    final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
    final String username = requestContext.getFlowScope().getString("username");
    final PasswordManagementProperties pm = casProperties.getAuthn().getPm();
    if (!pm.getReset().isSecurityQuestionsEnabled()) {
        LOGGER.debug("Security questions are not enabled");
        return success();
    }
    final Map<String, String> questions = passwordManagementService.getSecurityQuestions(username);
    final AtomicInteger i = new AtomicInteger(0);
    final long c = questions.values().stream().filter(v -> {
        final String answer = request.getParameter("q" + i.getAndIncrement());
        return answer.equals(v);
    }).count();
    if (c == questions.size()) {
        return success();
    }
    return error();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) PasswordManagementProperties(org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties) Logger(org.slf4j.Logger) AbstractAction(org.springframework.webflow.action.AbstractAction) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestContext(org.springframework.webflow.execution.RequestContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PasswordManagementService(org.apereo.cas.pm.PasswordManagementService) Map(java.util.Map) WebUtils(org.apereo.cas.web.support.WebUtils) Event(org.springframework.webflow.execution.Event) PasswordManagementProperties(org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 27 with RequestContext

use of org.springframework.webflow.execution.RequestContext in project cas by apereo.

the class AuthenticationAttributeMultifactorAuthenticationPolicyEventResolver method resolveInternal.

@Override
public Set<Event> resolveInternal(final RequestContext context) {
    final RegisteredService service = resolveRegisteredServiceInRequestContext(context);
    final Authentication authentication = WebUtils.getAuthentication(context);
    if (service == null || authentication == null) {
        LOGGER.debug("No service or authentication is available to determine event for principal");
        return null;
    }
    if (attributeNames.isEmpty()) {
        LOGGER.debug("Authentication attribute name to determine event is not configured");
        return null;
    }
    final Map<String, MultifactorAuthenticationProvider> providerMap = WebUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
    if (providerMap == null || providerMap.isEmpty()) {
        LOGGER.error("No multifactor authentication providers are available in the application context");
        return null;
    }
    final Collection<MultifactorAuthenticationProvider> providers = flattenProviders(providerMap.values());
    if (providers.size() == 1 && StringUtils.isNotBlank(globalAuthenticationAttributeValueRegex)) {
        final MultifactorAuthenticationProvider provider = providers.iterator().next();
        LOGGER.debug("Found a single multifactor provider [{}] in the application context", provider);
        return resolveEventViaAuthenticationAttribute(authentication, attributeNames, service, context, providers, input -> input != null && input.matches(globalAuthenticationAttributeValueRegex));
    }
    return resolveEventViaAuthenticationAttribute(authentication, attributeNames, service, context, providers, input -> providers.stream().filter(provider -> input != null && provider.matches(input)).count() > 0);
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) Logger(org.slf4j.Logger) Audit(org.apereo.inspektr.audit.annotation.Audit) Collection(java.util.Collection) MultifactorAuthenticationProviderSelector(org.apereo.cas.services.MultifactorAuthenticationProviderSelector) LoggerFactory(org.slf4j.LoggerFactory) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) TicketRegistrySupport(org.apereo.cas.ticket.registry.TicketRegistrySupport) Set(java.util.Set) StringUtils(org.apache.commons.lang3.StringUtils) RequestContext(org.springframework.webflow.execution.RequestContext) RegisteredService(org.apereo.cas.services.RegisteredService) BaseMultifactorAuthenticationProviderEventResolver(org.apereo.cas.web.flow.authentication.BaseMultifactorAuthenticationProviderEventResolver) Authentication(org.apereo.cas.authentication.Authentication) Map(java.util.Map) AuthenticationSystemSupport(org.apereo.cas.authentication.AuthenticationSystemSupport) WebUtils(org.apereo.cas.web.support.WebUtils) CookieGenerator(org.springframework.web.util.CookieGenerator) Event(org.springframework.webflow.execution.Event) ServicesManager(org.apereo.cas.services.ServicesManager) StringUtils(org.springframework.util.StringUtils) RegisteredService(org.apereo.cas.services.RegisteredService) Authentication(org.apereo.cas.authentication.Authentication) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider)

Example 28 with RequestContext

use of org.springframework.webflow.execution.RequestContext in project cas by apereo.

the class PrincipalAttributeMultifactorAuthenticationPolicyEventResolver method resolveInternal.

@Override
public Set<Event> resolveInternal(final RequestContext context) {
    final RegisteredService service = resolveRegisteredServiceInRequestContext(context);
    final Authentication authentication = WebUtils.getAuthentication(context);
    if (service == null || authentication == null) {
        LOGGER.debug("No service or authentication is available to determine event for principal");
        return null;
    }
    final Principal principal = authentication.getPrincipal();
    if (attributeNames.isEmpty()) {
        LOGGER.debug("Attribute name to determine event is not configured for [{}]", principal.getId());
        return null;
    }
    final Map<String, MultifactorAuthenticationProvider> providerMap = WebUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
    if (providerMap == null || providerMap.isEmpty()) {
        LOGGER.error("No multifactor authentication providers are available in the application context");
        return null;
    }
    final Collection<MultifactorAuthenticationProvider> providers = flattenProviders(providerMap.values());
    if (providers.size() == 1 && StringUtils.isNotBlank(globalPrincipalAttributeValueRegex)) {
        final MultifactorAuthenticationProvider provider = providers.iterator().next();
        LOGGER.debug("Found a single multifactor provider [{}] in the application context", provider);
        return resolveEventViaPrincipalAttribute(principal, attributeNames, service, context, providers, input -> input != null && input.matches(globalPrincipalAttributeValueRegex));
    }
    return resolveEventViaPrincipalAttribute(principal, attributeNames, service, context, providers, input -> providers.stream().filter(provider -> input != null && provider.matches(input)).count() > 0);
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) Logger(org.slf4j.Logger) Audit(org.apereo.inspektr.audit.annotation.Audit) Collection(java.util.Collection) MultifactorAuthenticationProviderSelector(org.apereo.cas.services.MultifactorAuthenticationProviderSelector) LoggerFactory(org.slf4j.LoggerFactory) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) TicketRegistrySupport(org.apereo.cas.ticket.registry.TicketRegistrySupport) Set(java.util.Set) StringUtils(org.apache.commons.lang3.StringUtils) RequestContext(org.springframework.webflow.execution.RequestContext) RegisteredService(org.apereo.cas.services.RegisteredService) BaseMultifactorAuthenticationProviderEventResolver(org.apereo.cas.web.flow.authentication.BaseMultifactorAuthenticationProviderEventResolver) Authentication(org.apereo.cas.authentication.Authentication) StringUtils.commaDelimitedListToSet(org.springframework.util.StringUtils.commaDelimitedListToSet) Map(java.util.Map) AuthenticationSystemSupport(org.apereo.cas.authentication.AuthenticationSystemSupport) Principal(org.apereo.cas.authentication.principal.Principal) WebUtils(org.apereo.cas.web.support.WebUtils) CookieGenerator(org.springframework.web.util.CookieGenerator) Event(org.springframework.webflow.execution.Event) ServicesManager(org.apereo.cas.services.ServicesManager) RegisteredService(org.apereo.cas.services.RegisteredService) Authentication(org.apereo.cas.authentication.Authentication) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) Principal(org.apereo.cas.authentication.principal.Principal)

Example 29 with RequestContext

use of org.springframework.webflow.execution.RequestContext in project cas by apereo.

the class AuthenticationExceptionHandlerActionTests method handleAccountNotFoundExceptionByDefault.

@Test
public void handleAccountNotFoundExceptionByDefault() {
    final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction(CollectionUtils.wrapSet(AccountLockedException.class, AccountNotFoundException.class));
    final RequestContext req = getMockRequestContext();
    final Map<String, Throwable> map = new HashMap<>();
    map.put("notFound", new AccountNotFoundException());
    final String id = handler.handle(new AuthenticationException(map), req);
    assertEquals(AccountNotFoundException.class.getSimpleName(), id);
}
Also used : AccountLockedException(javax.security.auth.login.AccountLockedException) HashMap(java.util.HashMap) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) RequestContext(org.springframework.webflow.execution.RequestContext) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException) Test(org.junit.Test)

Example 30 with RequestContext

use of org.springframework.webflow.execution.RequestContext in project cas by apereo.

the class ServiceThemeResolverTests method verifyGetServiceThemeDoesNotExist.

@Test
public void verifyGetServiceThemeDoesNotExist() {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setTheme("myTheme");
    r.setId(1000);
    r.setName("Test Service");
    r.setServiceId("myServiceId");
    this.servicesManager.save(r);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final RequestContext ctx = mock(RequestContext.class);
    final MutableAttributeMap scope = new LocalAttributeMap();
    scope.put(CasProtocolConstants.PARAMETER_SERVICE, RegisteredServiceTestUtils.getService(r.getServiceId()));
    when(ctx.getFlowScope()).thenReturn(scope);
    RequestContextHolder.setRequestContext(ctx);
    request.addHeader(HttpRequestUtils.USER_AGENT_HEADER, MOZILLA);
    assertEquals(DEFAULT_THEME_NAME, this.themeResolver.resolveThemeName(request));
}
Also used : LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MutableAttributeMap(org.springframework.webflow.core.collection.MutableAttributeMap) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) RequestContext(org.springframework.webflow.execution.RequestContext) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

RequestContext (org.springframework.webflow.execution.RequestContext)32 WebUtils (org.apereo.cas.web.support.WebUtils)14 Event (org.springframework.webflow.execution.Event)14 RegisteredService (org.apereo.cas.services.RegisteredService)11 Map (java.util.Map)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 Slf4j (lombok.extern.slf4j.Slf4j)8 Authentication (org.apereo.cas.authentication.Authentication)8 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)8 ServicesManager (org.apereo.cas.services.ServicesManager)8 Test (org.junit.Test)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 MultifactorAuthenticationProvider (org.apereo.cas.services.MultifactorAuthenticationProvider)7 List (java.util.List)6 Set (java.util.Set)6 StringUtils (org.apache.commons.lang3.StringUtils)6 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)6 AuthenticationServiceSelectionPlan (org.apereo.cas.authentication.AuthenticationServiceSelectionPlan)6 AuthenticationSystemSupport (org.apereo.cas.authentication.AuthenticationSystemSupport)6 MultifactorAuthenticationProviderSelector (org.apereo.cas.services.MultifactorAuthenticationProviderSelector)6