Skip to content

Commit 6847a99

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 0446bf7 commit 6847a99

3 files changed

Lines changed: 450 additions & 3 deletions

File tree

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

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

23+
import com.google.common.annotations.VisibleForTesting;
24+
import org.jivesoftware.openfire.XMPPServer;
2325
import org.jivesoftware.openfire.lockout.LockOutManager;
2426
import org.jivesoftware.openfire.sasl.ScramSha1SaslServer;
2527
import org.jivesoftware.openfire.user.UserNotFoundException;
@@ -182,12 +184,36 @@ public static void setPassword(String username, String password) throws UserNotF
182184
*/
183185
public static AuthToken authenticate(String username, String password)
184186
throws UnauthorizedException, ConnectionException, InternalUnauthenticatedException {
185-
if (LockOutManager.getInstance().isAccountDisabled(username)) {
186-
LockOutManager.getInstance().recordFailedLogin(username);
187+
// The default provider accepts 'user@domain' for this server's domain; the lockout and the token are for the
188+
// account, so use its bare username for them. The provider is asked about the username as given.
189+
final String accountName = withoutServerDomain(username);
190+
if (LockOutManager.getInstance().isAccountDisabled(accountName)) {
191+
LockOutManager.getInstance().recordFailedLogin(accountName);
187192
throw new UnauthorizedException();
188193
}
189194
authProvider.authenticate(username, password);
190-
return AuthToken.generateUserToken( username );
195+
return AuthToken.generateUserToken( accountName );
196+
}
197+
198+
/**
199+
* Returns a username without a trailing '@' and this server's XMPP domain, which {@link DefaultAuthProvider}
200+
* accepts in a username; any other username is returned as it is.
201+
*
202+
* @param username the username as given.
203+
* @return the username without this server's domain.
204+
*/
205+
@VisibleForTesting
206+
static String withoutServerDomain(final String username) {
207+
if (username == null) {
208+
return null;
209+
}
210+
final int index = username.indexOf('@');
211+
if (index < 0) {
212+
return username;
213+
}
214+
final XMPPServer server = XMPPServer.getInstance();
215+
final String domain = server == null || server.getServerInfo() == null ? null : server.getServerInfo().getXMPPDomain();
216+
return domain != null && username.substring(index + 1).trim().equalsIgnoreCase(domain) ? username.substring(0, index) : username;
191217
}
192218

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

0 commit comments

Comments
 (0)