Skip to content

Commit d4179e6

Browse files
committed
OF-2279: Check the lockout of user@domain logins under the bare username
A SASL PLAIN login that gives the username with this server's domain (alice@example.org), which DefaultAuthProvider accepts, fails with not-authorized even with the right password, and the cause is logged only at DEBUG. This has been so since OF-2289 (8345443, in 4.7.0): AuthFactory.authenticate checks the lockout under the name as given, LockOutManager.getUserLockOut now builds a JID from it for its mutex, and nodeprep rejects the '@', so an IllegalArgumentException escapes before the provider is asked, and XMPPCallbackHandler turns it into a failed verification. It is fixed here because the login limits make it worse: they count the attempt against 'alice' when it is admitted and give it back only on success, so every such login, with the right password, would count as a failure, and enough of them would block the address or lock the account. AuthFactory.authenticate now removes a trailing '@' and this server's domain, as DefaultAuthProvider does, and uses the bare username for the lockout check, for recording a refused login and for the token. The provider is still asked about the username as given, so its behaviour is unchanged. A username with another domain is refused as before. This is a separate commit so that it can be split into its own change if the reviewer prefers.
1 parent be90abd commit d4179e6

3 files changed

Lines changed: 426 additions & 3 deletions

File tree

‎xmppserver/src/main/java/org/jivesoftware/openfire/auth/AuthFactory.java‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.security.NoSuchAlgorithmException;
2121
import java.util.Set;
2222

23+
import org.jivesoftware.openfire.XMPPServer;
2324
import org.jivesoftware.openfire.lockout.LockOutManager;
2425
import org.jivesoftware.openfire.sasl.ScramSha1SaslServer;
2526
import org.jivesoftware.openfire.user.UserNotFoundException;
@@ -182,12 +183,35 @@ public static void setPassword(String username, String password) throws UserNotF
182183
*/
183184
public static AuthToken authenticate(String username, String password)
184185
throws UnauthorizedException, ConnectionException, InternalUnauthenticatedException {
185-
if (LockOutManager.getInstance().isAccountDisabled(username)) {
186-
LockOutManager.getInstance().recordFailedLogin(username);
186+
// The default provider accepts 'user@domain' for this server's domain; the lockout and the token are for the
187+
// account, so use its bare username for them. The provider is asked about the username as given.
188+
final String accountName = withoutServerDomain(username);
189+
if (LockOutManager.getInstance().isAccountDisabled(accountName)) {
190+
LockOutManager.getInstance().recordFailedLogin(accountName);
187191
throw new UnauthorizedException();
188192
}
189193
authProvider.authenticate(username, password);
190-
return AuthToken.generateUserToken( username );
194+
return AuthToken.generateUserToken( accountName );
195+
}
196+
197+
/**
198+
* Returns a username without a trailing '@' and this server's XMPP domain, which {@link DefaultAuthProvider}
199+
* accepts in a username; any other username is returned as it is.
200+
*
201+
* @param username the username as given.
202+
* @return the username without this server's domain.
203+
*/
204+
private static String withoutServerDomain(final String username) {
205+
if (username == null) {
206+
return null;
207+
}
208+
final int index = username.indexOf('@');
209+
if (index < 0) {
210+
return username;
211+
}
212+
final XMPPServer server = XMPPServer.getInstance();
213+
final String domain = server == null || server.getServerInfo() == null ? null : server.getServerInfo().getXMPPDomain();
214+
return domain != null && username.substring(index + 1).trim().toLowerCase().equals(domain) ? username.substring(0, index) : username;
191215
}
192216

193217
/**
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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

Comments
 (0)