Skip to content

Commit 75c59a3

Browse files
committed
allow oauth login to skip force password change
1 parent 993e211 commit 75c59a3

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

engine/schema/src/main/java/org/apache/cloudstack/resourcedetail/UserDetailVO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class UserDetailVO implements ResourceDetail {
4949
public static final String PasswordResetToken = "PasswordResetToken";
5050
public static final String PasswordResetTokenExpiryDate = "PasswordResetTokenExpiryDate";
5151
public static final String PasswordChangeRequired = "PasswordChangeRequired";
52+
public static final String OauthLogin = "OauthLogin";
5253

5354
public UserDetailVO() {
5455
}

plugins/api/discovery/src/main/java/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
294294
// Limit APIs on first login requiring password change
295295
UserAccount userAccount = accountService.getUserAccountById(user.getId());
296296
Map<String, String> userAccDetails = userAccount.getDetails();
297-
if (MapUtils.isNotEmpty(userAccDetails) && "true".equalsIgnoreCase(userAccDetails.get(UserDetailVO.PasswordChangeRequired))) {
297+
if (MapUtils.isNotEmpty(userAccDetails) && !userAccDetails.containsKey(UserDetailVO.OauthLogin) &&
298+
"true".equalsIgnoreCase(userAccDetails.get(UserDetailVO.PasswordChangeRequired))) {
298299
apisAllowed = APIS_ALLOWED_FOR_PASSWORD_CHANGE;
299300
} else {
300301
if (role.getRoleType() == RoleType.Admin && role.getId() == RoleType.Admin.getId()) {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.cloudstack.api.auth.APIAuthenticator;
3535
import org.apache.cloudstack.api.auth.PluggableAPIAuthenticator;
3636
import org.apache.cloudstack.api.response.LoginCmdResponse;
37+
import org.apache.cloudstack.resourcedetail.UserDetailVO;
38+
import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao;
3739
import org.apache.commons.collections.CollectionUtils;
3840
import org.apache.commons.lang3.StringUtils;
3941
import org.jetbrains.annotations.Nullable;
@@ -74,6 +76,9 @@ public class OauthLoginAPIAuthenticatorCmd extends BaseCmd implements APIAuthent
7476
@Inject
7577
ApiServerService _apiServer;
7678

79+
@Inject
80+
UserDetailsDao userDetailsDao;
81+
7782
/////////////////////////////////////////////////////
7883
/////////////////// Accessors ///////////////////////
7984
/////////////////////////////////////////////////////
@@ -157,8 +162,10 @@ private String doOauthAuthentication(HttpSession session, Long domainId, String
157162
if (userAccount != null && User.Source.SAML2 == userAccount.getSource()) {
158163
throw new CloudAuthenticationException("User is not allowed CloudStack login");
159164
}
160-
return ApiResponseSerializer.toSerializedString(_apiServer.loginUser(session, userAccount.getUsername(), null, domainId, domain, remoteAddress, params),
165+
serializedResponse = ApiResponseSerializer.toSerializedString(_apiServer.loginUser(session, userAccount.getUsername(), null, domainId, domain, remoteAddress, params),
161166
responseType);
167+
userDetailsDao.addDetail(userAccount.getId(), UserDetailVO.OauthLogin, "true", false);
168+
return serializedResponse;
162169
} catch (final CloudAuthenticationException ex) {
163170
ApiServlet.invalidateHttpSession(session, "fall through to API key,");
164171
String msg = String.format("%s", ex.getMessage() != null ?

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.cloudstack.api.auth.APIAuthenticator;
3535
import org.apache.cloudstack.api.auth.PluggableAPIAuthenticator;
3636
import org.apache.cloudstack.api.response.LoginCmdResponse;
37+
import org.apache.cloudstack.resourcedetail.UserDetailVO;
38+
import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao;
3739
import org.jetbrains.annotations.Nullable;
3840

3941
import javax.inject.Inject;
@@ -66,6 +68,9 @@ public class DefaultLoginAPIAuthenticatorCmd extends BaseCmd implements APIAuthe
6668
@Inject
6769
ApiServerService _apiServer;
6870

71+
@Inject
72+
UserDetailsDao userDetailsDao;
73+
6974
/////////////////////////////////////////////////////
7075
/////////////////// Accessors ///////////////////////
7176
/////////////////////////////////////////////////////
@@ -151,8 +156,10 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
151156
if (userAccount != null && User.Source.SAML2 == userAccount.getSource()) {
152157
throw new CloudAuthenticationException("User is not allowed CloudStack login");
153158
}
154-
return ApiResponseSerializer.toSerializedString(_apiServer.loginUser(session, username[0], pwd, domainId, domain, remoteAddress, params),
159+
serializedResponse = ApiResponseSerializer.toSerializedString(_apiServer.loginUser(session, username[0], pwd, domainId, domain, remoteAddress, params),
155160
responseType);
161+
userDetailsDao.removeDetail(userAccount.getId(), UserDetailVO.OauthLogin);
162+
return serializedResponse;
156163
} catch (final CloudAuthenticationException ex) {
157164
ApiServlet.invalidateHttpSession(session, "fall through to API key,");
158165
// TODO: fall through to API key, or just fail here w/ auth error? (HTTP 401)

0 commit comments

Comments
 (0)