Browse Source
The following commits are merged using the default merge strategy.pull/876/head2dabfa02e0Remove constructor in OidcProviderConfigurationEndpointFilter6b66719a83Remove constructor in OAuth2AuthorizationServerMetadataEndpointFilteraebc613862Make AuthorizationServerContext an interfacef583668a9cMake AuthorizationServerContextFilter private3efee494adRename ProviderContextc60ae4532fRename ProviderSettings
60 changed files with 714 additions and 780 deletions
@ -0,0 +1,105 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.function.Supplier; |
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain; |
||||||
|
import jakarta.servlet.ServletException; |
||||||
|
import jakarta.servlet.http.HttpServletRequest; |
||||||
|
import jakarta.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import org.springframework.security.oauth2.server.authorization.context.AuthorizationServerContext; |
||||||
|
import org.springframework.security.oauth2.server.authorization.context.AuthorizationServerContextHolder; |
||||||
|
import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; |
||||||
|
import org.springframework.security.web.util.UrlUtils; |
||||||
|
import org.springframework.util.Assert; |
||||||
|
import org.springframework.web.filter.OncePerRequestFilter; |
||||||
|
import org.springframework.web.util.UriComponentsBuilder; |
||||||
|
|
||||||
|
/** |
||||||
|
* A {@code Filter} that associates the {@link AuthorizationServerContext} to the {@link AuthorizationServerContextHolder}. |
||||||
|
* |
||||||
|
* @author Joe Grandja |
||||||
|
* @since 0.2.2 |
||||||
|
* @see AuthorizationServerContext |
||||||
|
* @see AuthorizationServerContextHolder |
||||||
|
* @see AuthorizationServerSettings |
||||||
|
*/ |
||||||
|
final class AuthorizationServerContextFilter extends OncePerRequestFilter { |
||||||
|
private final AuthorizationServerSettings authorizationServerSettings; |
||||||
|
|
||||||
|
AuthorizationServerContextFilter(AuthorizationServerSettings authorizationServerSettings) { |
||||||
|
Assert.notNull(authorizationServerSettings, "authorizationServerSettings cannot be null"); |
||||||
|
this.authorizationServerSettings = authorizationServerSettings; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
||||||
|
throws ServletException, IOException { |
||||||
|
|
||||||
|
try { |
||||||
|
AuthorizationServerContext authorizationServerContext = |
||||||
|
new DefaultAuthorizationServerContext( |
||||||
|
() -> resolveIssuer(this.authorizationServerSettings, request), |
||||||
|
this.authorizationServerSettings); |
||||||
|
AuthorizationServerContextHolder.setContext(authorizationServerContext); |
||||||
|
filterChain.doFilter(request, response); |
||||||
|
} finally { |
||||||
|
AuthorizationServerContextHolder.resetContext(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static String resolveIssuer(AuthorizationServerSettings authorizationServerSettings, HttpServletRequest request) { |
||||||
|
return authorizationServerSettings.getIssuer() != null ? |
||||||
|
authorizationServerSettings.getIssuer() : |
||||||
|
getContextPath(request); |
||||||
|
} |
||||||
|
|
||||||
|
private static String getContextPath(HttpServletRequest request) { |
||||||
|
// @formatter:off
|
||||||
|
return UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) |
||||||
|
.replacePath(request.getContextPath()) |
||||||
|
.replaceQuery(null) |
||||||
|
.fragment(null) |
||||||
|
.build() |
||||||
|
.toUriString(); |
||||||
|
// @formatter:on
|
||||||
|
} |
||||||
|
|
||||||
|
private static final class DefaultAuthorizationServerContext implements AuthorizationServerContext { |
||||||
|
private final Supplier<String> issuerSupplier; |
||||||
|
private final AuthorizationServerSettings authorizationServerSettings; |
||||||
|
|
||||||
|
private DefaultAuthorizationServerContext(Supplier<String> issuerSupplier, AuthorizationServerSettings authorizationServerSettings) { |
||||||
|
this.issuerSupplier = issuerSupplier; |
||||||
|
this.authorizationServerSettings = authorizationServerSettings; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String getIssuer() { |
||||||
|
return this.issuerSupplier.get(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public AuthorizationServerSettings getAuthorizationServerSettings() { |
||||||
|
return this.authorizationServerSettings; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,44 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.security.oauth2.server.authorization.context; |
||||||
|
|
||||||
|
import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; |
||||||
|
|
||||||
|
/** |
||||||
|
* A context that holds information of the Authorization Server runtime environment. |
||||||
|
* |
||||||
|
* @author Joe Grandja |
||||||
|
* @since 0.2.2 |
||||||
|
* @see AuthorizationServerSettings |
||||||
|
* @see AuthorizationServerContextHolder |
||||||
|
*/ |
||||||
|
public interface AuthorizationServerContext { |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the {@code URL} of the Authorization Server's issuer identifier. |
||||||
|
* |
||||||
|
* @return the {@code URL} of the Authorization Server's issuer identifier |
||||||
|
*/ |
||||||
|
String getIssuer(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the {@link AuthorizationServerSettings}. |
||||||
|
* |
||||||
|
* @return the {@link AuthorizationServerSettings} |
||||||
|
*/ |
||||||
|
AuthorizationServerSettings getAuthorizationServerSettings(); |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,60 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.security.oauth2.server.authorization.context; |
||||||
|
|
||||||
|
/** |
||||||
|
* A holder of the {@link AuthorizationServerContext} that associates it with the current thread using a {@code ThreadLocal}. |
||||||
|
* |
||||||
|
* @author Joe Grandja |
||||||
|
* @since 0.2.2 |
||||||
|
* @see AuthorizationServerContext |
||||||
|
*/ |
||||||
|
public final class AuthorizationServerContextHolder { |
||||||
|
private static final ThreadLocal<AuthorizationServerContext> holder = new ThreadLocal<>(); |
||||||
|
|
||||||
|
private AuthorizationServerContextHolder() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the {@link AuthorizationServerContext} bound to the current thread. |
||||||
|
* |
||||||
|
* @return the {@link AuthorizationServerContext} |
||||||
|
*/ |
||||||
|
public static AuthorizationServerContext getContext() { |
||||||
|
return holder.get(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Bind the given {@link AuthorizationServerContext} to the current thread. |
||||||
|
* |
||||||
|
* @param authorizationServerContext the {@link AuthorizationServerContext} |
||||||
|
*/ |
||||||
|
public static void setContext(AuthorizationServerContext authorizationServerContext) { |
||||||
|
if (authorizationServerContext == null) { |
||||||
|
resetContext(); |
||||||
|
} else { |
||||||
|
holder.set(authorizationServerContext); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Reset the {@link AuthorizationServerContext} bound to the current thread. |
||||||
|
*/ |
||||||
|
public static void resetContext() { |
||||||
|
holder.remove(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,70 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2020-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.security.oauth2.server.authorization.context; |
|
||||||
|
|
||||||
import java.util.function.Supplier; |
|
||||||
|
|
||||||
import org.springframework.lang.Nullable; |
|
||||||
import org.springframework.security.oauth2.server.authorization.settings.ProviderSettings; |
|
||||||
import org.springframework.util.Assert; |
|
||||||
|
|
||||||
/** |
|
||||||
* A context that holds information of the Provider. |
|
||||||
* |
|
||||||
* @author Joe Grandja |
|
||||||
* @since 0.2.2 |
|
||||||
* @see ProviderSettings |
|
||||||
* @see ProviderContextHolder |
|
||||||
*/ |
|
||||||
public final class ProviderContext { |
|
||||||
private final ProviderSettings providerSettings; |
|
||||||
private final Supplier<String> issuerSupplier; |
|
||||||
|
|
||||||
/** |
|
||||||
* Constructs a {@code ProviderContext} using the provided parameters. |
|
||||||
* |
|
||||||
* @param providerSettings the provider settings |
|
||||||
* @param issuerSupplier a {@code Supplier} for the {@code URL} of the Provider's issuer identifier |
|
||||||
*/ |
|
||||||
public ProviderContext(ProviderSettings providerSettings, @Nullable Supplier<String> issuerSupplier) { |
|
||||||
Assert.notNull(providerSettings, "providerSettings cannot be null"); |
|
||||||
this.providerSettings = providerSettings; |
|
||||||
this.issuerSupplier = issuerSupplier; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the {@link ProviderSettings}. |
|
||||||
* |
|
||||||
* @return the {@link ProviderSettings} |
|
||||||
*/ |
|
||||||
public ProviderSettings getProviderSettings() { |
|
||||||
return this.providerSettings; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the {@code URL} of the Provider's issuer identifier. |
|
||||||
* The issuer identifier is resolved from the constructor parameter {@code Supplier<String>} |
|
||||||
* or if not provided then defaults to {@link ProviderSettings#getIssuer()}. |
|
||||||
* |
|
||||||
* @return the {@code URL} of the Provider's issuer identifier |
|
||||||
*/ |
|
||||||
public String getIssuer() { |
|
||||||
return this.issuerSupplier != null ? |
|
||||||
this.issuerSupplier.get() : |
|
||||||
getProviderSettings().getIssuer(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,63 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2020-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.security.oauth2.server.authorization.context; |
|
||||||
|
|
||||||
import org.springframework.security.oauth2.server.authorization.web.ProviderContextFilter; |
|
||||||
|
|
||||||
/** |
|
||||||
* A holder of {@link ProviderContext} that associates it with the current thread using a {@code ThreadLocal}. |
|
||||||
* |
|
||||||
* @author Joe Grandja |
|
||||||
* @since 0.2.2 |
|
||||||
* @see ProviderContext |
|
||||||
* @see ProviderContextFilter |
|
||||||
*/ |
|
||||||
public final class ProviderContextHolder { |
|
||||||
private static final ThreadLocal<ProviderContext> holder = new ThreadLocal<>(); |
|
||||||
|
|
||||||
private ProviderContextHolder() { |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the {@link ProviderContext} bound to the current thread. |
|
||||||
* |
|
||||||
* @return the {@link ProviderContext} |
|
||||||
*/ |
|
||||||
public static ProviderContext getProviderContext() { |
|
||||||
return holder.get(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Bind the given {@link ProviderContext} to the current thread. |
|
||||||
* |
|
||||||
* @param providerContext the {@link ProviderContext} |
|
||||||
*/ |
|
||||||
public static void setProviderContext(ProviderContext providerContext) { |
|
||||||
if (providerContext == null) { |
|
||||||
resetProviderContext(); |
|
||||||
} else { |
|
||||||
holder.set(providerContext); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Reset the {@link ProviderContext} bound to the current thread. |
|
||||||
*/ |
|
||||||
public static void resetProviderContext() { |
|
||||||
holder.remove(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,86 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2020-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.security.oauth2.server.authorization.web; |
|
||||||
|
|
||||||
import java.io.IOException; |
|
||||||
|
|
||||||
import jakarta.servlet.FilterChain; |
|
||||||
import jakarta.servlet.ServletException; |
|
||||||
import jakarta.servlet.http.HttpServletRequest; |
|
||||||
import jakarta.servlet.http.HttpServletResponse; |
|
||||||
|
|
||||||
import org.springframework.security.oauth2.server.authorization.context.ProviderContext; |
|
||||||
import org.springframework.security.oauth2.server.authorization.context.ProviderContextHolder; |
|
||||||
import org.springframework.security.oauth2.server.authorization.settings.ProviderSettings; |
|
||||||
import org.springframework.security.web.util.UrlUtils; |
|
||||||
import org.springframework.util.Assert; |
|
||||||
import org.springframework.web.filter.OncePerRequestFilter; |
|
||||||
import org.springframework.web.util.UriComponentsBuilder; |
|
||||||
|
|
||||||
/** |
|
||||||
* A {@code Filter} that associates the {@link ProviderContext} to the {@link ProviderContextHolder}. |
|
||||||
* |
|
||||||
* @author Joe Grandja |
|
||||||
* @since 0.2.2 |
|
||||||
* @see ProviderContext |
|
||||||
* @see ProviderContextHolder |
|
||||||
* @see ProviderSettings |
|
||||||
*/ |
|
||||||
public final class ProviderContextFilter extends OncePerRequestFilter { |
|
||||||
private final ProviderSettings providerSettings; |
|
||||||
|
|
||||||
/** |
|
||||||
* Constructs a {@code ProviderContextFilter} using the provided parameters. |
|
||||||
* |
|
||||||
* @param providerSettings the provider settings |
|
||||||
*/ |
|
||||||
public ProviderContextFilter(ProviderSettings providerSettings) { |
|
||||||
Assert.notNull(providerSettings, "providerSettings cannot be null"); |
|
||||||
this.providerSettings = providerSettings; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
|
||||||
throws ServletException, IOException { |
|
||||||
|
|
||||||
try { |
|
||||||
ProviderContext providerContext = new ProviderContext( |
|
||||||
this.providerSettings, () -> resolveIssuer(this.providerSettings, request)); |
|
||||||
ProviderContextHolder.setProviderContext(providerContext); |
|
||||||
filterChain.doFilter(request, response); |
|
||||||
} finally { |
|
||||||
ProviderContextHolder.resetProviderContext(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
private static String resolveIssuer(ProviderSettings providerSettings, HttpServletRequest request) { |
|
||||||
return providerSettings.getIssuer() != null ? |
|
||||||
providerSettings.getIssuer() : |
|
||||||
getContextPath(request); |
|
||||||
} |
|
||||||
|
|
||||||
private static String getContextPath(HttpServletRequest request) { |
|
||||||
// @formatter:off
|
|
||||||
return UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) |
|
||||||
.replacePath(request.getContextPath()) |
|
||||||
.replaceQuery(null) |
|
||||||
.fragment(null) |
|
||||||
.build() |
|
||||||
.toUriString(); |
|
||||||
// @formatter:on
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,47 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.security.oauth2.server.authorization.context; |
||||||
|
|
||||||
|
import java.util.function.Supplier; |
||||||
|
|
||||||
|
import org.springframework.lang.Nullable; |
||||||
|
import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Joe Grandja |
||||||
|
*/ |
||||||
|
public class TestAuthorizationServerContext implements AuthorizationServerContext { |
||||||
|
private final AuthorizationServerSettings authorizationServerSettings; |
||||||
|
private final Supplier<String> issuerSupplier; |
||||||
|
|
||||||
|
public TestAuthorizationServerContext(AuthorizationServerSettings authorizationServerSettings, @Nullable Supplier<String> issuerSupplier) { |
||||||
|
this.authorizationServerSettings = authorizationServerSettings; |
||||||
|
this.issuerSupplier = issuerSupplier; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String getIssuer() { |
||||||
|
return this.issuerSupplier != null ? |
||||||
|
this.issuerSupplier.get() : |
||||||
|
getAuthorizationServerSettings().getIssuer(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public AuthorizationServerSettings getAuthorizationServerSettings() { |
||||||
|
return this.authorizationServerSettings; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,101 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2020-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.security.oauth2.server.authorization.web; |
|
||||||
|
|
||||||
import jakarta.servlet.FilterChain; |
|
||||||
|
|
||||||
import org.junit.After; |
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
import org.springframework.mock.web.MockHttpServletRequest; |
|
||||||
import org.springframework.mock.web.MockHttpServletResponse; |
|
||||||
import org.springframework.security.oauth2.server.authorization.context.ProviderContext; |
|
||||||
import org.springframework.security.oauth2.server.authorization.context.ProviderContextHolder; |
|
||||||
import org.springframework.security.oauth2.server.authorization.settings.ProviderSettings; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
||||||
import static org.mockito.ArgumentMatchers.any; |
|
||||||
import static org.mockito.Mockito.doAnswer; |
|
||||||
import static org.mockito.Mockito.mock; |
|
||||||
|
|
||||||
/** |
|
||||||
* Tests for {@link ProviderContextFilter}. |
|
||||||
* |
|
||||||
* @author Joe Grandja |
|
||||||
*/ |
|
||||||
public class ProviderContextFilterTests { |
|
||||||
|
|
||||||
@After |
|
||||||
public void cleanup() { |
|
||||||
ProviderContextHolder.resetProviderContext(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void constructorWhenProviderSettingsNullThenThrowIllegalArgumentException() { |
|
||||||
assertThatThrownBy(() -> new ProviderContextFilter(null)) |
|
||||||
.isInstanceOf(IllegalArgumentException.class) |
|
||||||
.hasMessage("providerSettings cannot be null"); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void doFilterWhenIssuerConfiguredThenUsed() throws Exception { |
|
||||||
String issuer = "https://provider.com"; |
|
||||||
ProviderSettings providerSettings = ProviderSettings.builder().issuer(issuer).build(); |
|
||||||
ProviderContextFilter filter = new ProviderContextFilter(providerSettings); |
|
||||||
|
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); |
|
||||||
request.setServletPath("/"); |
|
||||||
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
||||||
FilterChain filterChain = mock(FilterChain.class); |
|
||||||
|
|
||||||
doAnswer(invocation -> { |
|
||||||
ProviderContext providerContext = ProviderContextHolder.getProviderContext(); |
|
||||||
assertThat(providerContext).isNotNull(); |
|
||||||
assertThat(providerContext.getProviderSettings()).isSameAs(providerSettings); |
|
||||||
assertThat(providerContext.getIssuer()).isEqualTo(issuer); |
|
||||||
return null; |
|
||||||
}).when(filterChain).doFilter(any(), any()); |
|
||||||
|
|
||||||
filter.doFilter(request, response, filterChain); |
|
||||||
|
|
||||||
assertThat(ProviderContextHolder.getProviderContext()).isNull(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void doFilterWhenIssuerNotConfiguredThenResolveFromRequest() throws Exception { |
|
||||||
ProviderSettings providerSettings = ProviderSettings.builder().build(); |
|
||||||
ProviderContextFilter filter = new ProviderContextFilter(providerSettings); |
|
||||||
|
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); |
|
||||||
request.setServletPath("/"); |
|
||||||
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
||||||
FilterChain filterChain = mock(FilterChain.class); |
|
||||||
|
|
||||||
doAnswer(invocation -> { |
|
||||||
ProviderContext providerContext = ProviderContextHolder.getProviderContext(); |
|
||||||
assertThat(providerContext).isNotNull(); |
|
||||||
assertThat(providerContext.getProviderSettings()).isSameAs(providerSettings); |
|
||||||
assertThat(providerContext.getIssuer()).isEqualTo("http://localhost"); |
|
||||||
return null; |
|
||||||
}).when(filterChain).doFilter(any(), any()); |
|
||||||
|
|
||||||
filter.doFilter(request, response, filterChain); |
|
||||||
|
|
||||||
assertThat(ProviderContextHolder.getProviderContext()).isNull(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue