Skip to content

Commit 01fb9a8

Browse files
committed
Refactor OAuth plugin domain scope handling to use a centralized method for enabling checks
1 parent b056914 commit 01fb9a8

6 files changed

Lines changed: 23 additions & 26 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//
1919
package org.apache.cloudstack.oauth2;
2020

21+
import com.cloud.domain.Domain;
2122
import com.cloud.utils.component.PluggableService;
2223
import org.apache.cloudstack.api.auth.PluggableAPIAuthenticator;
2324
import org.apache.cloudstack.auth.UserOAuth2Authenticator;
@@ -64,4 +65,19 @@ public interface OAuth2AuthManager extends PluggableAPIAuthenticator, PluggableS
6465
OauthProviderVO updateOauthProvider(UpdateOAuthProviderCmd cmd);
6566

6667
Long resolveDomainId(Map<String, Object[]> params);
68+
69+
/**
70+
* Resolves whether the OAuth plugin is enabled for the given domain scope.
71+
* A null domain or the ROOT domain is treated as the global scope, since the
72+
* ROOT domain has no domain-level override and inherits the global value;
73+
* any other domain is checked strictly at its own domain scope (no inheritance).
74+
* @param domainId domain id, or null for global
75+
* @return true if OAuth is enabled for that scope
76+
*/
77+
static boolean isPluginEnabledForDomain(Long domainId) {
78+
if (domainId == null || domainId == Domain.ROOT_DOMAIN) {
79+
return Boolean.TRUE.equals(OAuth2IsPluginEnabled.value());
80+
}
81+
return Boolean.TRUE.equals(OAuth2IsPluginEnabled.valueInScope(ConfigKey.Scope.Domain, domainId, true));
82+
}
6783
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ public boolean start() {
7979
}
8080

8181
protected boolean isOAuthPluginEnabled(Long domainId) {
82-
if (domainId == null) {
83-
return Boolean.TRUE.equals(OAuth2IsPluginEnabled.value());
84-
}
85-
return Boolean.TRUE.equals(OAuth2IsPluginEnabled.valueInScope(ConfigKey.Scope.Domain, domainId, true));
82+
return OAuth2AuthManager.isPluginEnabledForDomain(domainId);
8683
}
8784

8885
@Override

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@
2727
import org.apache.cloudstack.api.ApiConstants;
2828
import org.apache.cloudstack.auth.UserAuthenticator;
2929
import org.apache.cloudstack.auth.UserOAuth2Authenticator;
30-
import org.apache.cloudstack.framework.config.ConfigKey;
3130

3231
import javax.inject.Inject;
3332
import java.util.Map;
3433
import java.util.Objects;
3534

36-
import static org.apache.cloudstack.oauth2.OAuth2AuthManager.OAuth2IsPluginEnabled;
37-
3835
public class OAuth2UserAuthenticator extends AdapterBase implements UserAuthenticator {
3936

4037
@Inject
@@ -92,9 +89,6 @@ public String encode(String password) {
9289
}
9390

9491
protected boolean isOAuthPluginEnabled(Long domainId) {
95-
if (domainId == null) {
96-
return Boolean.TRUE.equals(OAuth2IsPluginEnabled.value());
97-
}
98-
return Boolean.TRUE.equals(OAuth2IsPluginEnabled.valueInScope(ConfigKey.Scope.Domain, domainId, true));
92+
return OAuth2AuthManager.isPluginEnabledForDomain(domainId);
9993
}
10094
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.apache.cloudstack.api.response.DomainResponse;
3939
import org.apache.cloudstack.api.response.ListResponse;
4040
import org.apache.cloudstack.auth.UserOAuth2Authenticator;
41-
import org.apache.cloudstack.framework.config.ConfigKey;
4241
import org.apache.cloudstack.oauth2.OAuth2AuthManager;
4342
import org.apache.cloudstack.oauth2.api.response.OauthProviderResponse;
4443
import org.apache.cloudstack.oauth2.vo.OauthProviderVO;
@@ -145,9 +144,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
145144
Domain domain = result.getDomainId() != null ? ApiDBUtils.findDomainById(result.getDomainId()) : null;
146145
OauthProviderResponse r = new OauthProviderResponse(result.getUuid(), result.getProvider(),
147146
result.getDescription(), result.getClientId(), result.getSecretKey(), result.getRedirectUri(), domain);
148-
boolean oauthEnabled = result.getDomainId() == null
149-
? Boolean.TRUE.equals(OAuth2AuthManager.OAuth2IsPluginEnabled.value())
150-
: Boolean.TRUE.equals(OAuth2AuthManager.OAuth2IsPluginEnabled.valueInScope(ConfigKey.Scope.Domain, result.getDomainId(), true));
147+
boolean oauthEnabled = OAuth2AuthManager.isPluginEnabledForDomain(result.getDomainId());
151148
if (oauthEnabled && authenticatorPluginNames.contains(result.getProvider()) && result.isEnabled()) {
152149
r.setEnabled(true);
153150
} else {
@@ -162,7 +159,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
162159
List<OauthProviderVO> allProviders = _oauth2mgr.listOauthProviders(null, null, null);
163160
for (OauthProviderVO domainProvider : allProviders) {
164161
if (domainProvider.getDomainId() != null && domainProvider.isEnabled()
165-
&& Boolean.TRUE.equals(OAuth2AuthManager.OAuth2IsPluginEnabled.valueInScope(ConfigKey.Scope.Domain, domainProvider.getDomainId(), true))
162+
&& OAuth2AuthManager.isPluginEnabledForDomain(domainProvider.getDomainId())
166163
&& authenticatorPluginNames.contains(domainProvider.getProvider())) {
167164
totalEnabledCount++;
168165
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@
5050
import java.util.Map;
5151
import java.net.InetAddress;
5252

53-
import org.apache.cloudstack.framework.config.ConfigKey;
54-
55-
import static org.apache.cloudstack.oauth2.OAuth2AuthManager.OAuth2IsPluginEnabled;
53+
import org.apache.cloudstack.oauth2.OAuth2AuthManager;
5654

5755
@APICommand(name = "oauthlogin", description = "Logs a user into the CloudStack after successful verification of OAuth secret code from the particular provider." +
5856
"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.",
@@ -146,9 +144,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
146144
domainId = userDomain.getId();
147145
}
148146

149-
boolean oauthEnabled = domainId == null
150-
? Boolean.TRUE.equals(OAuth2IsPluginEnabled.value())
151-
: Boolean.TRUE.equals(OAuth2IsPluginEnabled.valueInScope(ConfigKey.Scope.Domain, domainId, true));
147+
boolean oauthEnabled = OAuth2AuthManager.isPluginEnabledForDomain(domainId);
152148
if (!oauthEnabled) {
153149
logger.debug(String.format("OAuth is not enabled %s, users cannot login using OAuth", domainId == null ? "globally" : "in domain " + domainId));
154150
throw new CloudAuthenticationException(String.format(

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.cloud.domain.Domain;
2121
import org.apache.cloudstack.api.ApiCommandResourceType;
2222
import org.apache.cloudstack.auth.UserOAuth2Authenticator;
23-
import org.apache.cloudstack.framework.config.ConfigKey;
2423
import org.apache.cloudstack.oauth2.OAuth2AuthManager;
2524
import org.apache.cloudstack.oauth2.api.response.OauthProviderResponse;
2625
import org.apache.cloudstack.oauth2.vo.OauthProviderVO;
@@ -144,9 +143,7 @@ public void execute() {
144143
String name = authenticator.getName();
145144
authenticatorPluginNames.add(name);
146145
}
147-
boolean oauthEnabled = result.getDomainId() == null
148-
? Boolean.TRUE.equals(OAuth2AuthManager.OAuth2IsPluginEnabled.value())
149-
: Boolean.TRUE.equals(OAuth2AuthManager.OAuth2IsPluginEnabled.valueInScope(ConfigKey.Scope.Domain, result.getDomainId(), true));
146+
boolean oauthEnabled = OAuth2AuthManager.isPluginEnabledForDomain(result.getDomainId());
150147
if (oauthEnabled && authenticatorPluginNames.contains(result.getProvider()) && result.isEnabled()) {
151148
r.setEnabled(true);
152149
} else {

0 commit comments

Comments
 (0)