Search in sources :

Example 6 with OAuth2TokenGenerator

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

the class OidcClientRegistrationAuthenticationProviderTests method setUp.

@Before
public void setUp() {
    this.registeredClientRepository = mock(RegisteredClientRepository.class);
    this.authorizationService = mock(OAuth2AuthorizationService.class);
    this.jwtEncoder = mock(JwtEncoder.class);
    JwtGenerator jwtGenerator = new JwtGenerator(this.jwtEncoder);
    this.tokenGenerator = spy(new OAuth2TokenGenerator<Jwt>() {

        @Override
        public Jwt generate(OAuth2TokenContext context) {
            return jwtGenerator.generate(context);
        }
    });
    this.providerSettings = ProviderSettings.builder().issuer("https://provider.com").build();
    ProviderContextHolder.setProviderContext(new ProviderContext(this.providerSettings, null));
    this.authenticationProvider = new OidcClientRegistrationAuthenticationProvider(this.registeredClientRepository, this.authorizationService, this.tokenGenerator);
}
Also used : JwtGenerator(org.springframework.security.oauth2.server.authorization.JwtGenerator) OAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator) ProviderContext(org.springframework.security.oauth2.server.authorization.context.ProviderContext) OAuth2TokenContext(org.springframework.security.oauth2.server.authorization.OAuth2TokenContext) OAuth2AuthorizationService(org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService) JwtEncoder(org.springframework.security.oauth2.jwt.JwtEncoder) RegisteredClientRepository(org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository) Before(org.junit.Before)

Example 7 with OAuth2TokenGenerator

use of org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator in project eurynome-cloud by herodotus-cloud.

the class AuthorizationServerConfiguration method authorizationServerSecurityFilterChain.

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity httpSecurity, JwtDecoder jwtDecoder, HttpCryptoProcessor httpCryptoProcessor) throws Exception {
    OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer = new OAuth2AuthorizationServerConfigurer<>();
    HerodotusAuthenticationFailureHandler failureHandler = new HerodotusAuthenticationFailureHandler();
    authorizationServerConfigurer.clientAuthentication(endpoint -> endpoint.errorResponseHandler(failureHandler));
    authorizationServerConfigurer.authorizationEndpoint(endpoint -> endpoint.errorResponseHandler(failureHandler));
    authorizationServerConfigurer.tokenRevocationEndpoint(endpoint -> endpoint.errorResponseHandler(failureHandler));
    authorizationServerConfigurer.tokenEndpoint(endpoint -> {
        AuthenticationConverter authenticationConverter = new DelegatingAuthenticationConverter(Arrays.asList(new OAuth2AuthorizationCodeAuthenticationConverter(), new OAuth2RefreshTokenAuthenticationConverter(), new OAuth2ClientCredentialsAuthenticationConverter(), new OAuth2ResourceOwnerPasswordAuthenticationConverter(httpCryptoProcessor)));
        endpoint.accessTokenRequestConverter(authenticationConverter);
        endpoint.errorResponseHandler(failureHandler);
    });
    RequestMatcher endpointsMatcher = authorizationServerConfigurer.getEndpointsMatcher();
    httpSecurity.requestMatcher(endpointsMatcher).authorizeRequests(authorizeRequests -> authorizeRequests.anyRequest().authenticated()).csrf(csrf -> csrf.ignoringRequestMatchers(endpointsMatcher)).oauth2ResourceServer(configurer -> configurer.jwt(jwt -> jwt.decoder(jwtDecoder)).bearerTokenResolver(new DefaultBearerTokenResolver()).accessDeniedHandler(new HerodotusAccessDeniedHandler()).authenticationEntryPoint(new HerodotusAuthenticationEntryPoint())).apply(authorizationServerConfigurer).oidc(oidc -> oidc.userInfoEndpoint(userInfo -> userInfo.userInfoMapper(context -> {
        OidcUserInfoAuthenticationToken authentication = context.getAuthentication();
        JwtAuthenticationToken principal = (JwtAuthenticationToken) authentication.getPrincipal();
        return new OidcUserInfo(principal.getToken().getClaims());
    })));
    SecurityFilterChain securityFilterChain = httpSecurity.formLogin(Customizer.withDefaults()).build();
    AuthenticationManager authenticationManager = httpSecurity.getSharedObject(AuthenticationManager.class);
    OAuth2AuthorizationService authorizationService = OAuth2ConfigurerUtils.getAuthorizationService(httpSecurity);
    OAuth2TokenGenerator<? extends OAuth2Token> tokenGenerator = OAuth2ConfigurerUtils.getTokenGenerator(httpSecurity);
    OAuth2ResourceOwnerPasswordAuthenticationProvider resourceOwnerPasswordAuthenticationProvider = new OAuth2ResourceOwnerPasswordAuthenticationProvider(authorizationService, tokenGenerator, authenticationManager);
    httpSecurity.authenticationProvider(resourceOwnerPasswordAuthenticationProvider);
    return securityFilterChain;
}
Also used : HerodotusTokenCustomizer(cn.herodotus.engine.oauth2.authorization.customizer.HerodotusTokenCustomizer) OAuth2Properties(cn.herodotus.engine.oauth2.core.properties.OAuth2Properties) KeyPair(java.security.KeyPair) Arrays(java.util.Arrays) JwtEncodingContext(org.springframework.security.oauth2.server.authorization.JwtEncodingContext) OAuth2AuthorizationServerConfiguration(org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration) OAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator) LoggerFactory(org.slf4j.LoggerFactory) HttpCryptoProcessor(cn.herodotus.engine.protect.web.crypto.processor.HttpCryptoProcessor) JWKSet(com.nimbusds.jose.jwk.JWKSet) OAuth2AuthorizationCodeAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AuthorizationCodeAuthenticationConverter) OAuth2ConfigurerUtils(cn.herodotus.engine.oauth2.authorization.utils.OAuth2ConfigurerUtils) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) OAuth2ResourceOwnerPasswordAuthenticationConverter(cn.herodotus.engine.oauth2.authorization.authentication.OAuth2ResourceOwnerPasswordAuthenticationConverter) RSAPublicKey(java.security.interfaces.RSAPublicKey) HerodotusAuthenticationFailureHandler(cn.herodotus.engine.oauth2.core.response.HerodotusAuthenticationFailureHandler) Resource(org.springframework.core.io.Resource) ProviderSettings(org.springframework.security.oauth2.server.authorization.config.ProviderSettings) KeyPairGenerator(java.security.KeyPairGenerator) JWKSource(com.nimbusds.jose.jwk.source.JWKSource) ResourceUtils(cn.herodotus.engine.assistant.core.utils.ResourceUtils) OidcUserInfoAuthenticationToken(org.springframework.security.oauth2.server.authorization.oidc.authentication.OidcUserInfoAuthenticationToken) OAuth2ClientCredentialsAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2ClientCredentialsAuthenticationConverter) DelegatingAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.DelegatingAuthenticationConverter) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) UUID(java.util.UUID) HerodotusAccessDeniedHandler(cn.herodotus.engine.oauth2.core.response.HerodotusAccessDeniedHandler) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) Configuration(org.springframework.context.annotation.Configuration) HerodotusAuthenticationEntryPoint(cn.herodotus.engine.oauth2.core.response.HerodotusAuthenticationEntryPoint) OAuth2ResourceOwnerPasswordAuthenticationProvider(cn.herodotus.engine.oauth2.authorization.authentication.OAuth2ResourceOwnerPasswordAuthenticationProvider) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PostConstruct(javax.annotation.PostConstruct) Certificate(cn.herodotus.engine.oauth2.core.enums.Certificate) OAuth2Token(org.springframework.security.oauth2.core.OAuth2Token) SecurityContext(com.nimbusds.jose.proc.SecurityContext) Ordered(org.springframework.core.Ordered) DefaultBearerTokenResolver(org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver) OAuth2AuthorizationServerConfigurer(org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization.OAuth2AuthorizationServerConfigurer) ArrayUtils(org.apache.commons.lang3.ArrayUtils) EndpointProperties(cn.herodotus.engine.web.core.properties.EndpointProperties) OidcUserInfo(org.springframework.security.oauth2.core.oidc.OidcUserInfo) JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Order(org.springframework.core.annotation.Order) Logger(org.slf4j.Logger) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Customizer(org.springframework.security.config.Customizer) IOException(java.io.IOException) OAuth2AuthorizationService(org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService) SecurityFilterChain(org.springframework.security.web.SecurityFilterChain) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) RSAKey(com.nimbusds.jose.jwk.RSAKey) OAuth2TokenCustomizer(org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer) KeyStoreKeyFactory(org.springframework.security.rsa.crypto.KeyStoreKeyFactory) AuthenticationConverter(org.springframework.security.web.authentication.AuthenticationConverter) Bean(org.springframework.context.annotation.Bean) OAuth2RefreshTokenAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2RefreshTokenAuthenticationConverter) OAuth2AuthorizationCodeAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AuthorizationCodeAuthenticationConverter) OAuth2ResourceOwnerPasswordAuthenticationConverter(cn.herodotus.engine.oauth2.authorization.authentication.OAuth2ResourceOwnerPasswordAuthenticationConverter) OAuth2ClientCredentialsAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2ClientCredentialsAuthenticationConverter) DelegatingAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.DelegatingAuthenticationConverter) AuthenticationConverter(org.springframework.security.web.authentication.AuthenticationConverter) OAuth2RefreshTokenAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2RefreshTokenAuthenticationConverter) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) OAuth2AuthorizationServerConfigurer(org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization.OAuth2AuthorizationServerConfigurer) OAuth2AuthorizationCodeAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AuthorizationCodeAuthenticationConverter) JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) OAuth2ResourceOwnerPasswordAuthenticationProvider(cn.herodotus.engine.oauth2.authorization.authentication.OAuth2ResourceOwnerPasswordAuthenticationProvider) HerodotusAuthenticationEntryPoint(cn.herodotus.engine.oauth2.core.response.HerodotusAuthenticationEntryPoint) OAuth2RefreshTokenAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2RefreshTokenAuthenticationConverter) DelegatingAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.DelegatingAuthenticationConverter) OidcUserInfo(org.springframework.security.oauth2.core.oidc.OidcUserInfo) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) SecurityFilterChain(org.springframework.security.web.SecurityFilterChain) OAuth2ClientCredentialsAuthenticationConverter(org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2ClientCredentialsAuthenticationConverter) DefaultBearerTokenResolver(org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver) HerodotusAccessDeniedHandler(cn.herodotus.engine.oauth2.core.response.HerodotusAccessDeniedHandler) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) OidcUserInfoAuthenticationToken(org.springframework.security.oauth2.server.authorization.oidc.authentication.OidcUserInfoAuthenticationToken) OAuth2AuthorizationService(org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService) HerodotusAuthenticationFailureHandler(cn.herodotus.engine.oauth2.core.response.HerodotusAuthenticationFailureHandler) OAuth2ResourceOwnerPasswordAuthenticationConverter(cn.herodotus.engine.oauth2.authorization.authentication.OAuth2ResourceOwnerPasswordAuthenticationConverter) Order(org.springframework.core.annotation.Order) Bean(org.springframework.context.annotation.Bean)

Example 8 with OAuth2TokenGenerator

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

the class OAuth2AuthorizationCodeAuthenticationProviderTests method setUp.

@Before
public void setUp() {
    this.authorizationService = mock(OAuth2AuthorizationService.class);
    this.jwtEncoder = mock(JwtEncoder.class);
    this.jwtCustomizer = mock(OAuth2TokenCustomizer.class);
    JwtGenerator jwtGenerator = new JwtGenerator(this.jwtEncoder);
    jwtGenerator.setJwtCustomizer(this.jwtCustomizer);
    this.accessTokenCustomizer = mock(OAuth2TokenCustomizer.class);
    OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
    accessTokenGenerator.setAccessTokenCustomizer(this.accessTokenCustomizer);
    OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
    OAuth2TokenGenerator<OAuth2Token> delegatingTokenGenerator = new DelegatingOAuth2TokenGenerator(jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
    this.tokenGenerator = spy(new OAuth2TokenGenerator<OAuth2Token>() {

        @Override
        public OAuth2Token generate(OAuth2TokenContext context) {
            return delegatingTokenGenerator.generate(context);
        }
    });
    this.authenticationProvider = new OAuth2AuthorizationCodeAuthenticationProvider(this.authorizationService, this.tokenGenerator);
    ProviderSettings providerSettings = ProviderSettings.builder().issuer("https://provider.com").build();
    ProviderContextHolder.setProviderContext(new ProviderContext(providerSettings, null));
}
Also used : ProviderContext(org.springframework.security.oauth2.server.authorization.context.ProviderContext) OAuth2Token(org.springframework.security.oauth2.core.OAuth2Token) ProviderSettings(org.springframework.security.oauth2.server.authorization.config.ProviderSettings) JwtEncoder(org.springframework.security.oauth2.jwt.JwtEncoder) OAuth2TokenCustomizer(org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer) DelegatingOAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.DelegatingOAuth2TokenGenerator) JwtGenerator(org.springframework.security.oauth2.server.authorization.JwtGenerator) OAuth2RefreshTokenGenerator(org.springframework.security.oauth2.server.authorization.OAuth2RefreshTokenGenerator) OAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator) DelegatingOAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.DelegatingOAuth2TokenGenerator) OAuth2TokenContext(org.springframework.security.oauth2.server.authorization.OAuth2TokenContext) OAuth2AuthorizationService(org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService) OAuth2AccessTokenGenerator(org.springframework.security.oauth2.server.authorization.OAuth2AccessTokenGenerator) Before(org.junit.Before)

Example 9 with OAuth2TokenGenerator

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

the class DelegatingOAuth2TokenGeneratorTests method generateWhenTokenGeneratorSupportedThenReturnToken.

@Test
@SuppressWarnings("unchecked")
public void generateWhenTokenGeneratorSupportedThenReturnToken() {
    OAuth2TokenGenerator<OAuth2Token> tokenGenerator1 = mock(OAuth2TokenGenerator.class);
    OAuth2TokenGenerator<OAuth2Token> tokenGenerator2 = mock(OAuth2TokenGenerator.class);
    OAuth2TokenGenerator<OAuth2Token> tokenGenerator3 = mock(OAuth2TokenGenerator.class);
    OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token", Instant.now(), Instant.now().plusSeconds(300));
    when(tokenGenerator3.generate(any())).thenReturn(accessToken);
    DelegatingOAuth2TokenGenerator delegatingTokenGenerator = new DelegatingOAuth2TokenGenerator(tokenGenerator1, tokenGenerator2, tokenGenerator3);
    OAuth2Token token = delegatingTokenGenerator.generate(DefaultOAuth2TokenContext.builder().build());
    assertThat(token).isEqualTo(accessToken);
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) OAuth2Token(org.springframework.security.oauth2.core.OAuth2Token) Test(org.junit.Test)

Example 10 with OAuth2TokenGenerator

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

the class DelegatingOAuth2TokenGeneratorTests method generateWhenTokenGeneratorNotSupportedThenReturnNull.

@Test
@SuppressWarnings("unchecked")
public void generateWhenTokenGeneratorNotSupportedThenReturnNull() {
    OAuth2TokenGenerator<OAuth2Token> tokenGenerator1 = mock(OAuth2TokenGenerator.class);
    OAuth2TokenGenerator<OAuth2Token> tokenGenerator2 = mock(OAuth2TokenGenerator.class);
    OAuth2TokenGenerator<OAuth2Token> tokenGenerator3 = mock(OAuth2TokenGenerator.class);
    DelegatingOAuth2TokenGenerator delegatingTokenGenerator = new DelegatingOAuth2TokenGenerator(tokenGenerator1, tokenGenerator2, tokenGenerator3);
    OAuth2Token token = delegatingTokenGenerator.generate(DefaultOAuth2TokenContext.builder().build());
    assertThat(token).isNull();
}
Also used : OAuth2Token(org.springframework.security.oauth2.core.OAuth2Token) Test(org.junit.Test)

Aggregations

OAuth2AuthorizationService (org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService)7 OAuth2Token (org.springframework.security.oauth2.core.OAuth2Token)6 OAuth2TokenGenerator (org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator)6 Before (org.junit.Before)5 JwtGenerator (org.springframework.security.oauth2.server.authorization.JwtGenerator)5 ProviderSettings (org.springframework.security.oauth2.server.authorization.config.ProviderSettings)5 ProviderContext (org.springframework.security.oauth2.server.authorization.context.ProviderContext)5 JwtEncoder (org.springframework.security.oauth2.jwt.JwtEncoder)4 DelegatingOAuth2TokenGenerator (org.springframework.security.oauth2.server.authorization.DelegatingOAuth2TokenGenerator)4 OAuth2AccessTokenGenerator (org.springframework.security.oauth2.server.authorization.OAuth2AccessTokenGenerator)4 OAuth2TokenContext (org.springframework.security.oauth2.server.authorization.OAuth2TokenContext)4 OAuth2TokenCustomizer (org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer)4 OAuth2RefreshTokenGenerator (org.springframework.security.oauth2.server.authorization.OAuth2RefreshTokenGenerator)3 Test (org.junit.Test)2 RegisteredClientRepository (org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository)2 ResourceUtils (cn.herodotus.engine.assistant.core.utils.ResourceUtils)1 OAuth2ResourceOwnerPasswordAuthenticationConverter (cn.herodotus.engine.oauth2.authorization.authentication.OAuth2ResourceOwnerPasswordAuthenticationConverter)1 OAuth2ResourceOwnerPasswordAuthenticationProvider (cn.herodotus.engine.oauth2.authorization.authentication.OAuth2ResourceOwnerPasswordAuthenticationProvider)1 HerodotusTokenCustomizer (cn.herodotus.engine.oauth2.authorization.customizer.HerodotusTokenCustomizer)1 OAuth2ConfigurerUtils (cn.herodotus.engine.oauth2.authorization.utils.OAuth2ConfigurerUtils)1