Search in sources :

Example 1 with Mailable

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());
}
Also used : UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Mailable(org.cloudfoundry.identity.uaa.user.Mailable) LdapUserDetails(org.springframework.security.ldap.userdetails.LdapUserDetails) UserDetails(org.springframework.security.core.userdetails.UserDetails) ExtendedLdapUserDetails(org.cloudfoundry.identity.uaa.provider.ldap.ExtendedLdapUserDetails) UaaAuthenticationDetails(org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RandomValueStringGenerator(org.springframework.security.oauth2.common.util.RandomValueStringGenerator) Test(org.junit.Test)

Example 2 with Mailable

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);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Mailable(org.cloudfoundry.identity.uaa.user.Mailable) LdapUserDetails(org.springframework.security.ldap.userdetails.LdapUserDetails) UserDetails(org.springframework.security.core.userdetails.UserDetails) ExtendedLdapUserDetails(org.cloudfoundry.identity.uaa.provider.ldap.ExtendedLdapUserDetails) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) ApplicationEvent(org.springframework.context.ApplicationEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype) Test(org.junit.Test)

Example 3 with Mailable

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);
}
Also used : ExternallyIdentifiable(org.cloudfoundry.identity.uaa.user.ExternallyIdentifiable) Named(org.cloudfoundry.identity.uaa.user.Named) User(org.springframework.security.core.userdetails.User) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) VerifiableUser(org.cloudfoundry.identity.uaa.user.VerifiableUser) VerifiableUser(org.cloudfoundry.identity.uaa.user.VerifiableUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Date(java.util.Date) Mailable(org.cloudfoundry.identity.uaa.user.Mailable) DialableByPhone(org.cloudfoundry.identity.uaa.user.DialableByPhone) UserDetails(org.springframework.security.core.userdetails.UserDetails) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype)

Aggregations

Mailable (org.cloudfoundry.identity.uaa.user.Mailable)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)3 UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)2 ExtendedLdapUserDetails (org.cloudfoundry.identity.uaa.provider.ldap.ExtendedLdapUserDetails)2 UaaUser (org.cloudfoundry.identity.uaa.user.UaaUser)2 UaaUserPrototype (org.cloudfoundry.identity.uaa.user.UaaUserPrototype)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Authentication (org.springframework.security.core.Authentication)2 LdapUserDetails (org.springframework.security.ldap.userdetails.LdapUserDetails)2 Date (java.util.Date)1 UaaAuthenticationDetails (org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails)1 DialableByPhone (org.cloudfoundry.identity.uaa.user.DialableByPhone)1 ExternallyIdentifiable (org.cloudfoundry.identity.uaa.user.ExternallyIdentifiable)1 Named (org.cloudfoundry.identity.uaa.user.Named)1 VerifiableUser (org.cloudfoundry.identity.uaa.user.VerifiableUser)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 User (org.springframework.security.core.userdetails.User)1