use of org.springframework.security.oauth2.common.util.RandomValueStringGenerator in project uaa by cloudfoundry.
the class UserInfoEndpointMockMvcTests method setUp.
@BeforeEach
void setUp() throws Exception {
testClient = new TestClient(mockMvc);
String adminToken = testClient.getClientCredentialsOAuthAccessToken("admin", "adminsecret", "clients.read clients.write clients.secret scim.read scim.write clients.admin");
String authorities = "scim.read,scim.write,password.write,oauth.approvals,scim.create,openid";
MockMvcUtils.createClient(mockMvc, adminToken, clientId, clientSecret, Collections.singleton("oauth"), Arrays.asList("openid", USER_ATTRIBUTES, ROLES), Arrays.asList("client_credentials", "password"), authorities);
String userName = new RandomValueStringGenerator().generate() + "@test.org";
user = new ScimUser(null, userName, "PasswordResetUserFirst", "PasswordResetUserLast");
user.setPrimaryEmail(user.getUserName());
user.setPassword("secr3T");
user = MockMvcUtils.createUser(mockMvc, adminToken, user);
webApplicationContext.getBean(UaaUserDatabase.class).updateLastLogonTime(user.getId());
userAttributes = new LinkedMultiValueMap<>();
userAttributes.add("single", "1");
userAttributes.add("multi", "2");
userAttributes.add("multi", "3");
roles = Arrays.asList("role1", "role2", "role3");
UserInfo userInfo = new UserInfo().setUserAttributes(userAttributes).setRoles(roles);
webApplicationContext.getBean(UaaUserDatabase.class).storeUserInfo(user.getId(), userInfo);
}
use of org.springframework.security.oauth2.common.util.RandomValueStringGenerator 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.springframework.security.oauth2.common.util.RandomValueStringGenerator 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.springframework.security.oauth2.common.util.RandomValueStringGenerator in project uaa by cloudfoundry.
the class ClientAdminEndpointsIntegrationTests method createClientWithCommaDelimitedScopesValidatesAllTheScopes.
@Test
public void createClientWithCommaDelimitedScopesValidatesAllTheScopes() throws Exception {
// log in as admin
OAuth2AccessToken adminToken = getClientCredentialsAccessToken("");
HttpHeaders adminHeaders = getAuthenticatedHeaders(adminToken);
// make client that can create other clients
String newClientId = new RandomValueStringGenerator().generate();
BaseClientDetails clientCreator = new BaseClientDetails(newClientId, "", "clients.write,uaa.user", "client_credentials", "clients.write,uaa.user");
clientCreator.setClientSecret("secret");
ResponseEntity<UaaException> result = serverRunning.getRestTemplate().exchange(serverRunning.getUrl("/oauth/clients"), HttpMethod.POST, new HttpEntity<>(clientCreator, adminHeaders), UaaException.class);
// ensure success
assertEquals(HttpStatus.CREATED, result.getStatusCode());
// log in as new client
OAuth2AccessToken token = getClientCredentialsAccessToken(clientCreator.getClientId(), clientCreator.getClientSecret(), "");
HttpHeaders headers = getAuthenticatedHeaders(token);
// make client with restricted scopes
BaseClientDetails invalidClient = new BaseClientDetails(new RandomValueStringGenerator().generate(), "", newClientId + ".admin,uaa.admin", "client_credentials", "uaa.none");
invalidClient.setClientSecret("secret");
ResponseEntity<UaaException> invalidClientRequest = serverRunning.getRestTemplate().exchange(serverRunning.getUrl("/oauth/clients"), HttpMethod.POST, new HttpEntity<>(invalidClient, headers), UaaException.class);
// ensure correct failure
assertEquals(HttpStatus.BAD_REQUEST, invalidClientRequest.getStatusCode());
assertEquals("invalid_client", invalidClientRequest.getBody().getErrorCode());
assertTrue("Error message is unexpected", invalidClientRequest.getBody().getMessage().startsWith("uaa.admin is not an allowed scope for caller"));
}
use of org.springframework.security.oauth2.common.util.RandomValueStringGenerator in project uaa by cloudfoundry.
the class OpenIdTokenAuthorizationWithApprovalIntegrationTests method doOpenIdHybridFlowForLoginClient.
private void doOpenIdHybridFlowForLoginClient(Set<String> responseTypes, String responseTypeMatcher) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL));
AuthorizationCodeResourceDetails resource = testAccounts.getDefaultAuthorizationCodeResource();
StringBuilder responseType = new StringBuilder();
Iterator<String> rTypes = responseTypes.iterator();
while (rTypes.hasNext()) {
String type = rTypes.next();
responseType.append(type);
if (rTypes.hasNext()) {
responseType.append(" ");
}
}
String state = new RandomValueStringGenerator().generate();
String clientId = resource.getClientId();
String redirectUri = resource.getPreEstablishedRedirectUri();
String clientSecret = resource.getClientSecret();
String uri = serverRunning.getUrl("/oauth/authorize?response_type={response_type}&" + "state={state}&client_id={client_id}&client_secret={clientSecret}&redirect_uri={redirect_uri}&source=login&user_id={userId}&add_new=false");
HttpEntity<Void> request = new HttpEntity<>(null, headers);
ResponseEntity<Map> result = loginClient.exchange(serverRunning.getUrl(uri), HttpMethod.POST, request, Map.class, responseType, state, clientId, clientSecret, redirectUri, user.getId());
assertEquals(HttpStatus.FOUND, result.getStatusCode());
String location = UriUtils.decode(result.getHeaders().getLocation().toString(), "UTF-8");
assertTrue("Wrong location: " + location, location.matches(resource.getPreEstablishedRedirectUri() + responseTypeMatcher));
}
Aggregations