DefaultSAMLUserDetailsService implements SAMLUserDetailsService { @Override public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException { String unspecifiedNameId = credential.getNameID().getValue(); Map<String, List<String>> properties = getAttributes(credential); String clientId = credential.getRelayState(); List<String> persistentIds = properties.get(EDU_PERSON_TARGETED_ID); String sub; if (CollectionUtils.isEmpty(persistentIds)) { sub = hashedPairwiseIdentifierService.getIdentifier(unspecifiedNameId, clientId); LOG.info("Using the hashedPairwiseIdentifierService for the {} sub for {} and {}", sub, unspecifiedNameId, clientId); } else { sub = persistentIds.get(0); LOG.info("Using the persistent identifier for the {} sub for {} and {}", sub, unspecifiedNameId, clientId); } String authenticatingAuthority = getAuthenticatingAuthority(credential); FederatedUserInfo existingUserInfo = (FederatedUserInfo) extendedUserInfoService.getByUsernameAndClientId(sub, clientId); FederatedUserInfo userInfo = this.buildUserInfo(unspecifiedNameId, sub, authenticatingAuthority, properties); if (existingUserInfo == null) { extendedUserInfoService.saveUserInfo(userInfo); } else if (!existingUserInfo.hashed().equals(userInfo.hashed())) { userInfo.setId(existingUserInfo.getId()); extendedUserInfoService.saveUserInfo(userInfo); } return new SAMLUser(sub, clientId.equals(this.localSpEntityId), clientId); } DefaultSAMLUserDetailsService(String localSpEntityId); @Override Object loadUserBySAML(SAMLCredential credential); void setExtendedUserInfoService(FederatedUserInfoService extendedUserInfoService); void setHashedPairwiseIdentifierService(HashedPairwiseIdentifierService hashedPairwiseIdentifierService); static final String PERSISTENT_NAME_ID_FORMAT; static final String EDU_PERSON_TARGETED_ID; }
@Test public void testLoadUserBySAMLWithPersistentIdentifier() throws Exception { SAMLCredential credential = SAMLTestHelper.parseSAMLCredential(SP_ENTITY_ID, "saml/assertionResponse.xml"); SAMLUser user = (SAMLUser) subject.loadUserBySAML(credential); assertTrue(user.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))); assertEquals(persistentId, user.getUsername()); assertEquals(federatedUserInfo.toString(), saveUserInfoArgument.toString()); }
@Test public void testReprovisionUserWhenAttributesChange() throws Exception { findByUserNameReturnValue = new FederatedUserInfo(); findByUserNameReturnValue.setSchacHomeOrganizationType("outdated"); SAMLCredential samlCredential = SAMLTestHelper.parseSAMLCredential("http: subject.loadUserBySAML(samlCredential); assertEquals("different", saveUserInfoArgument.getSchacHomeOrganizationType()); }
@Test public void testLoadSAmlUserWithoutPersistentIdentifier() throws Exception { SAMLCredential samlCredential = SAMLTestHelper.parseSAMLCredential("http: SAMLUser samlUser = (SAMLUser) subject.loadUserBySAML(samlCredential); assertEquals("83e491ac-aede-378e-b7cb-40347f03d786", samlUser.getUsername()); }
@Test public void testLoadUserBySAMLWithEmptyAttributes() throws Exception { SAMLCredential samlCredential = SAMLTestHelper.parseSAMLCredential("http: SAMLUser samlUser = (SAMLUser) subject.loadUserBySAML(samlCredential); assertEquals("fd9021b35ce0e2bb4fc28d1781e6cbb9eb720fed", samlUser.getUsername()); FederatedUserInfo emptyUserInfo = new FederatedUserInfo(); emptyUserInfo.setSub(samlUser.getUsername()); emptyUserInfo.setUnspecifiedNameId("urn:collab:person:example.com:test"); emptyUserInfo.setAuthenticatingAuthority("http: emptyUserInfo.setEduPersonTargetedId("fd9021b35ce0e2bb4fc28d1781e6cbb9eb720fed"); assertEquals(emptyUserInfo.toString(), saveUserInfoArgument.toString()); }