use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication 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());
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManagerTest method testNoUsernameOnlyEmail.
@Test
public void testNoUsernameOnlyEmail() {
String email = "joe@test.org";
userDetails = mock(UserDetails.class, withSettings().extraInterfaces(Mailable.class));
when(((Mailable) userDetails).getEmailAddress()).thenReturn(email);
mockUserDetails(userDetails);
mockUaaWithUser();
UaaAuthenticationDetails uaaAuthenticationDetails = mock(UaaAuthenticationDetails.class);
when(uaaAuthenticationDetails.getOrigin()).thenReturn(origin);
when(uaaAuthenticationDetails.getClientId()).thenReturn(null);
when(uaaAuthenticationDetails.getSessionId()).thenReturn(new RandomValueStringGenerator().generate());
when(inputAuth.getDetails()).thenReturn(uaaAuthenticationDetails);
when(user.getUsername()).thenReturn(email);
when(uaaUserDatabase.retrieveUserByName(email, origin)).thenReturn(user);
when(userDetails.getUsername()).thenReturn(null);
Authentication result = manager.authenticate(inputAuth);
assertNotNull(result);
assertEquals(UaaAuthentication.class, result.getClass());
UaaAuthentication uaaAuthentication = (UaaAuthentication) result;
assertEquals(email, uaaAuthentication.getPrincipal().getName());
assertEquals(origin, uaaAuthentication.getPrincipal().getOrigin());
assertEquals(userId, uaaAuthentication.getPrincipal().getId());
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class LdapLoginAuthenticationManagerTests method test_authentication_attributes.
void test_authentication_attributes(boolean storeUserInfo) {
UaaUser user = getUaaUser();
ExtendedLdapUserImpl authDetails = getAuthDetails(user.getEmail(), user.getGivenName(), user.getFamilyName(), user.getPhoneNumber(), new AttributeInfo(UAA_MANAGER, new String[] { KARI_THE_ANT_EATER, JOHN_THE_SLOTH }), new AttributeInfo(COST_CENTER, new String[] { DENVER_CO }));
Map<String, String[]> role1 = new HashMap<>();
role1.put("cn", new String[] { "ldap.role.1.a", "ldap.role.1.b", "ldap.role.1" });
Map<String, String[]> role2 = new HashMap<>();
role2.put("cn", new String[] { "ldap.role.2.a", "ldap.role.2.b", "ldap.role.2" });
authDetails.setAuthorities(Arrays.asList(new LdapAuthority("role1", "cn=role1,ou=test,ou=com", role1), new LdapAuthority("role2", "cn=role2,ou=test,ou=com", role2)));
definition.setExternalGroupsWhitelist(Collections.singletonList("*"));
when(auth.getPrincipal()).thenReturn(authDetails);
UaaUserDatabase db = mock(UaaUserDatabase.class);
when(db.retrieveUserByName(anyString(), eq(OriginKeys.LDAP))).thenReturn(user);
when(db.retrieveUserById(anyString())).thenReturn(user);
am.setOrigin(OriginKeys.LDAP);
am.setUserDatabase(db);
// set the config flag
definition.setStoreCustomAttributes(storeUserInfo);
UaaAuthentication authentication = (UaaAuthentication) am.authenticate(auth);
UserInfo info = new UserInfo().setUserAttributes(authentication.getUserAttributes()).setRoles(Arrays.asList("ldap.role.1.a", "ldap.role.1.b", "ldap.role.1", "ldap.role.2.a", "ldap.role.2.b", "ldap.role.2"));
if (storeUserInfo) {
verify(db, times(1)).storeUserInfo(anyString(), eq(info));
} else {
verify(db, never()).storeUserInfo(anyString(), eq(info));
}
assertEquals("Expected two user attributes", 2, authentication.getUserAttributes().size());
assertNotNull("Expected cost center attribute", authentication.getUserAttributes().get(COST_CENTERS));
assertEquals(DENVER_CO, authentication.getUserAttributes().getFirst(COST_CENTERS));
assertNotNull("Expected manager attribute", authentication.getUserAttributes().get(MANAGERS));
assertEquals("Expected 2 manager attribute values", 2, authentication.getUserAttributes().get(MANAGERS).size());
assertThat(authentication.getUserAttributes().get(MANAGERS), containsInAnyOrder(JOHN_THE_SLOTH, KARI_THE_ANT_EATER));
assertThat(authentication.getAuthenticationMethods(), containsInAnyOrder("ext", "pwd"));
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class AuthenticationSuccessListenerTests method previousLoginIsSetOnTheAuthentication.
@Test
void previousLoginIsSetOnTheAuthentication() {
userPrototype.withLastLogonSuccess(123456789L);
UserAuthenticationSuccessEvent event = getEvent();
final String zoneId = event.getIdentityZoneId();
when(mockScimUserProvisioning.retrieve(this.id, zoneId)).thenReturn(getScimUser(event.getUser()));
UaaAuthentication authentication = (UaaAuthentication) event.getAuthentication();
listener.onApplicationEvent(event);
verify(authentication).setLastLoginSuccessTime(123456789L);
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManagerTest method testAuthenticateUsernamePasswordToken.
@Test
public void testAuthenticateUsernamePasswordToken() {
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userName, password);
Authentication result = manager.authenticate(auth);
assertNotNull(result);
assertEquals(UaaAuthentication.class, result.getClass());
UaaAuthentication uaaAuthentication = (UaaAuthentication) result;
assertEquals(userName, uaaAuthentication.getPrincipal().getName());
assertEquals(origin, uaaAuthentication.getPrincipal().getOrigin());
assertEquals(userId, uaaAuthentication.getPrincipal().getId());
}
Aggregations