|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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 not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law.or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.springframework.security.ldap.authentication; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.InjectMocks; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | + |
| 27 | +import org.springframework.dao.EmptyResultDataAccessException; |
| 28 | +import org.springframework.ldap.core.AttributesMapper; |
| 29 | +import org.springframework.ldap.core.DirContextOperations; |
| 30 | +import org.springframework.ldap.core.LdapClient; |
| 31 | +import org.springframework.ldap.core.support.BaseLdapPathContextSource; |
| 32 | +import org.springframework.ldap.query.LdapQuery; |
| 33 | +import org.springframework.ldap.support.LdapUtils; |
| 34 | +import org.springframework.security.authentication.BadCredentialsException; |
| 35 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 36 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 37 | +import org.springframework.security.ldap.search.LdapUserSearch; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 40 | +import static org.mockito.ArgumentMatchers.any; |
| 41 | +import static org.mockito.BDDMockito.given; |
| 42 | +import static org.mockito.Mockito.mock; |
| 43 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 44 | + |
| 45 | +/** |
| 46 | + * Unit tests for {@link PasswordComparisonAuthenticator}. |
| 47 | + * |
| 48 | + * @author Minkuk Jo |
| 49 | + */ |
| 50 | +@ExtendWith(MockitoExtension.class) |
| 51 | +class PasswordComparisonAuthenticatorUnitTests { |
| 52 | + |
| 53 | + @Mock |
| 54 | + BaseLdapPathContextSource contextSource; |
| 55 | + |
| 56 | + @InjectMocks |
| 57 | + PasswordComparisonAuthenticator authenticator; |
| 58 | + |
| 59 | + @Mock |
| 60 | + LdapClient ldapClient; |
| 61 | + |
| 62 | + @Mock |
| 63 | + LdapClient.SearchSpec searchSpec; |
| 64 | + |
| 65 | + @Test |
| 66 | + void authenticateWhenUserNotFoundThenThrowsUsernameNotFoundException() { |
| 67 | + this.authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" }); |
| 68 | + this.authenticator.ldapClient = this.ldapClient; |
| 69 | + UsernamePasswordAuthenticationToken authentication = UsernamePasswordAuthenticationToken.unauthenticated("user", |
| 70 | + "password"); |
| 71 | + given(this.ldapClient.search()).willReturn(this.searchSpec); |
| 72 | + given(this.searchSpec.query(any(LdapQuery.class))).willReturn(this.searchSpec); |
| 73 | + given(this.searchSpec.toObject(any(AttributesMapper.class))).willThrow(new EmptyResultDataAccessException(1)); |
| 74 | + LdapUserSearch userSearch = mock(LdapUserSearch.class); |
| 75 | + this.authenticator.setUserSearch(userSearch); |
| 76 | + given(userSearch.searchForUser("user")).willReturn(null); |
| 77 | + |
| 78 | + assertThatExceptionOfType(UsernameNotFoundException.class) |
| 79 | + .isThrownBy(() -> this.authenticator.authenticate(authentication)) |
| 80 | + .withMessage("user not found"); |
| 81 | + verifyNoInteractions(this.contextSource); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @Test |
| 86 | + void authenticateWhenPasswordCompareFailsThenThrowsBadCredentialsException() { |
| 87 | + this.authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" }); |
| 88 | + this.authenticator.ldapClient = this.ldapClient; |
| 89 | + UsernamePasswordAuthenticationToken authentication = UsernamePasswordAuthenticationToken.unauthenticated("user", |
| 90 | + "password"); |
| 91 | + |
| 92 | + DirContextOperations user = mock(DirContextOperations.class); |
| 93 | + LdapClient.SearchSpec userSearchSpec = mock(LdapClient.SearchSpec.class); |
| 94 | + given(user.getDn()).willReturn(LdapUtils.newLdapName("uid=user,ou=people")); |
| 95 | + given(userSearchSpec.query(any(LdapQuery.class))).willReturn(userSearchSpec); |
| 96 | + given(userSearchSpec.toObject(any(AttributesMapper.class))).willReturn(user); |
| 97 | + |
| 98 | + LdapClient.SearchSpec passwordSearchSpec = mock(LdapClient.SearchSpec.class); |
| 99 | + given(passwordSearchSpec.query(any(LdapQuery.class))).willReturn(passwordSearchSpec); |
| 100 | + given(passwordSearchSpec.toList(any(AttributesMapper.class))).willReturn(Collections.emptyList()); |
| 101 | + |
| 102 | + given(this.ldapClient.search()).willReturn(userSearchSpec, passwordSearchSpec); |
| 103 | + |
| 104 | + assertThatExceptionOfType(BadCredentialsException.class) |
| 105 | + .isThrownBy(() -> this.authenticator.authenticate(authentication)); |
| 106 | + } |
| 107 | +} |
0 commit comments