Skip to content

Commit 8c12a13

Browse files
Fix NPE during reset password (#12585)
1 parent ae5308b commit 8c12a13

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,8 @@ private String doOauthAuthentication(HttpSession session, Long domainId, String
177177

178178
protected Long getDomainIdFromParams(Map<String, Object[]> params, StringBuilder auditTrailSb, String responseType) {
179179
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
180-
181-
if (domainIdArr == null) {
182-
domainIdArr = (String[])params.get(ApiConstants.DOMAIN__ID);
183-
}
184180
Long domainId = null;
185-
if ((domainIdArr != null) && (domainIdArr.length > 0)) {
181+
if (domainIdArr != null && domainIdArr.length > 0) {
186182
try {
187183
//check if UUID is passed in for domain
188184
domainId = _apiServer.fetchDomainId(domainIdArr[0]);

plugins/user-authenticators/saml2/src/main/java/org/apache/cloudstack/api/command/SAML2LoginAPIAuthenticatorCmd.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,17 @@ public String authenticate(final String command, final Map<String, Object[]> par
158158
String domainPath = null;
159159

160160
if (params.containsKey(ApiConstants.IDP_ID)) {
161-
idpId = ((String[])params.get(ApiConstants.IDP_ID))[0];
161+
String[] idpIds = (String[])params.get(ApiConstants.IDP_ID);
162+
if (idpIds != null && idpIds.length > 0) {
163+
idpId = idpIds[0];
164+
}
162165
}
163166

164167
if (params.containsKey(ApiConstants.DOMAIN)) {
165-
domainPath = ((String[])params.get(ApiConstants.DOMAIN))[0];
168+
String[] domainPaths = (String[])params.get(ApiConstants.DOMAIN);
169+
if (domainPaths != null && domainPaths.length > 0) {
170+
domainPath = domainPaths[0];
171+
}
166172
}
167173

168174
if (domainPath != null && !domainPath.isEmpty()) {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.servlet.http.HttpServletResponse;
3535
import javax.servlet.http.HttpSession;
3636

37+
import com.cloud.api.auth.DefaultForgotPasswordAPIAuthenticatorCmd;
3738
import org.apache.cloudstack.api.ApiConstants;
3839
import org.apache.cloudstack.api.ApiErrorCode;
3940
import org.apache.cloudstack.api.ApiServerService;
@@ -164,7 +165,6 @@ private void checkSingleQueryParameterValue(Map<String, String[]> params) {
164165
LOGGER.warn(message);
165166
}
166167
});
167-
168168
}
169169

170170
void processRequestInContext(final HttpServletRequest req, final HttpServletResponse resp) {
@@ -226,7 +226,6 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
226226
}
227227

228228
if (command != null && !command.equals(ValidateUserTwoFactorAuthenticationCodeCmd.APINAME)) {
229-
230229
APIAuthenticator apiAuthenticator = authManager.getAPIAuthenticator(command);
231230
if (apiAuthenticator != null) {
232231
auditTrailSb.append("command=");
@@ -262,7 +261,9 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
262261
} catch (ServerApiException e) {
263262
httpResponseCode = e.getErrorCode().getHttpCode();
264263
responseString = e.getMessage();
265-
LOGGER.debug("Authentication failure: " + e.getMessage());
264+
if (!DefaultForgotPasswordAPIAuthenticatorCmd.APINAME.equalsIgnoreCase(command) || StringUtils.isNotBlank(username)) {
265+
LOGGER.debug("Authentication failure: {}", e.getMessage());
266+
}
266267
}
267268

268269
if (apiAuthenticator.getAPIType() == APIAuthenticationType.LOGOUT_API) {
@@ -330,7 +331,7 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
330331
}
331332
}
332333

333-
if (! requestChecksoutAsSane(resp, auditTrailSb, responseType, params, session, command, userId, account, accountObj))
334+
if (!requestChecksoutAsSane(resp, auditTrailSb, responseType, params, session, command, userId, account, accountObj))
334335
return;
335336
} else {
336337
CallContext.register(accountMgr.getSystemUser(), accountMgr.getSystemAccount());
@@ -360,7 +361,6 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
360361
apiServer.getSerializedApiError(HttpServletResponse.SC_UNAUTHORIZED, "unable to verify user credentials and/or request signature", params,
361362
responseType);
362363
HttpUtils.writeHttpResponse(resp, serializedResponse, HttpServletResponse.SC_UNAUTHORIZED, responseType, ApiServer.JSONcontentType.value());
363-
364364
}
365365
} catch (final ServerApiException se) {
366366
final String serializedResponseText = apiServer.getSerializedApiError(se, params, responseType);
@@ -550,6 +550,9 @@ public static void invalidateHttpSession(HttpSession session, String msg) {
550550
if (LOGGER.isTraceEnabled()) {
551551
LOGGER.trace(msg);
552552
}
553+
if (session == null) {
554+
return;
555+
}
553556
session.invalidate();
554557
} catch (final IllegalStateException ise) {
555558
if (LOGGER.isTraceEnabled()) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
import java.util.List;
4545
import java.util.Map;
4646

47-
@APICommand(name = "forgotPassword",
47+
@APICommand(name = DefaultForgotPasswordAPIAuthenticatorCmd.APINAME,
4848
description = "Sends an email to the user with a token to reset the password using resetPassword command.",
4949
since = "4.20.0.0",
5050
requestHasSensitiveInfo = true,
5151
responseObject = SuccessResponse.class)
5252
public class DefaultForgotPasswordAPIAuthenticatorCmd extends BaseCmd implements APIAuthenticator {
53-
53+
public static final String APINAME = "forgotPassword";
5454

5555
/////////////////////////////////////////////////////
5656
//////////////// API parameters /////////////////////
@@ -108,10 +108,12 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
108108
if (userDomain != null) {
109109
domainId = userDomain.getId();
110110
} else {
111+
logger.debug("Unable to find the domain from the path {}", domain);
111112
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Unable to find the domain from the path %s", domain));
112113
}
113114
final UserAccount userAccount = _accountService.getActiveUserAccount(username[0], domainId);
114115
if (userAccount != null && List.of(User.Source.SAML2, User.Source.OAUTH2, User.Source.LDAP).contains(userAccount.getSource())) {
116+
logger.debug("Forgot Password is not allowed for the user {} from source {}", username[0], userAccount.getSource());
115117
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Forgot Password is not allowed for this user");
116118
}
117119
boolean success = _apiServer.forgotPassword(userAccount, userDomain);

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
@APICommand(name = "login", description = "Logs a user into the CloudStack. A successful login attempt will generate a JSESSIONID cookie value that can be passed in subsequent Query command calls until the \"logout\" command has been issued or the session has expired.", requestHasSensitiveInfo = true, responseObject = LoginCmdResponse.class, entityType = {})
4848
public class DefaultLoginAPIAuthenticatorCmd extends BaseCmd implements APIAuthenticator {
4949

50-
5150
/////////////////////////////////////////////////////
5251
//////////////// API parameters /////////////////////
5352
/////////////////////////////////////////////////////
@@ -107,17 +106,13 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
107106
if (HTTPMethod.valueOf(req.getMethod()) != HTTPMethod.POST) {
108107
throw new ServerApiException(ApiErrorCode.METHOD_NOT_ALLOWED, "Please use HTTP POST to authenticate using this API");
109108
}
109+
110110
// FIXME: ported from ApiServlet, refactor and cleanup
111111
final String[] username = (String[])params.get(ApiConstants.USERNAME);
112112
final String[] password = (String[])params.get(ApiConstants.PASSWORD);
113-
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
114-
115-
if (domainIdArr == null) {
116-
domainIdArr = (String[])params.get(ApiConstants.DOMAIN__ID);
117-
}
118-
final String[] domainName = (String[])params.get(ApiConstants.DOMAIN);
113+
final String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
119114
Long domainId = null;
120-
if ((domainIdArr != null) && (domainIdArr.length > 0)) {
115+
if (domainIdArr != null && domainIdArr.length > 0) {
121116
try {
122117
//check if UUID is passed in for domain
123118
domainId = _apiServer.fetchDomainId(domainIdArr[0]);
@@ -135,6 +130,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
135130
}
136131

137132
String domain = null;
133+
final String[] domainName = (String[])params.get(ApiConstants.DOMAIN);
138134
domain = getDomainName(auditTrailSb, domainName, domain);
139135

140136
String serializedResponse = null;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
responseObject = SuccessResponse.class)
5454
public class DefaultResetPasswordAPIAuthenticatorCmd extends BaseCmd implements APIAuthenticator {
5555

56-
5756
/////////////////////////////////////////////////////
5857
//////////////// API parameters /////////////////////
5958
/////////////////////////////////////////////////////

0 commit comments

Comments
 (0)