Example usage for org.springframework.security.oauth2.provider.client BaseClientDetails setAccessTokenValiditySeconds

List of usage examples for org.springframework.security.oauth2.provider.client BaseClientDetails setAccessTokenValiditySeconds

Introduction

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

Prototype

public void setAccessTokenValiditySeconds(Integer accessTokenValiditySeconds) 

Source Link

Usage

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

private ClientDetails syncWithExisting(ClientDetails existing, ClientDetails input) {
    BaseClientDetails details = new BaseClientDetails(input);
    if (details.getAccessTokenValiditySeconds() == null) {
        details.setAccessTokenValiditySeconds(existing.getAccessTokenValiditySeconds());
    }/*from  ww w .  j  a v a2 s  .  c o m*/
    if (details.getRefreshTokenValiditySeconds() == null) {
        details.setRefreshTokenValiditySeconds(existing.getRefreshTokenValiditySeconds());
    }
    if (details.getAuthorities() == null || details.getAuthorities().isEmpty()) {
        details.setAuthorities(existing.getAuthorities());
    }
    if (details.getAuthorizedGrantTypes() == null || details.getAuthorizedGrantTypes().isEmpty()) {
        details.setAuthorizedGrantTypes(existing.getAuthorizedGrantTypes());
    }
    if (details.getRegisteredRedirectUri() == null || details.getRegisteredRedirectUri().isEmpty()) {
        details.setRegisteredRedirectUri(existing.getRegisteredRedirectUri());
    }
    if (details.getResourceIds() == null || details.getResourceIds().isEmpty()) {
        details.setResourceIds(existing.getResourceIds());
    }
    if (details.getScope() == null || details.getScope().isEmpty()) {
        details.setScope(existing.getScope());
    }

    Map<String, Object> additionalInformation = new HashMap<String, Object>(
            existing.getAdditionalInformation());
    additionalInformation.putAll(input.getAdditionalInformation());
    for (String key : Collections.unmodifiableSet(additionalInformation.keySet())) {
        if (additionalInformation.get(key) == null) {
            additionalInformation.remove(key);
        }
    }
    details.setAdditionalInformation(additionalInformation);

    return details;
}

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);/*ww  w  . j a v a2  s  .  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 = 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);//from   w  ww .j a  v  a 2s . c o  m
    OAuth2AccessToken accessToken = tokenServices.createAccessToken(authentication);
    assertThat(accessToken, validFor(is(1)));

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