|
| 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