use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManagerTest method testAuthenticateCreateUserWithLdapUserDetailsPrincipal.
@Test
public void testAuthenticateCreateUserWithLdapUserDetailsPrincipal() {
String dn = "cn=" + userName + ",ou=Users,dc=test,dc=com";
String origin = LDAP;
String email = "joe@test.org";
LdapUserDetails baseLdapUserDetails = mock(LdapUserDetails.class);
mockUserDetails(baseLdapUserDetails);
when(baseLdapUserDetails.getDn()).thenReturn(dn);
HashMap<String, String[]> ldapAttrs = new HashMap<>();
String ldapMailAttrName = "email";
ldapAttrs.put(ldapMailAttrName, new String[] { email });
ExtendedLdapUserImpl ldapUserDetails = new ExtendedLdapUserImpl(baseLdapUserDetails, ldapAttrs);
ldapUserDetails.setMailAttributeName(ldapMailAttrName);
manager = new LdapLoginAuthenticationManager(null);
setupManager();
manager.setProviderProvisioning(null);
manager.setOrigin(origin);
when(user.getEmail()).thenReturn(email);
when(user.getOrigin()).thenReturn(origin);
when(uaaUserDatabase.retrieveUserByName(eq(userName), eq(origin))).thenReturn(null).thenReturn(user);
when(inputAuth.getPrincipal()).thenReturn(ldapUserDetails);
Authentication result = manager.authenticate(inputAuth);
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());
userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
verify(applicationEventPublisher, times(3)).publishEvent(userArgumentCaptor.capture());
assertEquals(3, userArgumentCaptor.getAllValues().size());
NewUserAuthenticatedEvent event = (NewUserAuthenticatedEvent) userArgumentCaptor.getAllValues().get(0);
assertEquals(origin, event.getUser().getOrigin());
assertEquals(dn, event.getUser().getExternalId());
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManagerTest method testAuthenticateWithAuthDetails.
@Test
public void testAuthenticateWithAuthDetails() {
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);
Authentication result = manager.authenticate(inputAuth);
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());
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class ExternalLoginAuthenticationManagerTest method testAmpersandInEndOfName.
@Test
public void testAmpersandInEndOfName() {
String name = "filip@hanik@";
String actual = name.replaceAll("@", "") + "@user.from." + origin + ".cf";
when(userDetails.getUsername()).thenReturn(name);
when(user.getUsername()).thenReturn(name);
when(uaaUserDatabase.retrieveUserByName(eq(name), eq(origin))).thenReturn(null).thenReturn(user);
Authentication result = manager.authenticate(inputAuth);
assertNotNull(result);
assertEquals(UaaAuthentication.class, result.getClass());
UaaAuthentication uaaAuthentication = (UaaAuthentication) result;
assertEquals(name, uaaAuthentication.getPrincipal().getName());
assertEquals(origin, uaaAuthentication.getPrincipal().getOrigin());
assertEquals(userId, uaaAuthentication.getPrincipal().getId());
userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
verify(applicationEventPublisher, times(2)).publishEvent(userArgumentCaptor.capture());
assertEquals(2, userArgumentCaptor.getAllValues().size());
NewUserAuthenticatedEvent event = (NewUserAuthenticatedEvent) userArgumentCaptor.getAllValues().get(0);
assertEquals(origin, event.getUser().getOrigin());
assertEquals(actual, event.getUser().getEmail());
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class DefaultSecurityContextAccessorTests method zoneAdminUserIsNotAdmin_BecauseOriginIsNotUaa.
@Test
void zoneAdminUserIsNotAdmin_BecauseOriginIsNotUaa() {
BaseClientDetails client = new BaseClientDetails();
List<SimpleGrantedAuthority> authorities = new LinkedList<>();
authorities.add(new SimpleGrantedAuthority("zones." + IdentityZoneHolder.get().getId() + ".admin"));
client.setAuthorities(authorities);
UaaPrincipal principal = new UaaPrincipal("id", "username", "email", OriginKeys.UAA, null, MultitenancyFixture.identityZone("test", "test").getId());
UaaAuthentication userAuthentication = new UaaAuthentication(principal, authorities, new UaaAuthenticationDetails(new MockHttpServletRequest()));
AuthorizationRequest authorizationRequest = new AuthorizationRequest("admin", UaaStringUtils.getStringsFromAuthorities(authorities));
authorizationRequest.setResourceIdsAndAuthoritiesFromClientDetails(client);
SecurityContextHolder.getContext().setAuthentication(new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication));
assertFalse(defaultSecurityContextAccessor.isAdmin());
}
use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.
the class TokenMvcMockTests method setAuthentication.
private void setAuthentication(MockHttpSession session, ScimUser developer, boolean forcePasswordChange, String... authMethods) {
UaaPrincipal p = new UaaPrincipal(developer.getId(), developer.getUserName(), developer.getPrimaryEmail(), OriginKeys.UAA, "", IdentityZoneHolder.get().getId());
UaaAuthentication auth = new UaaAuthentication(p, UaaAuthority.USER_AUTHORITIES, new UaaAuthenticationDetails(false, "clientId", OriginKeys.ORIGIN, "sessionId"));
SessionUtils.setPasswordChangeRequired(session, forcePasswordChange);
auth.setAuthenticationMethods(new HashSet<>(Arrays.asList(authMethods)));
assertTrue(auth.isAuthenticated());
SecurityContextHolder.getContext().setAuthentication(auth);
session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, new MockSecurityContext(auth));
}
Aggregations