forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes spring-projectsgh-14644
- Loading branch information
ruabtmh
authored and
ruabtmh
committed
Feb 27, 2024
1 parent
e771267
commit 7854a5c
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...rg/springframework/security/oauth2/server/resource/web/DelegatingBearerTokenResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2002-2024 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.resource.web; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link BearerTokenResolver}, that iterates over multiple {@link BearerTokenResolver}. | ||
* | ||
* @author Max Batischev | ||
* @since 6.3 | ||
*/ | ||
public class DelegatingBearerTokenResolver implements BearerTokenResolver { | ||
|
||
private final List<BearerTokenResolver> delegates; | ||
|
||
public DelegatingBearerTokenResolver(List<BearerTokenResolver> delegates) { | ||
Assert.notEmpty(delegates, "delegates cannot be null"); | ||
this.delegates = delegates; | ||
} | ||
|
||
public DelegatingBearerTokenResolver(BearerTokenResolver... delegates) { | ||
this.delegates = List.of(delegates); | ||
} | ||
|
||
@Override | ||
public String resolve(HttpServletRequest request) { | ||
return (this.delegates).stream().map((d) -> d.resolve(request)).filter(Objects::nonNull).findAny().orElse(null); | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
...ringframework/security/oauth2/server/resource/web/DelegatingBearerTokenResolverTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2002-2024 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.resource.web; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link DelegatingBearerTokenResolver}. | ||
* | ||
* @author Max Batischev | ||
*/ | ||
public class DelegatingBearerTokenResolverTests { | ||
|
||
private static final String X_AUTH_TOKEN_HEADER = "X-Auth-Token"; | ||
|
||
private static final String TEST_BEARER_TOKEN = "test-bearer-token"; | ||
|
||
private static final String TEST_X_AUTH_TOKEN = "test-x-auth-token"; | ||
|
||
private static final String X_AUTHORIZATION_HEADER = "x-authorization"; | ||
|
||
private static final String X_AUTHORIZATION_TOKEN = "test-x-authorization-token"; | ||
|
||
@Test | ||
public void resolveWhenBearerAuthorizationHeaderIsPresentThenBearerTokenIsResolved() { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + TEST_BEARER_TOKEN); | ||
|
||
DelegatingBearerTokenResolver bearerTokenResolver = resolverWithTwoDelegates(); | ||
|
||
assertThat(bearerTokenResolver.resolve(request)).isEqualTo(TEST_BEARER_TOKEN); | ||
} | ||
|
||
@Test | ||
public void resolveWhenXAuthTokenHeaderIsPresentThenXAuthTokenIsResolved() { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.addHeader(X_AUTH_TOKEN_HEADER, TEST_X_AUTH_TOKEN); | ||
|
||
DelegatingBearerTokenResolver bearerTokenResolver = resolverWithTwoDelegates(); | ||
|
||
assertThat(bearerTokenResolver.resolve(request)).isEqualTo(TEST_X_AUTH_TOKEN); | ||
} | ||
|
||
@Test | ||
public void resolveWhenXAuthorizationHeaderIsPresentThenTokenIsNotResolved() { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.addHeader(X_AUTHORIZATION_HEADER, X_AUTHORIZATION_TOKEN); | ||
|
||
DelegatingBearerTokenResolver bearerTokenResolver = resolverWithTwoDelegates(); | ||
|
||
assertThat(bearerTokenResolver.resolve(request)).isNull(); | ||
} | ||
|
||
private DelegatingBearerTokenResolver resolverWithTwoDelegates() { | ||
return new DelegatingBearerTokenResolver(new DefaultBearerTokenResolver(), | ||
new HeaderBearerTokenResolver(X_AUTH_TOKEN_HEADER)); | ||
} | ||
|
||
} |