Skip to content

Commit de60cd1

Browse files
refactor and unit test
1 parent 9220ffc commit de60cd1

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiServerService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ public ResponseObject loginUser(HttpSession session, String username, String pas
4848
boolean forgotPassword(UserAccount userAccount, Domain domain);
4949

5050
boolean resetPassword(UserAccount userAccount, String token, String password);
51+
52+
String getDomainId(Map<String, Object[]> params);
5153
}

plugins/user-authenticators/oauth2/src/main/java/org/apache/cloudstack/oauth2/api/command/OauthLoginAPIAuthenticatorCmd.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,14 @@ private String doOauthAuthentication(HttpSession session, Long domainId, String
176176
}
177177

178178
protected Long getDomainIdFromParams(Map<String, Object[]> params, StringBuilder auditTrailSb, String responseType) {
179-
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
180-
if (domainIdArr == null) {
181-
// Fallback to support clients using the camelCase parameter name "domainId"
182-
domainIdArr = (String[])params.get(ApiConstants.DOMAIN__ID);
183-
}
179+
String domainIdStr = _apiServer.getDomainId(params);
184180
Long domainId = null;
185-
if (domainIdArr != null && domainIdArr.length > 0) {
181+
if (StringUtils.isNotEmpty(domainIdStr)) {
186182
try {
187183
//check if UUID is passed in for domain
188-
domainId = _apiServer.fetchDomainId(domainIdArr[0]);
184+
domainId = _apiServer.fetchDomainId(domainIdStr);
189185
if (domainId == null) {
190-
domainId = Long.parseLong(domainIdArr[0]);
186+
domainId = Long.parseLong(domainIdStr);
191187
}
192188
auditTrailSb.append(" domainid=" + domainId);// building the params for POST call
193189
} catch (final NumberFormatException e) {

plugins/user-authenticators/oauth2/src/test/java/org/apache/cloudstack/oauth2/api/command/OauthLoginAPIAuthenticatorCmdTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,29 @@ public void testGetDomainIdFromParams() {
8585
ApiServer apiServer = mock(ApiServer.class);
8686
cmd._apiServer = apiServer;
8787
when(apiServer.fetchDomainId("1234")).thenReturn(5678L);
88+
when(apiServer.getDomainId(params)).thenCallRealMethod();
8889

8990
Long domainId = cmd.getDomainIdFromParams(params, auditTrailSb, responseType);
9091

9192
assertEquals(Long.valueOf(5678), domainId);
9293
assertEquals(" domainid=5678", auditTrailSb.toString());
9394
}
95+
96+
@Test
97+
public void testGetDomainIdFromCamelCaseParam() {
98+
StringBuilder auditTrailSb = new StringBuilder();
99+
String responseType = "json";
100+
Map<String, Object[]> params = new HashMap<>();
101+
params.put(ApiConstants.DOMAIN_ID, null);
102+
params.put(ApiConstants.DOMAIN__ID, new String[]{"5678"});
103+
ApiServer apiServer = mock(ApiServer.class);
104+
cmd._apiServer = apiServer;
105+
when(apiServer.fetchDomainId("5678")).thenReturn(1234L);
106+
when(apiServer.getDomainId(params)).thenCallRealMethod();
107+
108+
Long domainId = cmd.getDomainIdFromParams(params, auditTrailSb, responseType);
109+
110+
assertEquals(Long.valueOf(1234), domainId);
111+
assertEquals(" domainid=1234", auditTrailSb.toString());
112+
}
94113
}

server/src/main/java/com/cloud/api/ApiServer.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
116116
import org.apache.cloudstack.user.UserPasswordResetManager;
117117
import org.apache.commons.codec.binary.Base64;
118+
import org.apache.commons.collections.MapUtils;
118119
import org.apache.commons.lang3.EnumUtils;
119120
import org.apache.http.ConnectionClosedException;
120121
import org.apache.http.HttpException;
@@ -1354,6 +1355,25 @@ public boolean resetPassword(UserAccount userAccount, String token, String passw
13541355
return userPasswordResetManager.validateAndResetPassword(userAccount, token, password);
13551356
}
13561357

1358+
@Override
1359+
public String getDomainId(Map<String, Object[]> params) {
1360+
if (MapUtils.isEmpty(params)) {
1361+
return null;
1362+
}
1363+
1364+
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
1365+
if (domainIdArr == null) {
1366+
// Fallback to support clients using the camelCase parameter name "domainId"
1367+
domainIdArr = (String[])params.get(ApiConstants.DOMAIN__ID);
1368+
}
1369+
1370+
if (domainIdArr == null || domainIdArr.length == 0) {
1371+
return null;
1372+
}
1373+
1374+
return domainIdArr[0];
1375+
}
1376+
13571377
private void checkCommandAvailable(final User user, final String commandName, final InetAddress remoteAddress) throws PermissionDeniedException {
13581378
if (user == null) {
13591379
throw new PermissionDeniedException("User is null for role based API access check for command" + commandName);

server/src/main/java/com/cloud/api/auth/DefaultLoginAPIAuthenticatorCmd.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.cloud.domain.Domain;
2121
import com.cloud.user.User;
2222
import com.cloud.user.UserAccount;
23+
import com.cloud.utils.StringUtils;
2324
import org.apache.cloudstack.api.ApiServerService;
2425
import com.cloud.api.response.ApiResponseSerializer;
2526
import com.cloud.exception.CloudAuthenticationException;
@@ -110,18 +111,14 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
110111
// FIXME: ported from ApiServlet, refactor and cleanup
111112
final String[] username = (String[])params.get(ApiConstants.USERNAME);
112113
final String[] password = (String[])params.get(ApiConstants.PASSWORD);
113-
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
114-
if (domainIdArr == null) {
115-
// Fallback to support clients using the camelCase parameter name "domainId"
116-
domainIdArr = (String[])params.get(ApiConstants.DOMAIN__ID);
117-
}
114+
String domainIdStr = _apiServer.getDomainId(params);
118115
Long domainId = null;
119-
if (domainIdArr != null && domainIdArr.length > 0) {
116+
if (StringUtils.isNotEmpty(domainIdStr)) {
120117
try {
121118
//check if UUID is passed in for domain
122-
domainId = _apiServer.fetchDomainId(domainIdArr[0]);
119+
domainId = _apiServer.fetchDomainId(domainIdStr);
123120
if (domainId == null) {
124-
domainId = Long.parseLong(domainIdArr[0]);
121+
domainId = Long.parseLong(domainIdStr);
125122
}
126123
auditTrailSb.append(" domainid=" + domainId);// building the params for POST call
127124
} catch (final NumberFormatException e) {

0 commit comments

Comments
 (0)