Search in sources :

Example 11 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class PersonDirectoryPrincipalResolverLdapTests method verifyChainedResolver.

@Test
public void verifyChainedResolver() {
    val resolver = CoreAuthenticationUtils.newPersonDirectoryPrincipalResolver(PrincipalFactoryUtils.newPrincipalFactory(), this.attributeRepository, CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger()), casProperties.getPersonDirectory());
    val chain = new ChainingPrincipalResolver(new DefaultPrincipalElectionStrategy(), casProperties);
    chain.setChain(Arrays.asList(new EchoingPrincipalResolver(), resolver));
    val attributes = new HashMap<String, List<Object>>(2);
    attributes.put("a1", List.of("v1"));
    attributes.put("a2", List.of("v2"));
    val p = chain.resolve(new UsernamePasswordCredential("admin", "password"), Optional.of(CoreAuthenticationTestUtils.getPrincipal("admin", attributes)), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertNotNull(p);
    assertTrue(p.getAttributes().containsKey("cn"));
    assertTrue(p.getAttributes().containsKey("a1"));
    assertTrue(p.getAttributes().containsKey("a2"));
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) HashMap(java.util.HashMap) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) EchoingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.EchoingPrincipalResolver) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 12 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class BaseActionTests method assertNoMfa.

protected void assertNoMfa() {
    val builder = WebUtils.getAuthenticationResultBuilder(requestContext);
    val attributes = builder.build(new DefaultPrincipalElectionStrategy()).getAuthentication().getAttributes();
    assertNull(attributes.get("inweboAuthenticationDevice"));
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy)

Example 13 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class TicketGrantingTicketResourceTests method initialize.

@BeforeEach
public void initialize() {
    val httpRequestCredentialFactory = new UsernamePasswordRestHttpRequestCredentialFactory() {

        @Override
        public List<Credential> fromAuthentication(final HttpServletRequest request, final MultiValueMap<String, String> requestBody, final Authentication authentication, final MultifactorAuthenticationProvider provider) {
            if (provider.getId().contains("unknown")) {
                return List.of();
            }
            return List.of(new UsernamePasswordCredential("mfa-user", "mfa-user"));
        }
    };
    val publisher = mock(ApplicationEventPublisher.class);
    val manager = mock(AuthenticationManager.class);
    lenient().when(manager.authenticate(any(AuthenticationTransaction.class))).thenReturn(CoreAuthenticationTestUtils.getAuthentication());
    lenient().when(ticketSupport.getAuthenticationFrom(anyString())).thenReturn(CoreAuthenticationTestUtils.getAuthentication());
    lenient().when(requestedContextValidator.validateAuthenticationContext(any(), any(), any(), any(), any())).thenReturn(AuthenticationContextValidationResult.builder().success(true).build());
    lenient().when(multifactorTriggerSelectionStrategy.resolve(any(), any(), any(), any(), any())).thenReturn(Optional.empty());
    val authenticationSystemSupport = new DefaultAuthenticationSystemSupport(new DefaultAuthenticationTransactionManager(publisher, manager), new DefaultPrincipalElectionStrategy(), new DefaultAuthenticationResultBuilderFactory(), new DefaultAuthenticationTransactionFactory());
    val api = new DefaultRestAuthenticationService(authenticationSystemSupport, httpRequestCredentialFactory, new WebApplicationServiceFactory(), multifactorTriggerSelectionStrategy, servicesManager, requestedContextValidator);
    val logoutManager = new DefaultLogoutManager(false, new DefaultLogoutExecutionPlan());
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    this.ticketGrantingTicketResourceUnderTest = new TicketGrantingTicketResource(api, casMock, new DefaultTicketGrantingTicketResourceEntityResponseFactory(), new GenericWebApplicationContext(), new DefaultSingleLogoutRequestExecutor(casMock, logoutManager, applicationContext));
    this.mockMvc = MockMvcBuilders.standaloneSetup(this.ticketGrantingTicketResourceUnderTest).defaultRequest(get("/").contextPath("/cas").contentType(MediaType.APPLICATION_FORM_URLENCODED)).build();
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Credential(org.apereo.cas.authentication.Credential) DefaultLogoutExecutionPlan(org.apereo.cas.logout.DefaultLogoutExecutionPlan) TicketGrantingTicketResource(org.apereo.cas.support.rest.resources.TicketGrantingTicketResource) DefaultLogoutManager(org.apereo.cas.logout.DefaultLogoutManager) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) UsernamePasswordRestHttpRequestCredentialFactory(org.apereo.cas.rest.factory.UsernamePasswordRestHttpRequestCredentialFactory) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) DefaultAuthenticationTransactionFactory(org.apereo.cas.authentication.DefaultAuthenticationTransactionFactory) DefaultAuthenticationTransactionManager(org.apereo.cas.authentication.DefaultAuthenticationTransactionManager) DefaultRestAuthenticationService(org.apereo.cas.rest.authentication.DefaultRestAuthenticationService) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) DefaultAuthenticationResultBuilderFactory(org.apereo.cas.authentication.DefaultAuthenticationResultBuilderFactory) Authentication(org.apereo.cas.authentication.Authentication) WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory) DefaultTicketGrantingTicketResourceEntityResponseFactory(org.apereo.cas.rest.factory.DefaultTicketGrantingTicketResourceEntityResponseFactory) DefaultAuthenticationSystemSupport(org.apereo.cas.authentication.DefaultAuthenticationSystemSupport) DefaultSingleLogoutRequestExecutor(org.apereo.cas.logout.slo.DefaultSingleLogoutRequestExecutor) AuthenticationTransaction(org.apereo.cas.authentication.AuthenticationTransaction) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MultiValueMap(org.springframework.util.MultiValueMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class UserAuthenticationResourceTests method verifyStatus.

@Test
public void verifyStatus() throws Exception {
    val builder = new DefaultAuthenticationResultBuilder().collect(CoreAuthenticationTestUtils.getAuthentication());
    val result = builder.build(new DefaultPrincipalElectionStrategy());
    lenient().when(authenticationSupport.finalizeAuthenticationTransaction(any(), anyCollection())).thenReturn(result);
    when(authenticationSupport.handleInitialAuthenticationTransaction(any(), any())).thenReturn(builder);
    when(authenticationSupport.finalizeAllAuthenticationTransactions(any(), any())).thenReturn(result);
    when(requestedContextValidator.validateAuthenticationContext(any(), any(), any(), any(), any())).thenReturn(AuthenticationContextValidationResult.builder().success(false).build());
    when(multifactorTriggerSelectionStrategy.resolve(any(), any(), any(), any(), any())).thenReturn(Optional.empty());
    this.mockMvc.perform(post(TICKETS_RESOURCE_URL).param("username", "casuser").param("password", "Mellon")).andExpect(status().isOk());
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) DefaultAuthenticationResultBuilder(org.apereo.cas.authentication.DefaultAuthenticationResultBuilder) Test(org.junit.jupiter.api.Test)

Example 15 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class UserAuthenticationResourceTests method verifyAuthWithMfa.

@Test
public void verifyAuthWithMfa() throws Exception {
    val builder = new DefaultAuthenticationResultBuilder().collect(CoreAuthenticationTestUtils.getAuthentication());
    val result = builder.build(new DefaultPrincipalElectionStrategy());
    when(authenticationSupport.finalizeAuthenticationTransaction(any(), anyCollection())).thenReturn(result);
    when(authenticationSupport.handleInitialAuthenticationTransaction(any(), any())).thenReturn(builder);
    when(requestedContextValidator.validateAuthenticationContext(any(), any(), any(), any(), any())).thenReturn(AuthenticationContextValidationResult.builder().success(false).build());
    when(multifactorTriggerSelectionStrategy.resolve(any(), any(), any(), any(), any())).thenReturn(Optional.of(new TestMultifactorAuthenticationProvider()));
    this.mockMvc.perform(post(TICKETS_RESOURCE_URL).param("username", "casuser").param("password", "Mellon")).andExpect(status().isOk());
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) DefaultAuthenticationResultBuilder(org.apereo.cas.authentication.DefaultAuthenticationResultBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)20 DefaultPrincipalElectionStrategy (org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy)20 Test (org.junit.jupiter.api.Test)13 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 HashMap (java.util.HashMap)4 DefaultAuthenticationResultBuilderFactory (org.apereo.cas.authentication.DefaultAuthenticationResultBuilderFactory)4 DefaultAuthenticationSystemSupport (org.apereo.cas.authentication.DefaultAuthenticationSystemSupport)4 DefaultAuthenticationTransactionFactory (org.apereo.cas.authentication.DefaultAuthenticationTransactionFactory)4 DefaultAuthenticationTransactionManager (org.apereo.cas.authentication.DefaultAuthenticationTransactionManager)4 Credential (org.apereo.cas.authentication.Credential)3 DefaultAuthenticationResultBuilder (org.apereo.cas.authentication.DefaultAuthenticationResultBuilder)3 WebApplicationServiceFactory (org.apereo.cas.authentication.principal.WebApplicationServiceFactory)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2