use of org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManager method populateAuthenticationAttributes.
protected void populateAuthenticationAttributes(UaaAuthentication authentication, Authentication request, ExternalAuthenticationDetails authenticationData) {
if (request.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) request.getPrincipal();
authentication.setUserAttributes(getUserAttributes(userDetails));
authentication.setExternalGroups(new HashSet<>(getExternalUserAuthorities(userDetails)));
}
if (authentication.getAuthenticationMethods() == null) {
authentication.setAuthenticationMethods(new HashSet<>());
}
authentication.getAuthenticationMethods().add("ext");
if ((hasUserAttributes(authentication) || hasExternalGroups(authentication)) && getProviderProvisioning() != null) {
IdentityProvider<ExternalIdentityProviderDefinition> provider = getProviderProvisioning().retrieveByOrigin(getOrigin(), IdentityZoneHolder.get().getId());
if (provider.getConfig() != null && provider.getConfig().isStoreCustomAttributes()) {
logger.debug("Storing custom attributes for user_id:" + authentication.getPrincipal().getId());
UserInfo userInfo = new UserInfo().setUserAttributes(authentication.getUserAttributes()).setRoles(new LinkedList(ofNullable(authentication.getExternalGroups()).orElse(EMPTY_SET)));
getUserDatabase().storeUserInfo(authentication.getPrincipal().getId(), userInfo);
}
}
}
use of org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManagerTest method testPopulateAttributesStoresCustomAttributesAndRoles.
@Test
public void testPopulateAttributesStoresCustomAttributesAndRoles() {
manager = new LdapLoginAuthenticationManager(null);
setupManager();
manager.setOrigin(origin);
IdentityProvider provider = mock(IdentityProvider.class);
ExternalIdentityProviderDefinition providerDefinition = new ExternalIdentityProviderDefinition();
when(provider.getConfig()).thenReturn(providerDefinition);
when(providerProvisioning.retrieveByOrigin(eq(origin), anyString())).thenReturn(provider);
UaaAuthentication uaaAuthentication = mock(UaaAuthentication.class);
UaaPrincipal uaaPrincipal = mock(UaaPrincipal.class);
when(uaaPrincipal.getId()).thenReturn("id");
when(uaaAuthentication.getPrincipal()).thenReturn(uaaPrincipal);
when(uaaAuthentication.getUserAttributes()).thenReturn(userAttributes);
HashSet<String> externalGroupsOnAuthentication = new HashSet<>(externalGroups);
when(uaaAuthentication.getExternalGroups()).thenReturn(externalGroupsOnAuthentication);
providerDefinition.setStoreCustomAttributes(false);
manager.populateAuthenticationAttributes(uaaAuthentication, mock(Authentication.class), null);
verify(manager.getUserDatabase(), never()).storeUserInfo(anyString(), any());
// when there are both attributes and groups, store them
providerDefinition.setStoreCustomAttributes(true);
manager.populateAuthenticationAttributes(uaaAuthentication, mock(Authentication.class), null);
UserInfo userInfo = new UserInfo().setUserAttributes(userAttributes).setRoles(externalGroups);
verify(manager.getUserDatabase(), times(1)).storeUserInfo(eq("id"), eq(userInfo));
// when provider is null do not store anything
reset(manager.getUserDatabase());
manager.setProviderProvisioning(null);
manager.populateAuthenticationAttributes(uaaAuthentication, mock(Authentication.class), null);
verify(manager.getUserDatabase(), never()).storeUserInfo(anyString(), any());
manager.setProviderProvisioning(providerProvisioning);
// when attributes is empty but roles have contents, store it
reset(manager.getUserDatabase());
userAttributes.clear();
manager.populateAuthenticationAttributes(uaaAuthentication, mock(Authentication.class), null);
userInfo = new UserInfo().setUserAttributes(userAttributes).setRoles(externalGroups);
verify(manager.getUserDatabase(), times(1)).storeUserInfo(eq("id"), eq(userInfo));
// when attributes and roles are both empty, do not store anything
reset(manager.getUserDatabase());
userAttributes.clear();
externalGroupsOnAuthentication.clear();
manager.populateAuthenticationAttributes(uaaAuthentication, mock(Authentication.class), null);
verify(manager.getUserDatabase(), never()).storeUserInfo(anyString(), any());
}
Aggregations