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();
}
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));
}
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();
}
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;
}
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();
}
Aggregations