|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved. |
| 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 | + * http://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.jivesoftware.openfire.auth; |
| 18 | + |
| 19 | +import org.jivesoftware.Fixtures; |
| 20 | +import org.jivesoftware.openfire.XMPPServer; |
| 21 | +import org.jivesoftware.openfire.lockout.LockOutManager; |
| 22 | +import org.jivesoftware.openfire.user.UserNotFoundException; |
| 23 | +import org.jivesoftware.util.cache.CacheFactory; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.BeforeAll; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.mockito.MockedStatic; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.mockStatic; |
| 39 | +import static org.mockito.Mockito.never; |
| 40 | +import static org.mockito.Mockito.times; |
| 41 | +import static org.mockito.Mockito.verify; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +/** |
| 45 | + * Unit tests for {@link AuthFactory#authenticate(String, String)}, for usernames given with this server's domain |
| 46 | + * ('alice@domain'), which the default authentication provider accepts. |
| 47 | + * |
| 48 | + * @author Matthew Vivian |
| 49 | + */ |
| 50 | +public class AuthFactoryTest |
| 51 | +{ |
| 52 | + static final String DOMAIN = Fixtures.XMPP_DOMAIN; |
| 53 | + static final String PASSWORD = "secret"; |
| 54 | + |
| 55 | + private Class<?> originalProvider; |
| 56 | + |
| 57 | + /** |
| 58 | + * Accepts 'alice' with the password 'secret', stripping this server's domain as {@link DefaultAuthProvider} does, |
| 59 | + * and records the usernames it is asked about. |
| 60 | + */ |
| 61 | + public static class DomainStrippingAuthProvider implements AuthProvider |
| 62 | + { |
| 63 | + public static final List<String> calls = Collections.synchronizedList(new ArrayList<>()); |
| 64 | + |
| 65 | + @Override |
| 66 | + public void authenticate(String username, String password) throws UnauthorizedException |
| 67 | + { |
| 68 | + calls.add(username); |
| 69 | + username = username.trim().toLowerCase(); |
| 70 | + if (username.contains("@")) { |
| 71 | + final int index = username.indexOf('@'); |
| 72 | + if (!username.substring(index + 1).equals(DOMAIN)) { |
| 73 | + throw new UnauthorizedException(); |
| 74 | + } |
| 75 | + username = username.substring(0, index); |
| 76 | + } |
| 77 | + if (!"alice".equals(username) || !PASSWORD.equals(password)) { |
| 78 | + throw new UnauthorizedException(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override public String getPassword(String username) throws UserNotFoundException { throw new UserNotFoundException(); } |
| 83 | + @Override public void setPassword(String username, String password) { throw new UnsupportedOperationException(); } |
| 84 | + @Override public boolean supportsPasswordRetrieval() { return false; } |
| 85 | + @Override public boolean isScramSupported() { return false; } |
| 86 | + @Override public String getSalt(String username) { throw new UnsupportedOperationException(); } |
| 87 | + @Override public int getIterations(String username) { throw new UnsupportedOperationException(); } |
| 88 | + @Override public String getServerKey(String username) { throw new UnsupportedOperationException(); } |
| 89 | + @Override public String getStoredKey(String username) { throw new UnsupportedOperationException(); } |
| 90 | + } |
| 91 | + |
| 92 | + @BeforeAll |
| 93 | + public static void setUpClass() throws Exception |
| 94 | + { |
| 95 | + Fixtures.reconfigureOpenfireHome(); |
| 96 | + Fixtures.disableDatabasePersistence(); |
| 97 | + CacheFactory.initialize(); |
| 98 | + } |
| 99 | + |
| 100 | + @BeforeEach |
| 101 | + public void setUp() |
| 102 | + { |
| 103 | + XMPPServer.setInstance(Fixtures.mockXMPPServer()); |
| 104 | + originalProvider = AuthFactory.AUTH_PROVIDER.getValue(); |
| 105 | + AuthFactory.AUTH_PROVIDER.setValue(DomainStrippingAuthProvider.class); |
| 106 | + DomainStrippingAuthProvider.calls.clear(); |
| 107 | + } |
| 108 | + |
| 109 | + @AfterEach |
| 110 | + public void tearDown() |
| 111 | + { |
| 112 | + AuthFactory.AUTH_PROVIDER.setValue((Class) originalProvider); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Verifies that a bare username is authenticated as before. |
| 117 | + */ |
| 118 | + @Test |
| 119 | + public void testAuthenticateBareUsername() throws Exception |
| 120 | + { |
| 121 | + // Execute system under test. |
| 122 | + final AuthToken token = AuthFactory.authenticate("alice", PASSWORD); |
| 123 | + |
| 124 | + // Verify result. |
| 125 | + assertEquals("alice", token.getUsername(), "the token must be for the bare username"); |
| 126 | + assertEquals(List.of("alice"), DomainStrippingAuthProvider.calls, "the provider must be asked about the username as given"); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Verifies that a username with this server's domain is authenticated: the lockout check is made for the bare |
| 131 | + * username, the provider is asked about the username as given, and the token is for the bare username. |
| 132 | + */ |
| 133 | + @Test |
| 134 | + public void testAuthenticateUsernameWithServerDomain() throws Exception |
| 135 | + { |
| 136 | + // Execute system under test. |
| 137 | + final AuthToken token = AuthFactory.authenticate("alice@" + DOMAIN, PASSWORD); |
| 138 | + |
| 139 | + // Verify result. |
| 140 | + assertEquals("alice", token.getUsername(), "the token must be for the bare username"); |
| 141 | + assertEquals(List.of("alice@" + DOMAIN), DomainStrippingAuthProvider.calls, "the provider must be asked about the username as given"); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Verifies that a wrong password for a username with this server's domain is still refused. |
| 146 | + */ |
| 147 | + @Test |
| 148 | + public void testAuthenticateUsernameWithServerDomainAndWrongPassword() |
| 149 | + { |
| 150 | + // Execute system under test & verify result. |
| 151 | + assertThrows(UnauthorizedException.class, () -> AuthFactory.authenticate("alice@" + DOMAIN, "wrong"), |
| 152 | + "a wrong password must be refused"); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Verifies that a locked account is refused when the username is given with this server's domain, without asking |
| 157 | + * the provider, and that the refusal is recorded against the bare username. |
| 158 | + */ |
| 159 | + @Test |
| 160 | + public void testLockedAccountIsRefusedForUsernameWithServerDomain() |
| 161 | + { |
| 162 | + // Setup test fixture. |
| 163 | + final LockOutManager lockOutManager = mock(LockOutManager.class); |
| 164 | + when(lockOutManager.isAccountDisabled("alice")).thenReturn(true); |
| 165 | + try (final MockedStatic<LockOutManager> lockOutStatic = mockStatic(LockOutManager.class)) { |
| 166 | + lockOutStatic.when(LockOutManager::getInstance).thenReturn(lockOutManager); |
| 167 | + |
| 168 | + // Execute system under test. |
| 169 | + assertThrows(UnauthorizedException.class, () -> AuthFactory.authenticate("alice@" + DOMAIN, PASSWORD), |
| 170 | + "a locked account must be refused whatever form its username is given in"); |
| 171 | + |
| 172 | + // Verify result. |
| 173 | + verify(lockOutManager, times(1).description("the refusal must be recorded against the bare username")).recordFailedLogin("alice"); |
| 174 | + verify(lockOutManager, never().description("the lockout must not be looked up under the username with the domain")).isAccountDisabled("alice@" + DOMAIN); |
| 175 | + assertTrue(DomainStrippingAuthProvider.calls.isEmpty(), "the provider must not be asked about a locked account"); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Verifies that a username with another domain is not treated as a local account: the provider is not asked, as |
| 181 | + * before. |
| 182 | + */ |
| 183 | + @Test |
| 184 | + public void testUsernameWithOtherDomainIsNotAuthenticated() |
| 185 | + { |
| 186 | + // Execute system under test & verify result. |
| 187 | + assertThrows(Exception.class, () -> AuthFactory.authenticate("alice@other.example", PASSWORD), |
| 188 | + "a username with another domain must not be authenticated"); |
| 189 | + assertTrue(DomainStrippingAuthProvider.calls.isEmpty(), "the provider must not be asked about a username with another domain"); |
| 190 | + } |
| 191 | +} |
0 commit comments