Example usage for org.springframework.security.oauth2.provider AuthorizationRequest AuthorizationRequest

List of usage examples for org.springframework.security.oauth2.provider AuthorizationRequest AuthorizationRequest

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.provider AuthorizationRequest AuthorizationRequest.

Prototype

public AuthorizationRequest(String clientId, Collection<String> scopes) 

Source Link

Document

Convenience constructor for unit tests, where client ID and scope are often the only needed fields.

Usage

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test
public void testChangedExpiryForTokens() {
    BaseClientDetails clientDetails = cloneClient(defaultClient);
    clientDetails.setAccessTokenValiditySeconds(3600);
    clientDetails.setRefreshTokenValiditySeconds(36000);
    clientDetailsService.setClientDetailsStore(Collections.singletonMap(CLIENT_ID, clientDetails));

    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);/*  w w  w  .ja  v a 2s.c  o  m*/
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    assertThat(accessToken, validFor(is(3600)));
    assertThat(accessToken.getRefreshToken(), is(not(nullValue())));

    assertThat(accessToken.getRefreshToken(), OAuth2RefreshTokenMatchers.validFor(is(36000)));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test(expected = TokenRevokedException.class)
public void testUserUpdatedAfterRefreshTokenIssued() {
    Calendar expiresAt = Calendar.getInstance();
    expiresAt.add(Calendar.MILLISECOND, 3000);

    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID).setScope(readScope.get(0))
            .setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));
    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID)
            .setScope(writeScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);/*  w  ww . j a v  a2  s  . c  o m*/
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    UaaUser user = userDatabase.retrieveUserByName(username, OriginKeys.UAA);
    UaaUser newUser = new UaaUser(new UaaUserPrototype().withId(userId).withUsername(user.getUsername())
            .withPassword("blah").withEmail(user.getEmail()).withAuthorities(user.getAuthorities()));
    userDatabase.updateUser(userId, newUser);

    AuthorizationRequest refreshAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    refreshAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(refreshAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    refreshAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(refreshAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test(expected = InvalidTokenException.class)
public void testRefreshTokenExpiry() {
    Calendar expiresAt = Calendar.getInstance();
    expiresAt.add(Calendar.MILLISECOND, 3000);

    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID).setScope(readScope.get(0))
            .setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));
    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID)
            .setScope(writeScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));

    BaseClientDetails clientDetails = cloneClient(defaultClient);
    // Back date the refresh token. Crude way to do this but i'm not sure of
    // another//from  w  w w  .  j a va2  s .co m
    clientDetails.setRefreshTokenValiditySeconds(-36000);
    clientDetailsService.setClientDetailsStore(Collections.singletonMap(CLIENT_ID, clientDetails));

    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    AuthorizationRequest refreshAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    refreshAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(refreshAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    refreshAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(refreshAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test(expected = InvalidTokenException.class)
public void testRefreshTokenAfterApprovalsRevoked() {
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);//from  w ww .  ja  v a 2  s  . c o  m
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    Calendar expiresAt = Calendar.getInstance();
    expiresAt.add(Calendar.MILLISECOND, 3000);

    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID).setScope(readScope.get(0))
            .setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));

    // Other scope is left unapproved

    for (Approval approval : approvalStore.getApprovals(userId, CLIENT_ID)) {
        approvalStore.revokeApproval(approval);
    }

    AuthorizationRequest refreshAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    refreshAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(refreshAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    refreshAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(refreshAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test(expected = InvalidTokenException.class)
public void testRefreshTokenAfterApprovalsExpired() {
    Calendar expiresAt = Calendar.getInstance();
    expiresAt.add(Calendar.MILLISECOND, -3000);

    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID).setScope(readScope.get(0))
            .setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));
    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID)
            .setScope(writeScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));

    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);//from w  ww.jav  a2s  . c  o  m
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    AuthorizationRequest refreshAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    refreshAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(refreshAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    refreshAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(refreshAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test(expected = InvalidTokenException.class)
public void testRefreshTokenAfterApprovalsDenied() {
    Calendar expiresAt = Calendar.getInstance();
    expiresAt.add(Calendar.MILLISECOND, -3000);

    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID).setScope(readScope.get(0))
            .setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.DENIED));
    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID)
            .setScope(writeScope.get(0)).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED));

    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);//from  ww w . j  av  a 2 s  .c o m
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    AuthorizationRequest refreshAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    refreshAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(refreshAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    refreshAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(refreshAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test(expected = InvalidTokenException.class)
public void testRefreshTokenAfterApprovalsMissing() {
    Calendar expiresAt = Calendar.getInstance();
    expiresAt.add(Calendar.MILLISECOND, -3000);

    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID).setScope(readScope.get(0))
            .setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.DENIED));

    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);/*  w  w w  .ja  v  a  2  s .  c om*/
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    AuthorizationRequest refreshAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    refreshAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(refreshAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    refreshAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(refreshAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test(expected = InvalidTokenException.class)
public void testRefreshTokenAfterApprovalsMissing2() {
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    authorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);//from  ww w .j  ava 2s  .com
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    AuthorizationRequest refreshAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    refreshAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(refreshAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    refreshAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(refreshAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test
public void refreshAccessTokenWithGrantTypeRestricted() {
    expectedEx.expect(InsufficientScopeException.class);
    expectedEx.expectMessage("Expected scope " + UAA_REFRESH_TOKEN + " is missing");

    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID, requestedAuthScopes);
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            defaultUserAuthentication);//from  w  w w  .ja v a 2s  .  c o m
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    AuthorizationRequest reducedScopeAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, readScope);
    reducedScopeAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(
            reducedScopeAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    reducedScopeAuthorizationRequest.setRequestParameters(refreshAzParameters);

    tokenServices.setRestrictRefreshGrant(true);
    tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(reducedScopeAuthorizationRequest, "refresh_token"));
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaTokenServicesTests.java

@Test
public void refreshAccessTokenWithGrantTypeRestricted_butRefreshScopePresent() {
    AuthorizationRequest authorizationRequest = new AuthorizationRequest(CLIENT_ID,
            Arrays.asList(UAA_REFRESH_TOKEN));
    Map<String, String> azParameters = new HashMap<>(authorizationRequest.getRequestParameters());
    azParameters.put(GRANT_TYPE, AUTHORIZATION_CODE);
    authorizationRequest.setRequestParameters(azParameters);

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            defaultUserAuthentication);//from ww w. ja  v a 2s. co m
    tokenServices.setRestrictRefreshGrant(true);
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);

    AuthorizationRequest reducedScopeAuthorizationRequest = new AuthorizationRequest(CLIENT_ID, null);
    reducedScopeAuthorizationRequest.setResourceIds(new HashSet<>(resourceIds));
    Map<String, String> refreshAzParameters = new HashMap<>(
            reducedScopeAuthorizationRequest.getRequestParameters());
    refreshAzParameters.put(GRANT_TYPE, REFRESH_TOKEN);
    reducedScopeAuthorizationRequest.setRequestParameters(refreshAzParameters);

    expiresAt.add(Calendar.MILLISECOND, 300000);
    updatedAt.add(Calendar.MILLISECOND, -1000);
    approvalStore.addApproval(new Approval().setUserId(userId).setClientId(CLIENT_ID)
            .setScope(UAA_REFRESH_TOKEN).setExpiresAt(expiresAt.getTime()).setStatus(ApprovalStatus.APPROVED)
            .setLastUpdatedAt(updatedAt.getTime()));

    tokenServices.setRestrictRefreshGrant(true);
    OAuth2AccessToken refresh_token = tokenServices.refreshAccessToken(accessToken.getRefreshToken().getValue(),
            requestFactory.createTokenRequest(reducedScopeAuthorizationRequest, "refresh_token"));
    assertNotNull(refresh_token);
}