use of org.maxkey.authz.oauth2.provider.AuthorizationRequest in project uaa by cloudfoundry.
the class DeprecatedUaaTokenServicesTests method test_missing_required_user_groups.
@Test
public void test_missing_required_user_groups() {
tokenSupport.defaultClient.addAdditionalInformation(REQUIRED_USER_GROUPS, singletonList("uaa.admin"));
AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, tokenSupport.requestedAuthScopes);
authorizationRequest.setResourceIds(new HashSet<>(tokenSupport.resourceIds));
Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
azParameters.put(GRANT_TYPE, GRANT_TYPE_PASSWORD);
authorizationRequest.setRequestParameters(azParameters);
Authentication userAuthentication = tokenSupport.defaultUserAuthentication;
OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication);
expectedException.expect(InvalidTokenException.class);
expectedException.expectMessage("User does not meet the client's required group criteria.");
tokenServices.createAccessToken(authentication);
}
use of org.maxkey.authz.oauth2.provider.AuthorizationRequest in project uaa by cloudfoundry.
the class DeprecatedUaaTokenServicesTests method performPasswordGrant.
private OAuth2AccessToken performPasswordGrant(String tokenFormat) {
AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, tokenSupport.requestedAuthScopes);
authorizationRequest.setResourceIds(new HashSet<>(tokenSupport.resourceIds));
Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
azParameters.put(GRANT_TYPE, GRANT_TYPE_PASSWORD);
azParameters.put(REQUEST_TOKEN_FORMAT, tokenFormat);
authorizationRequest.setRequestParameters(azParameters);
Authentication userAuthentication = tokenSupport.defaultUserAuthentication;
OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication);
return tokenServices.createAccessToken(authentication);
}
use of org.maxkey.authz.oauth2.provider.AuthorizationRequest in project uaa by cloudfoundry.
the class DeprecatedUaaTokenServicesTests method testCreateAccessTokenAuthcodeGrantNarrowerScopes.
@Test
public void testCreateAccessTokenAuthcodeGrantNarrowerScopes() {
Calendar expiresAt = Calendar.getInstance();
expiresAt.add(Calendar.MILLISECOND, 3000);
Calendar updatedAt = Calendar.getInstance();
updatedAt.add(Calendar.MILLISECOND, -1000);
tokenSupport.approvalStore.addApproval(new Approval().setUserId(tokenSupport.userId).setClientId(CLIENT_ID).setScope(tokenSupport.readScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED).setLastUpdatedAt(updatedAt.getTime()), IdentityZoneHolder.get().getId());
tokenSupport.approvalStore.addApproval(new Approval().setUserId(tokenSupport.userId).setClientId(CLIENT_ID).setScope(tokenSupport.writeScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED).setLastUpdatedAt(updatedAt.getTime()), IdentityZoneHolder.get().getId());
// First Request
AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, tokenSupport.requestedAuthScopes);
authorizationRequest.setResourceIds(new HashSet<>(tokenSupport.resourceIds));
Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
azParameters.put(GRANT_TYPE, GRANT_TYPE_AUTHORIZATION_CODE);
authorizationRequest.setRequestParameters(azParameters);
Authentication userAuthentication = tokenSupport.defaultUserAuthentication;
OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication);
OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);
assertThat(accessToken, scope(is(tokenSupport.requestedAuthScopes)));
OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
assertThat(refreshToken, is(not(nullValue())));
assertThat(refreshToken, OAuth2RefreshTokenMatchers.scope(is(tokenSupport.requestedAuthScopes)));
assertThat(refreshToken, OAuth2RefreshTokenMatchers.audience(is(tokenSupport.resourceIds)));
// Second request with reduced scopes
AuthorizationRequest reducedScopeAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, tokenSupport.readScope);
reducedScopeAuthorizationRequest.setResourceIds(new HashSet<>(tokenSupport.resourceIds));
Map<String, String> refreshAzParameters = new HashMap<>(reducedScopeAuthorizationRequest.getRequestParameters());
refreshAzParameters.put(GRANT_TYPE, GRANT_TYPE_REFRESH_TOKEN);
reducedScopeAuthorizationRequest.setRequestParameters(refreshAzParameters);
OAuth2Authentication reducedScopeAuthentication = new OAuth2Authentication(reducedScopeAuthorizationRequest.createOAuth2Request(), userAuthentication);
OAuth2AccessToken reducedScopeAccessToken = tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(), tokenSupport.requestFactory.createTokenRequest(reducedScopeAuthorizationRequest, "refresh_token"));
// AT should have the new scopes, RT should be the same
assertThat(reducedScopeAccessToken, scope(is(tokenSupport.readScope)));
assertEquals(reducedScopeAccessToken.getRefreshToken(), accessToken.getRefreshToken());
}
use of org.maxkey.authz.oauth2.provider.AuthorizationRequest in project uaa by cloudfoundry.
the class DeprecatedUaaTokenServicesTests method testCreateAccessTokenForAnotherIssuer.
@Test
public void testCreateAccessTokenForAnotherIssuer() throws Exception {
String subdomain = "test-zone-subdomain";
IdentityZone identityZone = getIdentityZone(subdomain);
identityZone.setConfig(JsonUtils.readValue("{\"issuer\": \"http://uaamaster:8080/uaa\"}", IdentityZoneConfiguration.class));
identityZone.getConfig().getTokenPolicy().setAccessTokenValidity(tokenSupport.accessTokenValidity);
tokenSupport.copyClients(IdentityZoneHolder.get().getId(), identityZone.getId());
IdentityZoneHolder.set(identityZone);
AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, tokenSupport.clientScopes);
authorizationRequest.setResourceIds(new HashSet<>(tokenSupport.resourceIds));
Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
azParameters.put(GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS);
authorizationRequest.setRequestParameters(azParameters);
OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), null);
tokenServices.setTokenEndpointBuilder(new TokenEndpointBuilder("http://uaaslave:8080/uaa"));
OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);
assertCommonClientAccessTokenProperties(accessToken);
assertThat(accessToken, validFor(is(tokenSupport.accessTokenValidity)));
assertThat(accessToken, issuerUri(is("http://uaamaster:8080/uaa/oauth/token")));
assertThat(accessToken, zoneId(is(IdentityZoneHolder.get().getId())));
assertThat(accessToken.getRefreshToken(), is(nullValue()));
validateExternalAttributes(accessToken);
}
use of org.maxkey.authz.oauth2.provider.AuthorizationRequest in project uaa by cloudfoundry.
the class DeprecatedUaaTokenServicesTests method readAccessToken.
private void readAccessToken(Set<String> excludedClaims) {
tokenServices.setExcludedClaims(excludedClaims);
AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, tokenSupport.requestedAuthScopes);
authorizationRequest.setResourceIds(new HashSet<>(tokenSupport.resourceIds));
Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
azParameters.put(GRANT_TYPE, GRANT_TYPE_AUTHORIZATION_CODE);
authorizationRequest.setRequestParameters(azParameters);
Authentication userAuthentication = tokenSupport.defaultUserAuthentication;
Calendar expiresAt = Calendar.getInstance();
expiresAt.add(Calendar.MILLISECOND, 3000);
Calendar updatedAt = Calendar.getInstance();
updatedAt.add(Calendar.MILLISECOND, -1000);
tokenSupport.approvalStore.addApproval(new Approval().setUserId(tokenSupport.userId).setClientId(CLIENT_ID).setScope(tokenSupport.readScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED).setLastUpdatedAt(updatedAt.getTime()), IdentityZoneHolder.get().getId());
tokenSupport.approvalStore.addApproval(new Approval().setUserId(tokenSupport.userId).setClientId(CLIENT_ID).setScope(tokenSupport.writeScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED).setLastUpdatedAt(updatedAt.getTime()), IdentityZoneHolder.get().getId());
Approval approval = new Approval().setUserId(tokenSupport.userId).setClientId(CLIENT_ID).setScope(OPENID).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED).setLastUpdatedAt(updatedAt.getTime());
tokenSupport.approvalStore.addApproval(approval, IdentityZoneHolder.get().getId());
OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication);
OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);
assertEquals(accessToken, tokenServices.readAccessToken(accessToken.getValue()));
tokenSupport.approvalStore.revokeApproval(approval, IdentityZoneHolder.get().getId());
try {
tokenServices.readAccessToken(accessToken.getValue());
fail("Approval has been revoked");
} catch (InvalidTokenException x) {
assertThat("Exception should be about approvals", x.getMessage().contains("some requested scopes are not approved"));
}
}
Aggregations