Search in sources :

Example 1 with OAuth2TokenType

use of org.springframework.security.oauth2.core.OAuth2TokenType in project spring-authorization-server by spring-projects.

the class OAuth2AccessTokenGeneratorTests method generateWhenUnsupportedTokenTypeThenReturnNull.

@Test
public void generateWhenUnsupportedTokenTypeThenReturnNull() {
    // @formatter:off
    TokenSettings tokenSettings = TokenSettings.builder().accessTokenFormat(OAuth2TokenFormat.REFERENCE).build();
    RegisteredClient registeredClient = TestRegisteredClients.registeredClient().tokenSettings(tokenSettings).build();
    OAuth2TokenContext tokenContext = DefaultOAuth2TokenContext.builder().registeredClient(registeredClient).tokenType(new OAuth2TokenType("unsupported_token_type")).build();
    // @formatter:on
    assertThat(this.accessTokenGenerator.generate(tokenContext)).isNull();
}
Also used : OAuth2TokenType(org.springframework.security.oauth2.core.OAuth2TokenType) TokenSettings(org.springframework.security.oauth2.server.authorization.config.TokenSettings) RegisteredClient(org.springframework.security.oauth2.server.authorization.client.RegisteredClient) Test(org.junit.Test)

Example 2 with OAuth2TokenType

use of org.springframework.security.oauth2.core.OAuth2TokenType in project spring-authorization-server by spring-projects.

the class OAuth2TokenRevocationTests method requestWhenTokenRevocationEndpointCustomizedThenUsed.

@Test
public void requestWhenTokenRevocationEndpointCustomizedThenUsed() throws Exception {
    this.spring.register(AuthorizationServerConfigurationCustomTokenRevocationEndpoint.class).autowire();
    RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
    this.registeredClientRepository.save(registeredClient);
    OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
    OAuth2AccessToken token = authorization.getAccessToken().getToken();
    OAuth2TokenType tokenType = OAuth2TokenType.ACCESS_TOKEN;
    this.authorizationService.save(authorization);
    Authentication clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret());
    OAuth2TokenRevocationAuthenticationToken tokenRevocationAuthentication = new OAuth2TokenRevocationAuthenticationToken(token, clientPrincipal);
    when(authenticationConverter.convert(any())).thenReturn(tokenRevocationAuthentication);
    when(authenticationProvider.supports(eq(OAuth2TokenRevocationAuthenticationToken.class))).thenReturn(true);
    when(authenticationProvider.authenticate(any())).thenReturn(tokenRevocationAuthentication);
    this.mvc.perform(post(DEFAULT_TOKEN_REVOCATION_ENDPOINT_URI).params(getTokenRevocationRequestParameters(token, tokenType)).header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(registeredClient.getClientId(), registeredClient.getClientSecret()))).andExpect(status().isOk());
    verify(authenticationConverter).convert(any());
    verify(authenticationProvider).authenticate(eq(tokenRevocationAuthentication));
    verify(authenticationSuccessHandler).onAuthenticationSuccess(any(), any(), eq(tokenRevocationAuthentication));
}
Also used : OAuth2TokenType(org.springframework.security.oauth2.core.OAuth2TokenType) OAuth2TokenRevocationAuthenticationToken(org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenRevocationAuthenticationToken) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) Authentication(org.springframework.security.core.Authentication) OAuth2Authorization(org.springframework.security.oauth2.server.authorization.OAuth2Authorization) OAuth2ClientAuthenticationToken(org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken) RegisteredClient(org.springframework.security.oauth2.server.authorization.client.RegisteredClient) Test(org.junit.Test)

Example 3 with OAuth2TokenType

use of org.springframework.security.oauth2.core.OAuth2TokenType in project spring-authorization-server by spring-projects.

the class OAuth2RefreshTokenGrantTests method requestWhenRevokeAndRefreshThenAccessTokenActive.

// gh-432
@Test
public void requestWhenRevokeAndRefreshThenAccessTokenActive() throws Exception {
    this.spring.register(AuthorizationServerConfiguration.class).autowire();
    RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
    this.registeredClientRepository.save(registeredClient);
    OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
    this.authorizationService.save(authorization);
    OAuth2AccessToken token = authorization.getAccessToken().getToken();
    OAuth2TokenType tokenType = OAuth2TokenType.ACCESS_TOKEN;
    this.mvc.perform(post(DEFAULT_TOKEN_REVOCATION_ENDPOINT_URI).params(getTokenRevocationRequestParameters(token, tokenType)).header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(registeredClient.getClientId(), registeredClient.getClientSecret()))).andExpect(status().isOk());
    this.mvc.perform(post(DEFAULT_TOKEN_ENDPOINT_URI).params(getRefreshTokenRequestParameters(authorization)).header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(registeredClient.getClientId(), registeredClient.getClientSecret()))).andExpect(status().isOk());
    OAuth2Authorization updatedAuthorization = this.authorizationService.findById(authorization.getId());
    OAuth2Authorization.Token<OAuth2AccessToken> accessToken = updatedAuthorization.getAccessToken();
    assertThat(accessToken.isActive()).isTrue();
}
Also used : OAuth2TokenType(org.springframework.security.oauth2.core.OAuth2TokenType) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) OAuth2Authorization(org.springframework.security.oauth2.server.authorization.OAuth2Authorization) OAuth2AuthorizationServerConfiguration(org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration) RegisteredClient(org.springframework.security.oauth2.server.authorization.client.RegisteredClient) Test(org.junit.Test)

Example 4 with OAuth2TokenType

use of org.springframework.security.oauth2.core.OAuth2TokenType in project muses by acgist.

the class RedisOAuth2AuthorizationService method findByToken.

@Override
public OAuth2Authorization findByToken(String token, OAuth2TokenType tokenType) {
    if (tokenType != null) {
        return (OAuth2Authorization) this.redisTemplate.opsForValue().get(buildTokenKey(tokenType.getValue(), token));
    } else {
        OAuth2Authorization authorization = (OAuth2Authorization) this.redisTemplate.opsForValue().get(buildTokenKey(OAuth2ParameterNames.ACCESS_TOKEN, token));
        if (authorization != null) {
            return authorization;
        }
        authorization = (OAuth2Authorization) this.redisTemplate.opsForValue().get(buildTokenKey(OAuth2ParameterNames.REFRESH_TOKEN, token));
        if (authorization != null) {
            return authorization;
        }
        authorization = (OAuth2Authorization) this.redisTemplate.opsForValue().get(buildTokenKey(OAuth2ParameterNames.CODE, token));
        if (authorization != null) {
            return authorization;
        }
    }
    return null;
}
Also used : OAuth2Authorization(org.springframework.security.oauth2.server.authorization.OAuth2Authorization)

Example 5 with OAuth2TokenType

use of org.springframework.security.oauth2.core.OAuth2TokenType in project oauth2-server by gw2auth.

the class OAuth2ConsentController method consentDeny.

@GetMapping(value = "/api/oauth2/consent-deny")
public ResponseEntity<Void> consentDeny(@RequestParam(OAuth2ParameterNames.STATE) String state) {
    final OAuth2Authorization oauth2Authorization = this.auth2AuthorizationService.findByToken(state, new OAuth2TokenType((OAuth2ParameterNames.STATE)));
    if (oauth2Authorization == null) {
        return ResponseEntity.badRequest().build();
    }
    final OAuth2AuthorizationRequest authorizationRequest = oauth2Authorization.getAttribute(OAuth2AuthorizationRequest.class.getName());
    if (authorizationRequest == null) {
        return ResponseEntity.badRequest().build();
    }
    final URI redirectUri = UriComponentsBuilder.fromHttpUrl(authorizationRequest.getRedirectUri()).replaceQueryParam(OAuth2ParameterNames.STATE, authorizationRequest.getState()).replaceQueryParam(OAuth2ParameterNames.ERROR, OAuth2ErrorCodes.ACCESS_DENIED).replaceQueryParam(OAuth2ParameterNames.ERROR_DESCRIPTION, "The user has denied your application access.").build().toUri();
    return ResponseEntity.status(HttpStatus.FOUND).location(redirectUri).build();
}
Also used : OAuth2TokenType(org.springframework.security.oauth2.core.OAuth2TokenType) OAuth2Authorization(org.springframework.security.oauth2.server.authorization.OAuth2Authorization) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) URI(java.net.URI) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

OAuth2TokenType (org.springframework.security.oauth2.core.OAuth2TokenType)7 OAuth2Authorization (org.springframework.security.oauth2.server.authorization.OAuth2Authorization)6 Test (org.junit.Test)5 RegisteredClient (org.springframework.security.oauth2.server.authorization.client.RegisteredClient)5 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)4 OAuth2AuthorizationServerConfiguration (org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration)3 OAuth2RefreshToken (org.springframework.security.oauth2.core.OAuth2RefreshToken)2 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)2 URI (java.net.URI)1 Authentication (org.springframework.security.core.Authentication)1 OAuth2AuthorizationCodeRequestAuthenticationToken (org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationToken)1 OAuth2ClientAuthenticationToken (org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken)1 OAuth2TokenRevocationAuthenticationToken (org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenRevocationAuthenticationToken)1 TokenSettings (org.springframework.security.oauth2.server.authorization.config.TokenSettings)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1