use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.
the class PersonDirectoryPrincipalResolverTests method verifyNullPrincipal.
@Test
public void verifyNullPrincipal() {
val context = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(new StubPersonAttributeDao(new HashMap<>(0))).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(String::trim).useCurrentPrincipalId(false).resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
val resolver = new PersonDirectoryPrincipalResolver(context);
val p = resolver.resolve(() -> null, Optional.of(CoreAuthenticationTestUtils.getPrincipal()), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
assertNull(p);
}
use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.
the class PersonDirectoryPrincipalResolverTests method verifyMultiplePrincipalAttributeNames.
@Test
public void verifyMultiplePrincipalAttributeNames() {
val context1 = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(CoreAuthenticationTestUtils.getAttributeRepository()).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(formUserId -> formUserId).useCurrentPrincipalId(false).resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
val resolver = new PersonDirectoryPrincipalResolver(context1);
val context2 = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(new StubPersonAttributeDao(Collections.singletonMap("something", CollectionUtils.wrap("principal-id")))).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(formUserId -> formUserId).useCurrentPrincipalId(false).principalAttributeNames(" invalid, something").resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
val resolver2 = new PersonDirectoryPrincipalResolver(context2);
val chain = new ChainingPrincipalResolver(new DefaultPrincipalElectionStrategy(), casProperties);
chain.setChain(Arrays.asList(new EchoingPrincipalResolver(), resolver, resolver2));
val p = chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), Optional.of(CoreAuthenticationTestUtils.getPrincipal("somethingelse", Collections.singletonMap(ATTR_1, List.of("value")))), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
assertNotNull(p);
assertEquals("principal-id", p.getId());
}
use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.
the class CoreAuthenticationTestUtils method getAttributeRepository.
public static StubPersonAttributeDao getAttributeRepository() {
val attributes = new HashMap<String, List<Object>>();
attributes.put("uid", CollectionUtils.wrap(CONST_USERNAME));
attributes.put("cn", CollectionUtils.wrap(CONST_USERNAME.toUpperCase()));
attributes.put("givenName", CollectionUtils.wrap(CONST_USERNAME));
attributes.put("mail", CollectionUtils.wrap(CONST_USERNAME + "@example.org"));
attributes.put("memberOf", CollectionUtils.wrapList("system", "admin", "cas", "staff"));
return new StubPersonAttributeDao(attributes);
}
use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.
the class RegisteredServiceAttributeReleasePolicyTests method checkServiceAttributeFilterByAttributeRepositoryId.
@Test
public void checkServiceAttributeFilterByAttributeRepositoryId() {
val policy = new ReturnAllAttributeReleasePolicy();
val attributes = new HashMap<String, List<Object>>();
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" }));
val person = mock(IPersonAttributes.class);
when(person.getName()).thenReturn("uid");
when(person.getAttributes()).thenReturn(attributes);
val stub = new StubPersonAttributeDao(attributes);
stub.setId("SampleStubRepository");
val dao = new MergingPersonAttributeDaoImpl();
dao.setPersonAttributeDaos(List.of(stub));
ApplicationContextProvider.registerBeanIntoApplicationContext(this.applicationContext, dao, PrincipalResolver.BEAN_NAME_ATTRIBUTE_REPOSITORY);
val repository = new CachingPrincipalAttributesRepository(TimeUnit.MILLISECONDS.name(), 0);
val p = PrincipalFactoryUtils.newPrincipalFactory().createPrincipal("uid", Collections.singletonMap("mail", List.of("final@example.com")));
repository.setAttributeRepositoryIds(CollectionUtils.wrapSet("SampleStubRepository".toUpperCase()));
policy.setPrincipalAttributesRepository(repository);
val context = RegisteredServiceAttributeReleasePolicyContext.builder().registeredService(CoreAttributesTestUtils.getRegisteredService()).service(CoreAttributesTestUtils.getService()).principal(p).build();
var attr = policy.getAttributes(context);
assertEquals(attr.size(), attributes.size() + 1);
repository.setAttributeRepositoryIds(CollectionUtils.wrapSet("DoesNotExist"));
policy.setPrincipalAttributesRepository(repository);
attr = policy.getAttributes(context);
assertEquals(1, attr.size());
}
use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.
the class CoreAuthenticationUtilsTests method verifyPersonDirectoryOverrides.
@Test
public void verifyPersonDirectoryOverrides() {
val principal = new PersonDirectoryPrincipalResolverProperties();
val personDirectory = new PersonDirectoryPrincipalResolverProperties();
val principalResolutionContext = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), principal, personDirectory);
assertFalse(principalResolutionContext.isUseCurrentPrincipalId());
assertTrue(principalResolutionContext.isResolveAttributes());
assertFalse(principalResolutionContext.isReturnNullIfNoAttributes());
assertTrue(principalResolutionContext.getActiveAttributeRepositoryIdentifiers().isEmpty());
assertTrue(principalResolutionContext.getPrincipalAttributeNames().isEmpty());
personDirectory.setUseExistingPrincipalId(TriStateBoolean.TRUE);
personDirectory.setAttributeResolutionEnabled(TriStateBoolean.TRUE);
personDirectory.setReturnNull(TriStateBoolean.TRUE);
personDirectory.setAttributeResolutionEnabled(TriStateBoolean.FALSE);
personDirectory.setActiveAttributeRepositoryIds("test1,test2");
personDirectory.setPrincipalAttribute("principalAttribute");
val principalResolutionContext2 = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), principal, personDirectory);
assertTrue(principalResolutionContext2.isUseCurrentPrincipalId());
assertFalse(principalResolutionContext2.isResolveAttributes());
assertTrue(principalResolutionContext2.isReturnNullIfNoAttributes());
assertEquals(2, principalResolutionContext2.getActiveAttributeRepositoryIdentifiers().size());
assertEquals("principalAttribute", principalResolutionContext2.getPrincipalAttributeNames());
principal.setUseExistingPrincipalId(TriStateBoolean.FALSE);
principal.setAttributeResolutionEnabled(TriStateBoolean.FALSE);
principal.setReturnNull(TriStateBoolean.FALSE);
principal.setAttributeResolutionEnabled(TriStateBoolean.TRUE);
principal.setActiveAttributeRepositoryIds("test1,test2,test3");
principal.setPrincipalAttribute("principalAttribute2");
val principalResolutionContext3 = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), principal, personDirectory);
assertFalse(principalResolutionContext3.isUseCurrentPrincipalId());
assertTrue(principalResolutionContext3.isResolveAttributes());
assertFalse(principalResolutionContext3.isReturnNullIfNoAttributes());
assertEquals(3, principalResolutionContext3.getActiveAttributeRepositoryIdentifiers().size());
assertEquals("principalAttribute2", principalResolutionContext3.getPrincipalAttributeNames());
val principalResolutionContext4 = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), personDirectory);
assertTrue(principalResolutionContext4.isUseCurrentPrincipalId());
assertFalse(principalResolutionContext4.isResolveAttributes());
assertTrue(principalResolutionContext4.isReturnNullIfNoAttributes());
assertEquals(2, principalResolutionContext4.getActiveAttributeRepositoryIdentifiers().size());
assertEquals("principalAttribute", principalResolutionContext4.getPrincipalAttributeNames());
}
Aggregations