|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.oauth2.client.web.reactive.function.client; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | +import org.springframework.http.HttpHeaders; |
| 21 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; |
| 22 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 23 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 24 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 25 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 26 | +import org.springframework.web.reactive.function.client.ClientRequest; |
| 27 | + |
| 28 | +import java.net.URI; |
| 29 | +import java.time.Duration; |
| 30 | +import java.time.Instant; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.*; |
| 33 | +import static org.springframework.http.HttpMethod.GET; |
| 34 | +import static org.springframework.security.oauth2.client.web.reactive.function.client.OAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Rob Winch |
| 38 | + * @since 5.1 |
| 39 | + */ |
| 40 | +public class OAuth2AuthorizedClientExchangeFilterFunctionTests { |
| 41 | + private OAuth2AuthorizedClientExchangeFilterFunction function = new OAuth2AuthorizedClientExchangeFilterFunction(); |
| 42 | + |
| 43 | + private MockExchangeFunction exchange = new MockExchangeFunction(); |
| 44 | + |
| 45 | + private ClientRegistration github = ClientRegistration.withRegistrationId("github") |
| 46 | + .redirectUriTemplate("{baseUrl}/{action}/oauth2/code/{registrationId}") |
| 47 | + .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC) |
| 48 | + .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) |
| 49 | + .scope("read:user") |
| 50 | + .authorizationUri("https://github.com/login/oauth/authorize") |
| 51 | + .tokenUri("https://github.com/login/oauth/access_token") |
| 52 | + .userInfoUri("https://api.github.com/user") |
| 53 | + .userNameAttributeName("id") |
| 54 | + .clientName("GitHub") |
| 55 | + .clientId("clientId") |
| 56 | + .clientSecret("clientSecret") |
| 57 | + .build(); |
| 58 | + |
| 59 | + private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, |
| 60 | + "token", |
| 61 | + Instant.now(), |
| 62 | + Instant.now().plus(Duration.ofDays(1))); |
| 63 | + |
| 64 | + @Test |
| 65 | + public void filterWhenAuthorizedClientNullThenAuthorizationHeaderNull() { |
| 66 | + ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")) |
| 67 | + .build(); |
| 68 | + |
| 69 | + this.function.filter(request, this.exchange).block(); |
| 70 | + |
| 71 | + assertThat(this.exchange.getRequest().headers().getFirst(HttpHeaders.AUTHORIZATION)).isNull(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void filterWhenAuthorizedClientThenAuthorizationHeader() { |
| 76 | + OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github, |
| 77 | + "principalName", this.accessToken); |
| 78 | + ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")) |
| 79 | + .attributes(oauth2AuthorizedClient(authorizedClient)) |
| 80 | + .build(); |
| 81 | + |
| 82 | + this.function.filter(request, this.exchange).block(); |
| 83 | + |
| 84 | + assertThat(this.exchange.getRequest().headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer " + this.accessToken.getTokenValue()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() { |
| 89 | + OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github, |
| 90 | + "principalName", this.accessToken); |
| 91 | + ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")) |
| 92 | + .header(HttpHeaders.AUTHORIZATION, "Existing") |
| 93 | + .attributes(oauth2AuthorizedClient(authorizedClient)) |
| 94 | + .build(); |
| 95 | + |
| 96 | + this.function.filter(request, this.exchange).block(); |
| 97 | + |
| 98 | + HttpHeaders headers = this.exchange.getRequest().headers(); |
| 99 | + assertThat(headers.get(HttpHeaders.AUTHORIZATION)).containsOnly("Bearer " + this.accessToken.getTokenValue()); |
| 100 | + } |
| 101 | +} |
0 commit comments