You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ha...@apache.org on 2022/10/26 20:22:24 UTC
[cloudstack] 08/17: Added new command to list providers
This is an automated email from the ASF dual-hosted git repository.
harikrishna pushed a commit to branch 2FA
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
commit cb38633ccdcfb65e1e585f99bb85cfeda9325d39
Author: Harikrishna Patnala <ha...@gmail.com>
AuthorDate: Fri Oct 14 09:35:53 2022 +0530
Added new command to list providers
---
...UserTwoFactorAuthenticatorProviderResponse.java | 52 ++++++++++++
.../auth/UserTwoFactorAuthenticator.java | 16 ++++
.../auth/GoogleUserTwoFactorAuthenticator.java | 10 +++
.../auth/StaticPinUserTwoFactorAuthenticator.java | 10 +++
server/src/main/java/com/cloud/api/ApiServlet.java | 2 +
.../api/auth/APIAuthenticationManagerImpl.java | 8 ++
...ListUserTwoFactorAuthenticatorProvidersCmd.java | 96 ++++++++++++++++++++++
.../main/java/com/cloud/user/AccountManager.java | 4 +-
8 files changed, 196 insertions(+), 2 deletions(-)
diff --git a/api/src/main/java/org/apache/cloudstack/api/response/UserTwoFactorAuthenticatorProviderResponse.java b/api/src/main/java/org/apache/cloudstack/api/response/UserTwoFactorAuthenticatorProviderResponse.java
new file mode 100644
index 00000000000..4101dc375ed
--- /dev/null
+++ b/api/src/main/java/org/apache/cloudstack/api/response/UserTwoFactorAuthenticatorProviderResponse.java
@@ -0,0 +1,52 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.response;
+
+import com.cloud.serializer.Param;
+import com.google.gson.annotations.SerializedName;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseResponse;
+import org.apache.cloudstack.api.EntityReference;
+import org.apache.cloudstack.auth.UserTwoFactorAuthenticator;
+
+@EntityReference(UserTwoFactorAuthenticator.class)
+public class UserTwoFactorAuthenticatorProviderResponse extends BaseResponse {
+
+ @SerializedName(ApiConstants.NAME)
+ @Param(description = "the user two factor authenticator provider name")
+ private String name;
+
+ @SerializedName(ApiConstants.DESCRIPTION)
+ @Param(description = "the description of the user two factor authenticator provider")
+ private String description;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+}
diff --git a/api/src/main/java/org/apache/cloudstack/auth/UserTwoFactorAuthenticator.java b/api/src/main/java/org/apache/cloudstack/auth/UserTwoFactorAuthenticator.java
index f7257a0d18e..88af239202a 100644
--- a/api/src/main/java/org/apache/cloudstack/auth/UserTwoFactorAuthenticator.java
+++ b/api/src/main/java/org/apache/cloudstack/auth/UserTwoFactorAuthenticator.java
@@ -22,6 +22,22 @@ import com.cloud.utils.component.Adapter;
public interface UserTwoFactorAuthenticator extends Adapter {
+ /**
+ * Returns the unique name of the provider
+ * @return returns provider name
+ */
+ String getName();
+
+ /**
+ * Returns description about the user 2FA provider plugin
+ * @return returns description
+ */
+ String getDescription();
+
+ /**
+ * verifies the 2FA code provided by user
+ * @return returns description
+ */
void check2FA(String code, UserAccount userAccount) throws CloudAuthenticationException;
}
diff --git a/plugins/user-two-factor-authenticators/google/src/main/java/org/apache/cloudstack/auth/GoogleUserTwoFactorAuthenticator.java b/plugins/user-two-factor-authenticators/google/src/main/java/org/apache/cloudstack/auth/GoogleUserTwoFactorAuthenticator.java
index 8dec2a862ea..c285ed60271 100644
--- a/plugins/user-two-factor-authenticators/google/src/main/java/org/apache/cloudstack/auth/GoogleUserTwoFactorAuthenticator.java
+++ b/plugins/user-two-factor-authenticators/google/src/main/java/org/apache/cloudstack/auth/GoogleUserTwoFactorAuthenticator.java
@@ -39,6 +39,16 @@ public class GoogleUserTwoFactorAuthenticator extends AdapterBase implements Use
@Inject
private UserAccountDao _userAccountDao;
+ @Override
+ public String getName() {
+ return "google";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Google user two factor authentication provider Plugin";
+ }
+
@Override
public void check2FA(String code, UserAccount userAccount) throws CloudAuthenticationException {
String expectedCode = get2FACode(get2FAKey(userAccount));
diff --git a/plugins/user-two-factor-authenticators/static-pin/src/main/java/org/apache/cloudstack/auth/StaticPinUserTwoFactorAuthenticator.java b/plugins/user-two-factor-authenticators/static-pin/src/main/java/org/apache/cloudstack/auth/StaticPinUserTwoFactorAuthenticator.java
index 9e80f8aab1f..a45c22c7732 100644
--- a/plugins/user-two-factor-authenticators/static-pin/src/main/java/org/apache/cloudstack/auth/StaticPinUserTwoFactorAuthenticator.java
+++ b/plugins/user-two-factor-authenticators/static-pin/src/main/java/org/apache/cloudstack/auth/StaticPinUserTwoFactorAuthenticator.java
@@ -30,6 +30,16 @@ public class StaticPinUserTwoFactorAuthenticator extends AdapterBase implements
@Inject
private UserAccountDao _userAccountDao;
+ @Override
+ public String getName() {
+ return "staticpin";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Static Pin user two factor authentication provider Plugin";
+ }
+
@Override
public void check2FA(String code, UserAccount userAccount) throws CloudAuthenticationException {
}
diff --git a/server/src/main/java/com/cloud/api/ApiServlet.java b/server/src/main/java/com/cloud/api/ApiServlet.java
index 2062e220c7a..262f0289433 100644
--- a/server/src/main/java/com/cloud/api/ApiServlet.java
+++ b/server/src/main/java/com/cloud/api/ApiServlet.java
@@ -315,6 +315,8 @@ public class ApiServlet extends HttpServlet {
responseType);
HttpUtils.writeHttpResponse(resp, serializedResponse, HttpServletResponse.SC_UNAUTHORIZED, responseType, ApiServer.JSONcontentType.value());
+ } else {
+
}
}
}
diff --git a/server/src/main/java/com/cloud/api/auth/APIAuthenticationManagerImpl.java b/server/src/main/java/com/cloud/api/auth/APIAuthenticationManagerImpl.java
index c304a10d917..600d2a1c057 100644
--- a/server/src/main/java/com/cloud/api/auth/APIAuthenticationManagerImpl.java
+++ b/server/src/main/java/com/cloud/api/auth/APIAuthenticationManagerImpl.java
@@ -32,6 +32,8 @@ import org.apache.cloudstack.api.auth.PluggableAPIAuthenticator;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.component.ManagerBase;
+import static com.cloud.user.AccountManager.enable2FA;
+
@SuppressWarnings("unchecked")
public class APIAuthenticationManagerImpl extends ManagerBase implements APIAuthenticationManager {
public static final Logger s_logger = Logger.getLogger(APIAuthenticationManagerImpl.class.getName());
@@ -77,6 +79,12 @@ public class APIAuthenticationManagerImpl extends ManagerBase implements APIAuth
List<Class<?>> cmdList = new ArrayList<Class<?>>();
cmdList.add(DefaultLoginAPIAuthenticatorCmd.class);
cmdList.add(DefaultLogoutAPIAuthenticatorCmd.class);
+
+ if(enable2FA.value()) {
+ cmdList.add(ListUserTwoFactorAuthenticatorProvidersCmd.class);
+ cmdList.add(TwoFactorAuthenticationCmd.class);
+ }
+
for (PluggableAPIAuthenticator apiAuthenticator: _apiAuthenticators) {
List<Class<?>> commands = apiAuthenticator.getAuthCommands();
if (commands != null) {
diff --git a/server/src/main/java/com/cloud/api/auth/ListUserTwoFactorAuthenticatorProvidersCmd.java b/server/src/main/java/com/cloud/api/auth/ListUserTwoFactorAuthenticatorProvidersCmd.java
new file mode 100644
index 00000000000..aa82061ac16
--- /dev/null
+++ b/server/src/main/java/com/cloud/api/auth/ListUserTwoFactorAuthenticatorProvidersCmd.java
@@ -0,0 +1,96 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.api.auth;
+
+import com.cloud.user.Account;
+import com.cloud.user.AccountManager;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.UserTwoFactorAuthenticatorProviderResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.auth.UserTwoFactorAuthenticator;
+
+import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.List;
+
+@APICommand(name = ListUserTwoFactorAuthenticatorProvidersCmd.APINAME,
+ description = "Lists user two factor authenticator providers",
+ responseObject = UserTwoFactorAuthenticatorProviderResponse.class, since = "4.18.0")
+public class ListUserTwoFactorAuthenticatorProvidersCmd extends BaseCmd {
+
+ public static final String APINAME = "listUserTwoFactorAuthenticatorProviders";
+
+ @Inject
+ private AccountManager accountManager;
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name = ApiConstants.NAME, type = BaseCmd.CommandType.STRING, description = "List user two factor authenticator provider by name")
+ private String name;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public String getCommandName() {
+ return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ return Account.ACCOUNT_ID_SYSTEM;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ private void setupResponse(final List<UserTwoFactorAuthenticator> providers) {
+ final ListResponse<UserTwoFactorAuthenticatorProviderResponse> response = new ListResponse<>();
+ final List<UserTwoFactorAuthenticatorProviderResponse> responses = new ArrayList<>();
+ for (final UserTwoFactorAuthenticator provider : providers) {
+ if (provider == null || (getName() != null && !provider.getName().equals(getName()))) {
+ continue;
+ }
+ final UserTwoFactorAuthenticatorProviderResponse UserTwoFactorAuthenticatorProviderResponse = new UserTwoFactorAuthenticatorProviderResponse();
+ UserTwoFactorAuthenticatorProviderResponse.setName(provider.getName());
+ UserTwoFactorAuthenticatorProviderResponse.setDescription(provider.getDescription());
+ UserTwoFactorAuthenticatorProviderResponse.setObjectName("providers");
+ responses.add(UserTwoFactorAuthenticatorProviderResponse);
+ }
+ response.setResponses(responses);
+ response.setResponseName(getCommandName());
+ setResponseObject(response);
+ }
+
+ @Override
+ public void execute() {
+ List<UserTwoFactorAuthenticator> providers = accountManager.listUserTwoFactorAuthenticationProviders();
+ setupResponse(providers);
+ }
+
+}
\ No newline at end of file
diff --git a/server/src/main/java/com/cloud/user/AccountManager.java b/server/src/main/java/com/cloud/user/AccountManager.java
index 0e65a0caa78..5f22e7729bc 100644
--- a/server/src/main/java/com/cloud/user/AccountManager.java
+++ b/server/src/main/java/com/cloud/user/AccountManager.java
@@ -191,7 +191,7 @@ public interface AccountManager extends AccountService, Configurable {
ConfigKey<Boolean> enable2FA = new ConfigKey<Boolean>("Advanced",
Boolean.class,
"enable.two.factor.authentication",
- "true",
+ "false",
"Determines whether two factor authentication is enabled or not. This can be done at domain level as well",
true,
ConfigKey.Scope.Domain);
@@ -199,7 +199,7 @@ public interface AccountManager extends AccountService, Configurable {
ConfigKey<String> userTwoFactorAuthenticationProviderPlugin = new ConfigKey<>("Advanced", String.class,
"user.two.factor.authentication.provider.plugin",
"GOOGLE",
- "The user two factor authentication provider plugin. Eg. GOOGLE, STATICPIN", true, ConfigKey.Scope.Domain);
+ "The user two factor authentication provider plugin. Eg. google, staticpin", true, ConfigKey.Scope.Domain);
boolean moveUser(long id, Long domainId, Account newAccount);