Skip to content

Commit

Permalink
Added DelegatingBearerTokenResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ruabtmh authored and ruabtmh committed Feb 27, 2024
1 parent e771267 commit 7854a5c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
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);
}

}
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));
}

}

0 comments on commit 7854a5c

Please sign in to comment.