Search in sources :

Example 11 with DefaultPrincipalFactory

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

the class SpnegoCredentialsToPrincipalResolverTests method verifyValidCredentials.

@Test
public void verifyValidCredentials() {
    this.spnegoCredentials.setPrincipal(new DefaultPrincipalFactory().createPrincipal("test"));
    assertEquals("test", this.resolver.resolve(this.spnegoCredentials, CoreAuthenticationTestUtils.getPrincipal(), new SimpleTestUsernamePasswordAuthenticationHandler()).getId());
}
Also used : SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) DefaultPrincipalFactory(org.apereo.cas.authentication.principal.DefaultPrincipalFactory) Test(org.junit.Test)

Example 12 with DefaultPrincipalFactory

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

the class Saml10SuccessResponseViewTests method verifyResponseWithoutAuthMethod.

@Test
public void verifyResponseWithoutAuthMethod() throws Exception {
    final Map<String, Object> model = new HashMap<>();
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(TEST_ATTRIBUTE, TEST_VALUE);
    final Principal principal = new DefaultPrincipalFactory().createPrincipal(PRINCIPAL_ID, attributes);
    final Map<String, Object> authnAttributes = new HashMap<>();
    authnAttributes.put("authnAttribute1", "authnAttrbuteV1");
    authnAttributes.put("authnAttribute2", "authnAttrbuteV2");
    authnAttributes.put(RememberMeCredential.AUTHENTICATION_ATTRIBUTE_REMEMBER_ME, Boolean.TRUE);
    final Authentication primary = CoreAuthenticationTestUtils.getAuthentication(principal, authnAttributes);
    final Assertion assertion = new ImmutableAssertion(primary, Collections.singletonList(primary), CoreAuthenticationTestUtils.getService(), true);
    model.put("assertion", assertion);
    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    this.response.renderMergedOutputModel(model, new MockHttpServletRequest(), servletResponse);
    final String written = servletResponse.getContentAsString();
    assertTrue(written.contains(PRINCIPAL_ID));
    assertTrue(written.contains(TEST_ATTRIBUTE));
    assertTrue(written.contains(TEST_VALUE));
    assertTrue(written.contains("authnAttribute1"));
    assertTrue(written.contains("authnAttribute2"));
    assertTrue(written.contains(CasProtocolConstants.VALIDATION_REMEMBER_ME_ATTRIBUTE_NAME));
    assertTrue(written.contains("urn:oasis:names:tc:SAML:1.0:am:unspecified"));
}
Also used : HashMap(java.util.HashMap) ImmutableAssertion(org.apereo.cas.validation.ImmutableAssertion) Authentication(org.apereo.cas.authentication.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Assertion(org.apereo.cas.validation.Assertion) ImmutableAssertion(org.apereo.cas.validation.ImmutableAssertion) DefaultPrincipalFactory(org.apereo.cas.authentication.principal.DefaultPrincipalFactory) Principal(org.apereo.cas.authentication.principal.Principal) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 13 with DefaultPrincipalFactory

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

the class RegisteredServiceAttributeReleasePolicyTests method checkServiceAttributeFilterAllAttributesWithCachingTurnedOn.

@Test
public void checkServiceAttributeFilterAllAttributesWithCachingTurnedOn() {
    final ReturnAllAttributeReleasePolicy policy = new ReturnAllAttributeReleasePolicy();
    final Map<String, List<Object>> attributes = new HashMap<>();
    attributes.put("values", Arrays.asList(new Object[] { "v1", "v2", "v3" }));
    attributes.put("cn", Arrays.asList(new Object[] { "commonName" }));
    attributes.put("username", Arrays.asList(new Object[] { "uid" }));
    final IPersonAttributeDao dao = new StubPersonAttributeDao(attributes);
    final IPersonAttributes person = mock(IPersonAttributes.class);
    when(person.getName()).thenReturn("uid");
    when(person.getAttributes()).thenReturn(attributes);
    final CachingPrincipalAttributesRepository repository = new CachingPrincipalAttributesRepository(TimeUnit.MILLISECONDS.name(), 100);
    repository.setAttributeRepository(dao);
    final Principal p = new DefaultPrincipalFactory().createPrincipal("uid", Collections.singletonMap("mail", "final@example.com"));
    policy.setPrincipalAttributesRepository(repository);
    final Map<String, Object> attr = policy.getAttributes(p, CoreAuthenticationTestUtils.getRegisteredService());
    assertEquals(attr.size(), attributes.size());
}
Also used : HashMap(java.util.HashMap) DefaultPrincipalFactory(org.apereo.cas.authentication.principal.DefaultPrincipalFactory) IPersonAttributes(org.apereo.services.persondir.IPersonAttributes) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) List(java.util.List) CachingPrincipalAttributesRepository(org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository) Principal(org.apereo.cas.authentication.principal.Principal) StubPersonAttributeDao(org.apereo.services.persondir.support.StubPersonAttributeDao) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 14 with DefaultPrincipalFactory

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

the class PolicyBasedAuthenticationManagerTests method newMockHandler.

/**
     * Creates a new named mock authentication handler that either successfully validates all credentials or fails to
     * validate all credentials.
     *
     * @param name    Authentication handler name.
     * @param success True to authenticate all credentials, false to fail all credentials.
     * @return New mock authentication handler instance.
     * @throws Exception On errors.
     */
private static AuthenticationHandler newMockHandler(final String name, final boolean success) throws Exception {
    final AuthenticationHandler mock = mock(AuthenticationHandler.class);
    when(mock.getName()).thenReturn(name);
    when(mock.supports(any(Credential.class))).thenReturn(true);
    if (success) {
        final Principal p = new DefaultPrincipalFactory().createPrincipal("nobody");
        final HandlerResult result = new DefaultHandlerResult(mock, mock(CredentialMetaData.class), p);
        when(mock.authenticate(any(Credential.class))).thenReturn(result);
    } else {
        when(mock.authenticate(any(Credential.class))).thenThrow(new FailedLoginException());
    }
    return mock;
}
Also used : FailedLoginException(javax.security.auth.login.FailedLoginException) DefaultPrincipalFactory(org.apereo.cas.authentication.principal.DefaultPrincipalFactory) Principal(org.apereo.cas.authentication.principal.Principal)

Example 15 with DefaultPrincipalFactory

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

the class CentralAuthenticationServiceImplWithMockitoTests method prepareNewCAS.

@Before
public void prepareNewCAS() throws Exception {
    this.authentication = mock(Authentication.class);
    when(this.authentication.getAuthenticationDate()).thenReturn(ZonedDateTime.now(ZoneOffset.UTC));
    final CredentialMetaData metadata = new BasicCredentialMetaData(RegisteredServiceTestUtils.getCredentialsWithSameUsernameAndPassword("principal"));
    final Map<String, HandlerResult> successes = new HashMap<>();
    successes.put("handler1", new DefaultHandlerResult(mock(AuthenticationHandler.class), metadata));
    when(this.authentication.getCredentials()).thenReturn(Arrays.asList(metadata));
    when(this.authentication.getSuccesses()).thenReturn(successes);
    when(this.authentication.getPrincipal()).thenReturn(new DefaultPrincipalFactory().createPrincipal(PRINCIPAL));
    final Service service1 = getService(SVC1_ID);
    final ServiceTicket stMock = createMockServiceTicket(ST_ID, service1);
    final TicketGrantingTicket tgtRootMock = createRootTicketGrantingTicket();
    final TicketGrantingTicket tgtMock = createMockTicketGrantingTicket(TGT_ID, stMock, false, tgtRootMock, new ArrayList<>());
    when(tgtMock.getProxiedBy()).thenReturn(getService("proxiedBy"));
    final List<Authentication> authnListMock = mock(List.class);
    //Size is required to be 2, so that we can simulate proxying capabilities
    when(authnListMock.size()).thenReturn(2);
    when(authnListMock.get(anyInt())).thenReturn(this.authentication);
    when(tgtMock.getChainedAuthentications()).thenReturn(authnListMock);
    when(stMock.getGrantingTicket()).thenReturn(tgtMock);
    final Service service2 = getService(SVC2_ID);
    final ServiceTicket stMock2 = createMockServiceTicket(ST2_ID, service2);
    final TicketGrantingTicket tgtMock2 = createMockTicketGrantingTicket(TGT2_ID, stMock2, false, tgtRootMock, authnListMock);
    //Mock TicketRegistry
    mockTicketRegistry(stMock, tgtMock, stMock2, tgtMock2);
    //Mock ServicesManager
    final ServicesManager smMock = getServicesManager(service1, service2);
    final DefaultTicketFactory factory = new DefaultTicketFactory(new DefaultProxyGrantingTicketFactory(null, null, null), new DefaultTicketGrantingTicketFactory(null, null, null), new DefaultServiceTicketFactory(null, Collections.emptyMap(), false, null), new DefaultProxyTicketFactory(null, Collections.emptyMap(), null, true));
    final AuthenticationServiceSelectionPlan authenticationRequestServiceSelectionStrategies = new DefaultAuthenticationServiceSelectionPlan(new DefaultAuthenticationServiceSelectionStrategy());
    this.cas = new DefaultCentralAuthenticationService(ticketRegMock, factory, smMock, mock(LogoutManager.class), authenticationRequestServiceSelectionStrategies, new AcceptAnyAuthenticationPolicyFactory(), new DefaultPrincipalFactory(), null);
    this.cas.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
}
Also used : HashMap(java.util.HashMap) DefaultProxyTicketFactory(org.apereo.cas.ticket.factory.DefaultProxyTicketFactory) ServiceTicket(org.apereo.cas.ticket.ServiceTicket) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) DefaultHandlerResult(org.apereo.cas.authentication.DefaultHandlerResult) DefaultProxyGrantingTicketFactory(org.apereo.cas.ticket.factory.DefaultProxyGrantingTicketFactory) AcceptAnyAuthenticationPolicyFactory(org.apereo.cas.authentication.policy.AcceptAnyAuthenticationPolicyFactory) DefaultAuthenticationServiceSelectionStrategy(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionStrategy) DefaultServiceTicketFactory(org.apereo.cas.ticket.factory.DefaultServiceTicketFactory) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) RegisteredService(org.apereo.cas.services.RegisteredService) Service(org.apereo.cas.authentication.principal.Service) DefaultTicketGrantingTicketFactory(org.apereo.cas.ticket.factory.DefaultTicketGrantingTicketFactory) DefaultHandlerResult(org.apereo.cas.authentication.DefaultHandlerResult) HandlerResult(org.apereo.cas.authentication.HandlerResult) DefaultPrincipalFactory(org.apereo.cas.authentication.principal.DefaultPrincipalFactory) DefaultTicketFactory(org.apereo.cas.ticket.factory.DefaultTicketFactory) ServicesManager(org.apereo.cas.services.ServicesManager) Authentication(org.apereo.cas.authentication.Authentication) CredentialMetaData(org.apereo.cas.authentication.CredentialMetaData) BasicCredentialMetaData(org.apereo.cas.authentication.BasicCredentialMetaData) BasicCredentialMetaData(org.apereo.cas.authentication.BasicCredentialMetaData) Before(org.junit.Before)

Aggregations

DefaultPrincipalFactory (org.apereo.cas.authentication.principal.DefaultPrincipalFactory)16 Test (org.junit.Test)12 Principal (org.apereo.cas.authentication.principal.Principal)8 HashMap (java.util.HashMap)6 Authentication (org.apereo.cas.authentication.Authentication)4 DefaultHandlerResult (org.apereo.cas.authentication.DefaultHandlerResult)4 FailedLoginException (javax.security.auth.login.FailedLoginException)3 BasicCredentialMetaData (org.apereo.cas.authentication.BasicCredentialMetaData)3 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)3 Assertion (org.apereo.cas.validation.Assertion)3 ImmutableAssertion (org.apereo.cas.validation.ImmutableAssertion)3 ArrayList (java.util.ArrayList)2 PrincipalBearingCredential (org.apereo.cas.adaptors.trusted.authentication.principal.PrincipalBearingCredential)2 HandlerResult (org.apereo.cas.authentication.HandlerResult)2 ServiceTicket (org.apereo.cas.ticket.ServiceTicket)2 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1