use of org.cloudfoundry.identity.uaa.user.Mailable 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.user.Mailable in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManagerTest method testAuthenticateInvitedUserWithoutAcceptance.
@Test
public void testAuthenticateInvitedUserWithoutAcceptance() {
String username = "guyWhoDoesNotAcceptInvites";
String origin = LDAP;
String email = "guy@ldap.org";
UserDetails ldapUserDetails = mock(ExtendedLdapUserDetails.class, withSettings().extraInterfaces(Mailable.class));
when(ldapUserDetails.getUsername()).thenReturn(username);
when(ldapUserDetails.getPassword()).thenReturn(password);
when(ldapUserDetails.getAuthorities()).thenReturn(null);
when(ldapUserDetails.isAccountNonExpired()).thenReturn(true);
when(ldapUserDetails.isAccountNonLocked()).thenReturn(true);
when(ldapUserDetails.isCredentialsNonExpired()).thenReturn(true);
when(ldapUserDetails.isEnabled()).thenReturn(true);
when(((Mailable) ldapUserDetails).getEmailAddress()).thenReturn(email);
// Invited users are created with their email as their username.
UaaUser invitedUser = addUserToDb(email, userId, origin, email);
when(invitedUser.modifyAttributes(anyString(), anyString(), anyString(), anyString(), anyBoolean())).thenReturn(invitedUser);
UaaUser updatedUser = new UaaUser(new UaaUserPrototype().withUsername(username).withId(userId).withOrigin(origin).withEmail(email));
when(invitedUser.modifyUsername(username)).thenReturn(updatedUser);
manager = new LdapLoginAuthenticationManager(null);
setupManager();
manager.setProviderProvisioning(null);
manager.setOrigin(origin);
when(uaaUserDatabase.retrieveUserByName(eq(username), eq(origin))).thenThrow(new UsernameNotFoundException(""));
when(uaaUserDatabase.retrieveUserByEmail(eq(email), eq(origin))).thenReturn(invitedUser);
Authentication ldapAuth = mock(Authentication.class);
when(ldapAuth.getPrincipal()).thenReturn(ldapUserDetails);
manager.authenticate(ldapAuth);
userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
verify(applicationEventPublisher, atLeastOnce()).publishEvent(userArgumentCaptor.capture());
for (ApplicationEvent event : userArgumentCaptor.getAllValues()) {
assertNotEquals(event.getClass(), NewUserAuthenticatedEvent.class);
}
}
use of org.cloudfoundry.identity.uaa.user.Mailable in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManager method getUser.
protected UaaUser getUser(Authentication request, ExternalAuthenticationDetails authDetails) {
UserDetails userDetails;
if (request.getPrincipal() instanceof UserDetails) {
userDetails = (UserDetails) request.getPrincipal();
} else if (request instanceof UsernamePasswordAuthenticationToken) {
String username = request.getPrincipal().toString();
String password = request.getCredentials() != null ? request.getCredentials().toString() : "";
userDetails = new User(username, password, true, true, true, true, UaaAuthority.USER_AUTHORITIES);
} else if (request.getPrincipal() == null) {
logger.debug(this.getClass().getName() + "[" + name + "] cannot process null principal");
return null;
} else {
logger.debug(this.getClass().getName() + "[" + name + "] cannot process request of type: " + request.getClass().getName());
return null;
}
String name = userDetails.getUsername();
String email = null;
if (userDetails instanceof Mailable) {
email = ((Mailable) userDetails).getEmailAddress();
if (name == null) {
name = email;
}
}
if (email == null) {
email = generateEmailIfNull(name);
}
String givenName = null;
String familyName = null;
if (userDetails instanceof Named) {
Named names = (Named) userDetails;
givenName = names.getGivenName();
familyName = names.getFamilyName();
}
String phoneNumber = (userDetails instanceof DialableByPhone) ? ((DialableByPhone) userDetails).getPhoneNumber() : null;
String externalId = (userDetails instanceof ExternallyIdentifiable) ? ((ExternallyIdentifiable) userDetails).getExternalId() : name;
boolean verified = (userDetails instanceof VerifiableUser) ? ((VerifiableUser) userDetails).isVerified() : false;
UaaUserPrototype userPrototype = new UaaUserPrototype().withVerified(verified).withUsername(name).withPassword("").withEmail(email).withAuthorities(UaaAuthority.USER_AUTHORITIES).withGivenName(givenName).withFamilyName(familyName).withCreated(new Date()).withModified(new Date()).withOrigin(getOrigin()).withExternalId(externalId).withZoneId(IdentityZoneHolder.get().getId()).withPhoneNumber(phoneNumber);
return new UaaUser(userPrototype);
}
Aggregations