Skip to content

Commit c0928bf

Browse files
ruabtmhjzheaux
ruabtmh
authored andcommitted
Add DelegatingAuthenticationConverter
Closes gh-14644
1 parent d2f1e39 commit c0928bf

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2002-2024 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+
* https://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.web.authentication;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import jakarta.servlet.http.HttpServletRequest;
23+
24+
import org.springframework.security.core.Authentication;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* A {@link AuthenticationConverter}, that iterates over multiple
29+
* {@link AuthenticationConverter}. The first non-null {@link Authentication} will be used
30+
* as a result.
31+
*
32+
* @author Max Batischev
33+
* @since 6.3
34+
*/
35+
public final class DelegatingAuthenticationConverter implements AuthenticationConverter {
36+
37+
private final List<AuthenticationConverter> delegates;
38+
39+
public DelegatingAuthenticationConverter(List<AuthenticationConverter> delegates) {
40+
Assert.notEmpty(delegates, "delegates cannot be null");
41+
this.delegates = new ArrayList<>(delegates);
42+
}
43+
44+
public DelegatingAuthenticationConverter(AuthenticationConverter... delegates) {
45+
Assert.notEmpty(delegates, "delegates cannot be null");
46+
this.delegates = List.of(delegates);
47+
}
48+
49+
@Override
50+
public Authentication convert(HttpServletRequest request) {
51+
for (AuthenticationConverter delegate : this.delegates) {
52+
Authentication authentication = delegate.convert(request);
53+
if (authentication != null) {
54+
return authentication;
55+
}
56+
}
57+
return null;
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2002-2024 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+
* https://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.web.authentication;
18+
19+
import jakarta.servlet.http.HttpServletRequest;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
import org.mockito.Mock;
23+
import org.mockito.junit.jupiter.MockitoExtension;
24+
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.mock.web.MockHttpServletRequest;
27+
import org.springframework.security.authentication.AuthenticationDetailsSource;
28+
import org.springframework.security.authentication.BadCredentialsException;
29+
import org.springframework.security.authentication.TestingAuthenticationToken;
30+
import org.springframework.security.core.Authentication;
31+
import org.springframework.security.test.web.CodecTestUtils;
32+
import org.springframework.security.web.authentication.www.BasicAuthenticationConverter;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
36+
37+
/**
38+
* Tests for {@link DelegatingAuthenticationConverter}.
39+
*
40+
* @author Max Batischev
41+
*/
42+
@ExtendWith(MockitoExtension.class)
43+
public class DelegatingAuthenticationConverterTests {
44+
45+
private static final String X_AUTH_TOKEN_HEADER = "X-Auth-Token";
46+
47+
private static final String TEST_X_AUTH_TOKEN = "test-x-auth-token";
48+
49+
private static final String TEST_CUSTOM_PRINCIPAL = "test_custom_principal";
50+
51+
private static final String TEST_CUSTOM_CREDENTIALS = "test_custom_credentials";
52+
53+
private static final String TEST_BASIC_CREDENTIALS = "username:password";
54+
55+
private static final String INVALID_BASIC_CREDENTIALS = "invalid_credentials";
56+
57+
private DelegatingAuthenticationConverter converter;
58+
59+
@Mock
60+
private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource;
61+
62+
@Test
63+
public void requestWhenBasicAuthorizationHeaderIsPresentThenAuthenticates() {
64+
MockHttpServletRequest request = new MockHttpServletRequest();
65+
request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + CodecTestUtils.encodeBase64(TEST_BASIC_CREDENTIALS));
66+
this.converter = new DelegatingAuthenticationConverter(
67+
new BasicAuthenticationConverter(this.authenticationDetailsSource),
68+
new TestNullableAuthenticationConverter());
69+
70+
Authentication authentication = this.converter.convert(request);
71+
72+
assertThat(authentication).isNotNull();
73+
assertThat(authentication.getName()).isEqualTo("username");
74+
}
75+
76+
@Test
77+
public void requestWhenXAuthHeaderIsPresentThenAuthenticates() {
78+
MockHttpServletRequest request = new MockHttpServletRequest();
79+
request.addHeader(X_AUTH_TOKEN_HEADER, TEST_X_AUTH_TOKEN);
80+
this.converter = new DelegatingAuthenticationConverter(new TestAuthenticationConverter(),
81+
new TestNullableAuthenticationConverter());
82+
83+
Authentication authentication = this.converter.convert(request);
84+
85+
assertThat(authentication).isNotNull();
86+
assertThat(authentication.getName()).isEqualTo(TEST_CUSTOM_PRINCIPAL);
87+
}
88+
89+
@Test
90+
public void requestWhenXAuthHeaderIsPresentThenDoesntAuthenticate() {
91+
MockHttpServletRequest request = new MockHttpServletRequest();
92+
request.addHeader(X_AUTH_TOKEN_HEADER, TEST_X_AUTH_TOKEN);
93+
this.converter = new DelegatingAuthenticationConverter(new TestNullableAuthenticationConverter());
94+
95+
Authentication authentication = this.converter.convert(request);
96+
97+
assertThat(authentication).isNull();
98+
}
99+
100+
@Test
101+
public void requestWhenInvalidBasicAuthorizationTokenThenError() {
102+
MockHttpServletRequest request = new MockHttpServletRequest();
103+
request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + CodecTestUtils.encodeBase64(INVALID_BASIC_CREDENTIALS));
104+
this.converter = new DelegatingAuthenticationConverter(
105+
new BasicAuthenticationConverter(this.authenticationDetailsSource),
106+
new TestNullableAuthenticationConverter());
107+
108+
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.converter.convert(request));
109+
}
110+
111+
private static class TestAuthenticationConverter implements AuthenticationConverter {
112+
113+
@Override
114+
public Authentication convert(HttpServletRequest request) {
115+
String header = request.getHeader(X_AUTH_TOKEN_HEADER);
116+
if (header != null) {
117+
return new TestingAuthenticationToken(TEST_CUSTOM_PRINCIPAL, TEST_CUSTOM_CREDENTIALS);
118+
}
119+
else {
120+
return null;
121+
}
122+
}
123+
124+
}
125+
126+
private static class TestNullableAuthenticationConverter implements AuthenticationConverter {
127+
128+
@Override
129+
public Authentication convert(HttpServletRequest request) {
130+
return null;
131+
}
132+
133+
}
134+
135+
}

0 commit comments

Comments
 (0)