Skip to content

Commit daf664c

Browse files
vishesh92dhslove
authored andcommitted
Fixup response code on incorrect credentials (apache#8671)
1 parent a88f395 commit daf664c

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

plugins/user-authenticators/oauth2/src/main/java/org/apache/cloudstack/oauth2/OAuth2UserAuthenticator.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,46 @@
3131
import javax.inject.Inject;
3232
import java.util.Map;
3333

34+
import static org.apache.cloudstack.oauth2.OAuth2AuthManager.OAuth2IsPluginEnabled;
35+
3436
public class OAuth2UserAuthenticator extends AdapterBase implements UserAuthenticator {
3537

3638
@Inject
37-
private UserAccountDao _userAccountDao;
39+
private UserAccountDao userAccountDao;
3840
@Inject
39-
private UserDao _userDao;
41+
private UserDao userDao;
4042

4143
@Inject
42-
private OAuth2AuthManager _userOAuth2mgr;
44+
private OAuth2AuthManager userOAuth2mgr;
4345

4446
@Override
4547
public Pair<Boolean, ActionOnFailedAuthentication> authenticate(String username, String password, Long domainId, Map<String, Object[]> requestParameters) {
4648
if (logger.isDebugEnabled()) {
4749
logger.debug("Trying OAuth2 auth for user: " + username);
4850
}
4951

50-
final UserAccount userAccount = _userAccountDao.getUserAccount(username, domainId);
51-
if (userAccount == null || requestParameters == null || !OAuth2AuthManager.OAuth2IsPluginEnabled.value()) {
52-
logger.debug("Unable to find user with " + username + " in domain " + domainId + ", or user source is not OAUTH2");
52+
if (!isOAuthPluginEnabled()) {
53+
s_logger.debug("OAuth2 plugin is disabled");
54+
return new Pair<Boolean, ActionOnFailedAuthentication>(false, null);
55+
} else if (requestParameters == null) {
56+
s_logger.debug("Request parameters are null");
57+
return new Pair<Boolean, ActionOnFailedAuthentication>(false, null);
58+
}
59+
60+
final UserAccount userAccount = userAccountDao.getUserAccount(username, domainId);
61+
if (userAccount == null) {
62+
s_logger.debug("Unable to find user with " + username + " in domain " + domainId + ", or user source is not OAUTH2");
5363
return new Pair<Boolean, ActionOnFailedAuthentication>(false, null);
5464
} else {
55-
User user = _userDao.getUser(userAccount.getId());
65+
User user = userDao.getUser(userAccount.getId());
5666
final String[] provider = (String[])requestParameters.get(ApiConstants.PROVIDER);
5767
final String[] emailArray = (String[])requestParameters.get(ApiConstants.EMAIL);
5868
final String[] secretCodeArray = (String[])requestParameters.get(ApiConstants.SECRET_CODE);
5969
String oauthProvider = ((provider == null) ? null : provider[0]);
6070
String email = ((emailArray == null) ? null : emailArray[0]);
6171
String secretCode = ((secretCodeArray == null) ? null : secretCodeArray[0]);
6272

63-
UserOAuth2Authenticator authenticator = _userOAuth2mgr.getUserOAuth2AuthenticationProvider(oauthProvider);
73+
UserOAuth2Authenticator authenticator = userOAuth2mgr.getUserOAuth2AuthenticationProvider(oauthProvider);
6474
if (user != null && authenticator.verifyUser(email, secretCode)) {
6575
return new Pair<Boolean, ActionOnFailedAuthentication>(true, null);
6676
}
@@ -73,4 +83,8 @@ public Pair<Boolean, ActionOnFailedAuthentication> authenticate(String username,
7383
public String encode(String password) {
7484
return null;
7585
}
86+
87+
protected boolean isOAuthPluginEnabled() {
88+
return OAuth2IsPluginEnabled.value();
89+
}
7690
}

plugins/user-authenticators/oauth2/src/test/java/org/apache/cloudstack/oauth2/OAuth2UserAuthenticatorTest.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,29 @@
2727
import org.junit.After;
2828
import org.junit.Before;
2929
import org.junit.Test;
30+
import org.junit.runner.RunWith;
3031
import org.mockito.InjectMocks;
3132
import org.mockito.Mock;
3233
import org.mockito.MockitoAnnotations;
34+
import org.mockito.Spy;
35+
import org.mockito.junit.MockitoJUnitRunner;
3336

3437
import java.util.HashMap;
3538
import java.util.Map;
3639

3740
import static org.junit.Assert.assertEquals;
41+
import static org.junit.Assert.assertFalse;
42+
import static org.junit.Assert.assertNull;
43+
import static org.junit.Assert.assertTrue;
3844
import static org.mockito.ArgumentMatchers.anyLong;
3945
import static org.mockito.ArgumentMatchers.anyString;
46+
import static org.mockito.Mockito.doReturn;
4047
import static org.mockito.Mockito.mock;
4148
import static org.mockito.Mockito.never;
4249
import static org.mockito.Mockito.verify;
4350
import static org.mockito.Mockito.when;
4451

52+
@RunWith(MockitoJUnitRunner.class)
4553
public class OAuth2UserAuthenticatorTest {
4654

4755
@Mock
@@ -54,20 +62,24 @@ public class OAuth2UserAuthenticatorTest {
5462
private OAuth2AuthManager userOAuth2mgr;
5563

5664
@InjectMocks
65+
@Spy
5766
private OAuth2UserAuthenticator authenticator;
67+
private AutoCloseable closeable;
5868

5969
private AutoCloseable closeable;
6070

6171
@Before
6272
public void setUp() {
6373
closeable = MockitoAnnotations.openMocks(this);
74+
doReturn(true).when(authenticator).isOAuthPluginEnabled();
6475
}
6576

6677
@After
6778
public void tearDown() throws Exception {
6879
closeable.close();
6980
}
7081

82+
7183
@Test
7284
public void testAuthenticateWithValidCredentials() {
7385
String username = "testuser";
@@ -92,13 +104,13 @@ public void testAuthenticateWithValidCredentials() {
92104

93105
Pair<Boolean, OAuth2UserAuthenticator.ActionOnFailedAuthentication> result = authenticator.authenticate(username, null, domainId, requestParameters);
94106

107+
assertTrue(result.first());
108+
assertNull(result.second());
109+
95110
verify(userAccountDao).getUserAccount(username, domainId);
96111
verify(userDao).getUser(userAccount.getId());
97112
verify(userOAuth2mgr).getUserOAuth2AuthenticationProvider(provider[0]);
98113
verify(userOAuth2Authenticator).verifyUser(email[0], secretCode[0]);
99-
100-
assertEquals(true, result.first().booleanValue());
101-
assertEquals(null, result.second());
102114
}
103115

104116
@Test
@@ -114,7 +126,7 @@ public void testAuthenticateWithInvalidCredentials() {
114126
UserOAuth2Authenticator userOAuth2Authenticator = mock(UserOAuth2Authenticator.class);
115127

116128
when(userAccountDao.getUserAccount(username, domainId)).thenReturn(userAccount);
117-
when(userDao.getUser(userAccount.getId())).thenReturn( user);
129+
when(userDao.getUser(userAccount.getId())).thenReturn(user);
118130
when(userOAuth2mgr.getUserOAuth2AuthenticationProvider(provider[0])).thenReturn(userOAuth2Authenticator);
119131
when(userOAuth2Authenticator.verifyUser(email[0], secretCode[0])).thenReturn(false);
120132

@@ -125,13 +137,13 @@ public void testAuthenticateWithInvalidCredentials() {
125137

126138
Pair<Boolean, OAuth2UserAuthenticator.ActionOnFailedAuthentication> result = authenticator.authenticate(username, null, domainId, requestParameters);
127139

140+
assertFalse(result.first());
141+
assertEquals(OAuth2UserAuthenticator.ActionOnFailedAuthentication.INCREMENT_INCORRECT_LOGIN_ATTEMPT_COUNT, result.second());
142+
128143
verify(userAccountDao).getUserAccount(username, domainId);
129144
verify(userDao).getUser(userAccount.getId());
130145
verify(userOAuth2mgr).getUserOAuth2AuthenticationProvider(provider[0]);
131146
verify(userOAuth2Authenticator).verifyUser(email[0], secretCode[0]);
132-
133-
assertEquals(false, result.first().booleanValue());
134-
assertEquals(OAuth2UserAuthenticator.ActionOnFailedAuthentication.INCREMENT_INCORRECT_LOGIN_ATTEMPT_COUNT, result.second());
135147
}
136148

137149
@Test
@@ -151,11 +163,11 @@ public void testAuthenticateWithInvalidUserAccount() {
151163

152164
Pair<Boolean, OAuth2UserAuthenticator.ActionOnFailedAuthentication> result = authenticator.authenticate(username, null, domainId, requestParameters);
153165

166+
assertFalse(result.first());
167+
assertNull(result.second());
168+
154169
verify(userAccountDao).getUserAccount(username, domainId);
155170
verify(userDao, never()).getUser(anyLong());
156171
verify(userOAuth2mgr, never()).getUserOAuth2AuthenticationProvider(anyString());
157-
158-
assertEquals(false, result.first().booleanValue());
159-
assertEquals(null, result.second());
160172
}
161173
}

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
335335

336336
private List<SecurityChecker> _securityCheckers;
337337
private int _cleanupInterval;
338+
private static final String OAUTH2_PROVIDER_NAME = "oauth2";
338339
private List<String> apiNameList;
339340
private static final String OAUTH2_PROVIDER_NAME = "oauth2";
340341

0 commit comments

Comments
 (0)