Search in sources :

Example 6 with ClientRegistrationRepository

use of org.springframework.security.oauth2.client.registration.ClientRegistrationRepository in project spring-security by spring-projects.

the class OAuth2ClientConfigurerTests method setup.

@BeforeEach
public void setup() {
    // @formatter:off
    this.registration1 = TestClientRegistrations.clientRegistration().registrationId("registration-1").clientId("client-1").clientSecret("secret").clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).redirectUri("{baseUrl}/client-1").scope("user").authorizationUri("https://provider.com/oauth2/authorize").tokenUri("https://provider.com/oauth2/token").userInfoUri("https://provider.com/oauth2/user").userNameAttributeName("id").clientName("client-1").build();
    // @formatter:on
    clientRegistrationRepository = new InMemoryClientRegistrationRepository(this.registration1);
    authorizedClientService = new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
    authorizedClientRepository = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
    authorizationRequestResolver = new DefaultOAuth2AuthorizationRequestResolver(clientRegistrationRepository, "/oauth2/authorization");
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(300).build();
    accessTokenResponseClient = mock(OAuth2AccessTokenResponseClient.class);
    given(accessTokenResponseClient.getTokenResponse(any(OAuth2AuthorizationCodeGrantRequest.class))).willReturn(accessTokenResponse);
    requestCache = mock(RequestCache.class);
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) RequestCache(org.springframework.security.web.savedrequest.RequestCache) OAuth2AuthorizationCodeGrantRequest(org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest) InMemoryOAuth2AuthorizedClientService(org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService) InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository) OAuth2AccessTokenResponseClient(org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient) DefaultOAuth2AuthorizationRequestResolver(org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with ClientRegistrationRepository

use of org.springframework.security.oauth2.client.registration.ClientRegistrationRepository in project spring-boot by spring-projects.

the class OAuth2WebSecurityConfigurationTests method securityConfigurerConfiguresAuthorizationCode.

@Test
void securityConfigurerConfiguresAuthorizationCode() {
    this.contextRunner.withUserConfiguration(ClientRegistrationRepositoryConfiguration.class, OAuth2WebSecurityConfiguration.class).run((context) -> {
        ClientRegistrationRepository expected = context.getBean(ClientRegistrationRepository.class);
        ClientRegistrationRepository actual = (ClientRegistrationRepository) ReflectionTestUtils.getField(getFilters(context, OAuth2AuthorizationCodeGrantFilter.class).get(0), "clientRegistrationRepository");
        assertThat(isEqual(expected.findByRegistrationId("first"), actual.findByRegistrationId("first"))).isTrue();
        assertThat(isEqual(expected.findByRegistrationId("second"), actual.findByRegistrationId("second"))).isTrue();
    });
}
Also used : InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) OAuth2AuthorizationCodeGrantFilter(org.springframework.security.oauth2.client.web.OAuth2AuthorizationCodeGrantFilter) Test(org.junit.jupiter.api.Test)

Example 8 with ClientRegistrationRepository

use of org.springframework.security.oauth2.client.registration.ClientRegistrationRepository in project spring-boot by spring-projects.

the class OAuth2WebSecurityConfigurationTests method securityConfigurerConfiguresOAuth2Login.

@Test
void securityConfigurerConfiguresOAuth2Login() {
    this.contextRunner.withUserConfiguration(ClientRegistrationRepositoryConfiguration.class, OAuth2WebSecurityConfiguration.class).run((context) -> {
        ClientRegistrationRepository expected = context.getBean(ClientRegistrationRepository.class);
        ClientRegistrationRepository actual = (ClientRegistrationRepository) ReflectionTestUtils.getField(getFilters(context, OAuth2LoginAuthenticationFilter.class).get(0), "clientRegistrationRepository");
        assertThat(isEqual(expected.findByRegistrationId("first"), actual.findByRegistrationId("first"))).isTrue();
        assertThat(isEqual(expected.findByRegistrationId("second"), actual.findByRegistrationId("second"))).isTrue();
    });
}
Also used : OAuth2LoginAuthenticationFilter(org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter) InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) Test(org.junit.jupiter.api.Test)

Example 9 with ClientRegistrationRepository

use of org.springframework.security.oauth2.client.registration.ClientRegistrationRepository in project spring-boot by spring-projects.

the class OAuth2ClientRegistrationRepositoryConfigurationTests method clientRegistrationRepositoryBeanShouldBeCreatedWhenPropertiesPresent.

@Test
void clientRegistrationRepositoryBeanShouldBeCreatedWhenPropertiesPresent() {
    this.contextRunner.withUserConfiguration(OAuth2ClientRegistrationRepositoryConfiguration.class).withPropertyValues(REGISTRATION_PREFIX + ".foo.client-id=abcd", REGISTRATION_PREFIX + ".foo.client-secret=secret", REGISTRATION_PREFIX + ".foo.provider=github").run((context) -> {
        ClientRegistrationRepository repository = context.getBean(ClientRegistrationRepository.class);
        ClientRegistration registration = repository.findByRegistrationId("foo");
        assertThat(registration).isNotNull();
        assertThat(registration.getClientSecret()).isEqualTo("secret");
    });
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) Test(org.junit.jupiter.api.Test)

Example 10 with ClientRegistrationRepository

use of org.springframework.security.oauth2.client.registration.ClientRegistrationRepository in project tutorials by eugenp.

the class LoginController method getLoginPage.

@GetMapping("/oauth_login")
public String getLoginPage(Model model) {
    Iterable<ClientRegistration> clientRegistrations = null;
    ResolvableType type = ResolvableType.forInstance(clientRegistrationRepository).as(Iterable.class);
    if (type != ResolvableType.NONE && ClientRegistration.class.isAssignableFrom(type.resolveGenerics()[0])) {
        clientRegistrations = (Iterable<ClientRegistration>) clientRegistrationRepository;
    }
    clientRegistrations.forEach(registration -> oauth2AuthenticationUrls.put(registration.getClientName(), authorizationRequestBaseUri + "/" + registration.getRegistrationId()));
    model.addAttribute("urls", oauth2AuthenticationUrls);
    return "oauth_login";
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ResolvableType(org.springframework.core.ResolvableType) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Test (org.junit.jupiter.api.Test)10 ClientRegistrationRepository (org.springframework.security.oauth2.client.registration.ClientRegistrationRepository)10 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)7 InMemoryClientRegistrationRepository (org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository)5 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)4 OAuth2AuthorizedClientRepository (org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository)4 OAuth2AccessTokenResponseClient (org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)2 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)2 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)2 ResolvableType (org.springframework.core.ResolvableType)2 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)2 RegisteredOAuth2AuthorizedClient (org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient)2 ReactiveClientRegistrationRepository (org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 Arrays (java.util.Arrays)1