Search in sources :

Example 1 with ExternalIdentityProviderDefinition

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);
        }
    }
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserInfo(org.cloudfoundry.identity.uaa.user.UserInfo) ExternalIdentityProviderDefinition(org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition) LinkedList(java.util.LinkedList)

Example 2 with ExternalIdentityProviderDefinition

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());
}
Also used : UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) IdentityProvider(org.cloudfoundry.identity.uaa.provider.IdentityProvider) UserInfo(org.cloudfoundry.identity.uaa.user.UserInfo) ExternalIdentityProviderDefinition(org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

ExternalIdentityProviderDefinition (org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition)2 UserInfo (org.cloudfoundry.identity.uaa.user.UserInfo)2 LinkedList (java.util.LinkedList)1 UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)1 UaaPrincipal (org.cloudfoundry.identity.uaa.authentication.UaaPrincipal)1 IdentityProvider (org.cloudfoundry.identity.uaa.provider.IdentityProvider)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Authentication (org.springframework.security.core.Authentication)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1