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

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

Introduction

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

Prototype

public void setResourceIds(Set<String> resourceIds) 

Source Link

Usage

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

@Test(expected = InvalidTokenException.class)
public void testLoadAuthenticationWithAnExpiredToken() throws InterruptedException {
    BaseClientDetails shortExpiryClient = defaultClient;
    shortExpiryClient.setAccessTokenValiditySeconds(1);
    clientDetailsService.setClientDetailsStore(Collections.singletonMap(CLIENT_ID, shortExpiryClient));

    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 .  j  ava2  s  .c om*/
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);
    assertThat(accessToken, validFor(is(1)));

    Thread.sleep(1000l);
    tokenServices.loadAuthentication(accessToken.getValue());
}

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

@Test
public void testCreateAccessTokenAuthcodeGrantAdditionalAuthorizationAttributes() {
    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);
    azParameters.put("authorities",
            "{\"az_attr\":{\"external_group\":\"domain\\\\group1\", \"external_id\":\"abcd1234\"}}");
    authorizationRequest.setRequestParameters(azParameters);
    Authentication userAuthentication = defaultUserAuthentication;

    OAuth2Authentication authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(),
            userAuthentication);// w w w . j a va  2  s  .c  om
    OAuth2AccessToken token = tokenServices.createAccessToken(authentication);

    this.assertCommonUserAccessTokenProperties(token);
    assertThat(token, issuerUri(is(ISSUER_URI)));
    assertThat(token, scope(is(requestedAuthScopes)));
    assertThat(token, validFor(is(60 * 60 * 12)));

    OAuth2RefreshToken refreshToken = token.getRefreshToken();
    this.assertCommonUserRefreshTokenProperties(refreshToken);
    assertThat(refreshToken, OAuth2RefreshTokenMatchers.issuerUri(is(ISSUER_URI)));
    assertThat(refreshToken, OAuth2RefreshTokenMatchers.validFor(is(60 * 60 * 24 * 30)));

    this.assertCommonEventProperties(token, userId, buildJsonString(requestedAuthScopes));

    Map<String, String> azMap = new LinkedHashMap<>();
    azMap.put("external_group", "domain\\group1");
    azMap.put("external_id", "abcd1234");
    assertEquals(azMap, token.getAdditionalInformation().get("az_attr"));
}

From source file:org.orcid.core.oauth.OrcidClientCredentialsChecker.java

public OAuth2Request validateCredentials(String grantType, TokenRequest tokenRequest) {
    String clientId = tokenRequest.getClientId();
    Set<String> scopes = tokenRequest.getScope();
    ClientDetailsEntity clientDetails = clientDetailsEntityCacheManager.retrieve(clientId);
    orcidOAuth2RequestValidator.validateClientIsEnabled(clientDetails);
    validateGrantType(grantType, clientDetails);
    if (scopes != null) {
        validateScope(clientDetails, scopes);
    }//from  ww w . j  av  a 2s  .c  o  m

    Map<String, String> authorizationParams = new HashMap<String, String>();
    authorizationParams.putAll(tokenRequest.getRequestParameters());
    authorizationParams.put(OrcidOauth2Constants.GRANT_TYPE, grantType);
    authorizationParams.put(OAuth2Utils.SCOPE, StringUtils.join(scopes, ' '));
    authorizationParams.put(OAuth2Utils.CLIENT_ID, clientId);

    AuthorizationRequest authorizationRequest = oAuth2RequestFactory
            .createAuthorizationRequest(authorizationParams);
    authorizationRequest.setAuthorities(clientDetails.getAuthorities());
    authorizationRequest.setResourceIds(clientDetails.getResourceIds());
    authorizationRequest.setApproved(true);

    return oAuth2RequestFactory.createOAuth2Request(authorizationRequest);
}

From source file:org.orcid.core.oauth.service.OrcidTokenStoreServiceImpl.java

private OAuth2Authentication getOAuth2AuthenticationFromDetails(OrcidOauth2TokenDetail details) {
    if (details != null) {
        ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager
                .retrieve(details.getClientDetailsId());
        Authentication authentication = null;
        AuthorizationRequest request = null;
        if (clientDetailsEntity != null) {
            //Check member is not locked                
            orcidOAuth2RequestValidator.validateClientIsEnabled(clientDetailsEntity);
            Set<String> scopes = OAuth2Utils.parseParameterList(details.getScope());
            request = new AuthorizationRequest(clientDetailsEntity.getClientId(), scopes);
            request.setAuthorities(clientDetailsEntity.getAuthorities());
            Set<String> resourceIds = new HashSet<>();
            resourceIds.add(details.getResourceId());
            request.setResourceIds(resourceIds);
            request.setApproved(details.isApproved());
            ProfileEntity profile = details.getProfile();
            if (profile != null) {
                authentication = new OrcidOauth2UserAuthentication(profile, details.isApproved());
            }// w w w  .j  a  v a 2  s .  c om
        }
        return new OrcidOAuth2Authentication(request, authentication, details.getTokenValue());
    }
    throw new InvalidTokenException("Token not found");
}