Search in sources :

Example 1 with ProviderContext

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

the class OAuth2RefreshTokenAuthenticationProviderTests method setUp.

@Before
public void setUp() {
    this.authorizationService = mock(OAuth2AuthorizationService.class);
    this.jwtEncoder = mock(JwtEncoder.class);
    when(this.jwtEncoder.encode(any(), any())).thenReturn(createJwt(Collections.singleton("scope1")));
    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 OAuth2RefreshTokenAuthenticationProvider(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 2 with ProviderContext

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

the class OAuth2ClientCredentialsAuthenticationProviderTests 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);
    OAuth2TokenGenerator<OAuth2Token> delegatingTokenGenerator = new DelegatingOAuth2TokenGenerator(jwtGenerator, accessTokenGenerator);
    this.tokenGenerator = spy(new OAuth2TokenGenerator<OAuth2Token>() {

        @Override
        public OAuth2Token generate(OAuth2TokenContext context) {
            return delegatingTokenGenerator.generate(context);
        }
    });
    this.authenticationProvider = new OAuth2ClientCredentialsAuthenticationProvider(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) 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 3 with ProviderContext

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

the class JwtGeneratorTests method generateWhenAccessTokenTypeThenReturnJwt.

@Test
public void generateWhenAccessTokenTypeThenReturnJwt() {
    RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
    OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
    OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret());
    OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationRequest.class.getName());
    OAuth2AuthorizationCodeAuthenticationToken authentication = new OAuth2AuthorizationCodeAuthenticationToken("code", clientPrincipal, authorizationRequest.getRedirectUri(), null);
    // @formatter:off
    OAuth2TokenContext tokenContext = DefaultOAuth2TokenContext.builder().registeredClient(registeredClient).principal(authorization.getAttribute(Principal.class.getName())).providerContext(this.providerContext).authorization(authorization).authorizedScopes(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME)).tokenType(OAuth2TokenType.ACCESS_TOKEN).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).authorizationGrant(authentication).build();
    // @formatter:on
    assertGeneratedTokenType(tokenContext);
}
Also used : OAuth2AuthorizationCodeAuthenticationToken(org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken) OAuth2ClientAuthenticationToken(org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Principal(java.security.Principal) RegisteredClient(org.springframework.security.oauth2.server.authorization.client.RegisteredClient) Test(org.junit.Test)

Example 4 with ProviderContext

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

the class JwtGeneratorTests method generateWhenIdTokenTypeThenReturnJwt.

@Test
public void generateWhenIdTokenTypeThenReturnJwt() {
    RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scope(OidcScopes.OPENID).build();
    Map<String, Object> authenticationRequestAdditionalParameters = new HashMap<>();
    authenticationRequestAdditionalParameters.put(OidcParameterNames.NONCE, "nonce");
    OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient, authenticationRequestAdditionalParameters).build();
    OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret());
    OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationRequest.class.getName());
    OAuth2AuthorizationCodeAuthenticationToken authentication = new OAuth2AuthorizationCodeAuthenticationToken("code", clientPrincipal, authorizationRequest.getRedirectUri(), null);
    // @formatter:off
    OAuth2TokenContext tokenContext = DefaultOAuth2TokenContext.builder().registeredClient(registeredClient).principal(authorization.getAttribute(Principal.class.getName())).providerContext(this.providerContext).authorization(authorization).authorizedScopes(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME)).tokenType(ID_TOKEN_TOKEN_TYPE).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).authorizationGrant(authentication).build();
    // @formatter:on
    assertGeneratedTokenType(tokenContext);
}
Also used : HashMap(java.util.HashMap) OAuth2AuthorizationCodeAuthenticationToken(org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken) OAuth2ClientAuthenticationToken(org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Principal(java.security.Principal) RegisteredClient(org.springframework.security.oauth2.server.authorization.client.RegisteredClient) Test(org.junit.Test)

Example 5 with ProviderContext

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

the class OAuth2AccessTokenGeneratorTests method setUp.

@Before
public void setUp() {
    this.accessTokenCustomizer = mock(OAuth2TokenCustomizer.class);
    this.accessTokenGenerator = new OAuth2AccessTokenGenerator();
    this.accessTokenGenerator.setAccessTokenCustomizer(this.accessTokenCustomizer);
    ProviderSettings providerSettings = ProviderSettings.builder().issuer("https://provider.com").build();
    this.providerContext = new ProviderContext(providerSettings, null);
}
Also used : ProviderContext(org.springframework.security.oauth2.server.authorization.context.ProviderContext) ProviderSettings(org.springframework.security.oauth2.server.authorization.config.ProviderSettings) Before(org.junit.Before)

Aggregations

ProviderContext (org.springframework.security.oauth2.server.authorization.context.ProviderContext)18 ProviderSettings (org.springframework.security.oauth2.server.authorization.config.ProviderSettings)14 Test (org.junit.Test)12 RegisteredClient (org.springframework.security.oauth2.server.authorization.client.RegisteredClient)12 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)9 OAuth2TokenContext (org.springframework.security.oauth2.server.authorization.OAuth2TokenContext)9 OAuth2Token (org.springframework.security.oauth2.core.OAuth2Token)8 Instant (java.time.Instant)7 Before (org.junit.Before)7 OAuth2Authorization (org.springframework.security.oauth2.server.authorization.OAuth2Authorization)7 Principal (java.security.Principal)6 FilterChain (javax.servlet.FilterChain)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 ClaimAccessor (org.springframework.security.oauth2.core.ClaimAccessor)6 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)6 JwtEncoder (org.springframework.security.oauth2.jwt.JwtEncoder)6 OAuth2AuthorizationService (org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService)6 OAuth2ClientAuthenticationToken (org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken)6 OAuth2AuthenticationException (org.springframework.security.oauth2.core.OAuth2AuthenticationException)5