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/11/03 09:10:16 UTC

[cloudstack] 01/03: UI improvements

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 62c0c3861ad89868483d372db924cb1041e32849
Author: Harikrishna Patnala <ha...@gmail.com>
AuthorDate: Tue Nov 1 12:09:15 2022 +0530

    UI improvements
---
 .../auth/SetupUserTwoFactorAuthenticationCmd.java  |   6 +-
 ...ValidateUserTwoFactorAuthenticationCodeCmd.java |   9 +-
 ui/public/locales/en.json                          |   7 +-
 ui/src/views/dashboard/TwoFa.vue                   |  19 +++-
 ui/src/views/iam/RegisterTwoFactorAuth.vue         | 116 ++++++++++++++++-----
 5 files changed, 118 insertions(+), 39 deletions(-)

diff --git a/server/src/main/java/com/cloud/api/auth/SetupUserTwoFactorAuthenticationCmd.java b/server/src/main/java/com/cloud/api/auth/SetupUserTwoFactorAuthenticationCmd.java
index b5ab837863a..acce2cf3dfd 100644
--- a/server/src/main/java/com/cloud/api/auth/SetupUserTwoFactorAuthenticationCmd.java
+++ b/server/src/main/java/com/cloud/api/auth/SetupUserTwoFactorAuthenticationCmd.java
@@ -57,13 +57,15 @@ public class SetupUserTwoFactorAuthenticationCmd extends BaseCmd {
     }
 
     public Boolean getEnable() {
-        return enable != null? true:enable;
+        return enable == null ? true : enable;
     }
 
     @Override
     public void execute() throws ServerApiException {
         UserTwoFactorAuthenticationSetupResponse response = accountManager.setupUserTwoFactorAuthentication(this);
-        this.setResponseObject(response);
+        response.setObjectName("setup2fa");
+        response.setResponseName(getCommandName());
+        setResponseObject(response);
     }
 
     @Override
diff --git a/server/src/main/java/com/cloud/api/auth/ValidateUserTwoFactorAuthenticationCodeCmd.java b/server/src/main/java/com/cloud/api/auth/ValidateUserTwoFactorAuthenticationCodeCmd.java
index 413dec02e2a..a3405761eac 100644
--- a/server/src/main/java/com/cloud/api/auth/ValidateUserTwoFactorAuthenticationCodeCmd.java
+++ b/server/src/main/java/com/cloud/api/auth/ValidateUserTwoFactorAuthenticationCodeCmd.java
@@ -19,8 +19,8 @@ package com.cloud.api.auth;
 import com.cloud.api.ApiServlet;
 import com.cloud.api.response.ApiResponseSerializer;
 import com.cloud.exception.CloudAuthenticationException;
-import com.cloud.user.Account;
 import com.cloud.user.AccountManager;
+import com.cloud.user.UserAccount;
 import org.apache.cloudstack.api.APICommand;
 import org.apache.cloudstack.api.ApiConstants;
 import org.apache.cloudstack.api.ApiErrorCode;
@@ -96,13 +96,12 @@ public class ValidateUserTwoFactorAuthenticationCodeCmd extends BaseCmd implemen
             throw new ServerApiException(ApiErrorCode.ACCOUNT_ERROR, "Code for two factor authentication is required");
         }
 
-        Account owner = _accountService.getActiveAccountById(getEntityOwnerId());
-        Long domainId = owner.getDomainId();
-        Long accountId = owner.getAccountId();
+        final long currentUserId = (Long) session.getAttribute("userid");
+        final UserAccount currentUserAccount = _accountService.getUserAccountById(currentUserId);
 
         String serializedResponse = null;
         try {
-            accountManager.verifyUsingTwoFactorAuthenticationCode(twoFactorAuthenticationCode, domainId, accountId);
+            accountManager.verifyUsingTwoFactorAuthenticationCode(twoFactorAuthenticationCode, currentUserAccount.getDomainId(), currentUserId);
             SuccessResponse response = new SuccessResponse(getCommandName());
             setResponseObject(response);
             return ApiResponseSerializer.toSerializedString(response, responseType);
diff --git a/ui/public/locales/en.json b/ui/public/locales/en.json
index c118c1f57da..013391290c0 100644
--- a/ui/public/locales/en.json
+++ b/ui/public/locales/en.json
@@ -136,7 +136,7 @@
 "label.action.reboot.systemvm": "Reboot system VM",
 "label.action.recover.volume": "Recover volume",
 "label.action.recurring.snapshot": "Recurring snapshots",
-"label.action.register.2FA.user.auth": "Register user for 2FA authentication",
+"label.action.register.2FA.user.auth": "Register user for Two Factor Authentication",
 "label.action.register.iso": "Register ISO",
 "label.action.register.template": "Register template from URL",
 "label.action.release.ip": "Release IP",
@@ -409,7 +409,6 @@
 "label.compute.offerings": "Compute offerings",
 "label.configuration": "Configuration",
 "label.configure": "Configure",
-"label.configure.app": "Configure the App",
 "label.configure.ldap": "Configure LDAP",
 "label.configure.ovs": "Configure Ovs",
 "label.configure.sticky.policy": "Configure sticky policy",
@@ -677,7 +676,7 @@
 "label.endipv6": "IPv6 end IP",
 "label.endpoint": "Endpoint",
 "label.endport": "End port",
-"label.enter.code": "Enter 6-digit code",
+"label.enter.code": "Enter Authentication code to verify",
 "label.enter.token": "Enter token",
 "label.error": "Error",
 "label.error.caught": "Error caught",
@@ -1525,6 +1524,7 @@
 "label.select.ps": "Select primary storage",
 "label.select.tier": "Select tier",
 "label.select.zones": "Select zones",
+"label.select.2fa.provider": "Select the provider",
 "label.self": "Mine",
 "label.selfexecutable": "Self",
 "label.semanticversion": "Semantic version",
@@ -1736,6 +1736,7 @@
 "label.try.again": "Try again",
 "label.tuesday": "Tuesday",
 "label.two.factor.secret": "Your Two-factor secret",
+"label.two.factor.authentication": "Two Factor Authentication",
 "label.type": "Type",
 "label.type.id": "Type ID",
 "label.ucs": "UCS",
diff --git a/ui/src/views/dashboard/TwoFa.vue b/ui/src/views/dashboard/TwoFa.vue
index 31fd375fe4e..eb95d638426 100644
--- a/ui/src/views/dashboard/TwoFa.vue
+++ b/ui/src/views/dashboard/TwoFa.vue
@@ -27,7 +27,7 @@
       :src="$config.banner"
       class="user-layout-logo"
       alt="logo">
-    <h1 style="text-align: center; font-size: 24px; color: gray"> two-factor authentication </h1>
+    <h1 style="text-align: center; font-size: 24px; color: gray"> {{ $t('label.two.factor.authentication') }} </h1>
     <br />
     <br />
     <a-form
@@ -57,7 +57,9 @@
   </a-form>
 </template>
 <script>
-import { ref, reactive } from 'vue'
+
+import { api } from '@/api'
+import { ref, reactive, toRaw } from 'vue'
 
 export default {
   name: 'TwoFa',
@@ -78,6 +80,19 @@ export default {
       })
     },
     handleSubmit () {
+      this.formRef.value.validate().then(() => {
+        const values = toRaw(this.form)
+        console.log(values.secretkey)
+        api('validateUserTwoFactorAuthenticationCode', { '2facode': values.secretkey }).then(response => {
+          console.log(response)
+        }).catch(error => {
+          this.$notification.error({
+            message: this.$t('message.request.failed'),
+            description: (error.response && error.response.headers && error.response.headers['x-description']) || error.message
+          })
+        })
+      })
+
       // Add logic to set loginFlag to true
       this.$store.dispatch('SetLoginFlag', true)
     }
diff --git a/ui/src/views/iam/RegisterTwoFactorAuth.vue b/ui/src/views/iam/RegisterTwoFactorAuth.vue
index c9378772fb4..3ffb9f5b391 100644
--- a/ui/src/views/iam/RegisterTwoFactorAuth.vue
+++ b/ui/src/views/iam/RegisterTwoFactorAuth.vue
@@ -16,36 +16,71 @@
 // under the License.
 
 <template>
-  <h3> {{ $t('label.configure.app') }} </h3>
-  <div> {{ $t('message.two.fa.auth.register.account') }} </div>
-  <vue-qrious
-    class="center-align"
-    :value="resource.id"
-    @change="onDataUrlChange"
-  />
-  <br />
-  <div> {{ $t('message.two.fa.static.pin.part1') }} <a @click="generateStaticPin"> {{ $t('message.two.fa.static.pin.part2') }}</a></div>
-  <br />
-  <h3> {{ $t('label.enter.code') }} </h3>
-  <a-form @finish="submitPin" v-ctrl-enter="submitPin" class="container">
-    <a-input v-model:value="pin" />
-    <div :span="24">
-      <a-button ref="submit" type="primary" @click="submitPin">{{ $t('label.ok') }}</a-button>
+  <div style="width:500px;height=500px">
+  <h3> {{ $t('label.select.2fa.provider') }} </h3>
+  <a-form
+    :rules="rules"
+    layout="vertical">
+    <div class="form-layout" v-ctrl-enter="submitPin">
+      <a-select
+        v-model:value="selectedProvider"
+        optionFilterProp="label"
+        :filterOption="(input, option) => {
+          return option.children[0].children.toLowerCase().indexOf(input.toLowerCase()) >= 0
+        }"
+        style="width: 100%"
+        @change="val => { handleSelectChange(val) }">
+        <a-select-option
+          v-for="(opt) in providers"
+          :key="opt.name"
+          :disabled="opt.enabled === false">
+            {{ opt.name }}
+        </a-select-option>
+      </a-select>
     </div>
+    <div v-if="selectedProvider === 'google'">
+      <br />
+      <div> {{ $t('message.two.fa.auth.register.account') }} </div>
+      <vue-qrious
+        class="center-align"
+        :value="pin"
+        @change="onDataUrlChange"
+      />
+    </div>
+    <div v-else-if="selectedProvider === 'staticpin'">
+      <div> <a @click="setup2FAProvider"> {{ $t('message.two.fa.static.pin.part2') }}</a></div>
+    </div>
+    <div v-else-if="selectedProvider !== null && selectedProvider !== 'staticpin'">
+      <div> {{ $t('message.two.fa.static.pin.part1') }} <a @click="setup2FAProvider"> {{ $t('message.two.fa.static.pin.part2') }}</a></div>
+    </div>
+    <div v-if="selectedProvider">
+      <br />
+      <h3> {{ $t('label.enter.code') }} </h3>
+      <a-form @finish="submitPin" v-ctrl-enter="submitPin" class="container">
+        <a-input v-model:value="pin" />
+        <div :span="24">
+          <a-button ref="submit" type="primary" @click="submitPin">{{ $t('label.ok') }}</a-button>
+        </div>
+      </a-form>
+    </div>
+
+    <a-modal
+      v-if="showPin"
+      :visible="showPin"
+      :title="$t('label.two.factor.secret')"
+      :closable="true"
+      :footer="null"
+      @cancel="onCloseModal"
+      centered
+      width="450px">
+      <div> {{ pin }} </div>
+    </a-modal>
   </a-form>
-  <a-modal
-    v-if="showPin"
-    :visible="showPin"
-    :title="$t('label.two.factor.secret')"
-    :closable="true"
-    :footer="null"
-    @cancel="onCloseModal"
-    centered
-    width="450px">
-    <div> {{ pin }} </div>
-  </a-modal>
+  </div>
 </template>
 <script>
+
+import { api } from '@/api'
 import VueQrious from 'vue-qrious'
 export default {
   name: 'RegisterTwoFactorAuth',
@@ -62,13 +97,39 @@ export default {
     return {
       dataUrl: '',
       pin: '',
-      showPin: false
+      showPin: false,
+      providers: [],
+      selectedProvider: null
     }
   },
+  mounted () {
+    this.list2FAProviders()
+  },
   methods: {
     onDataUrlChange (dataUrl) {
       this.dataUrl = dataUrl
     },
+    handleSelectChange (val) {
+      this.selectedProvider = val
+      this.setup2FAProvider()
+    },
+    setup2FAProvider () {
+      api('setupUserTwoFactorAuthentication', { provider: this.selectedProvider }).then(response => {
+        console.log(response)
+        this.pin = response.setupusertwofactorauthenticationresponse.setup2fa.secretcode
+        this.showPin = true
+      }).catch(error => {
+        this.$notification.error({
+          message: this.$t('message.request.failed'),
+          description: (error.response && error.response.headers && error.response.headers['x-description']) || error.message
+        })
+      })
+    },
+    list2FAProviders () {
+      api('listUserTwoFactorAuthenticatorProviders', {}).then(response => {
+        this.providers = response.listusertwofactorauthenticatorprovidersresponse.providers || []
+      })
+    },
     submitPin () {
       // call api
     },
@@ -82,6 +143,7 @@ export default {
   }
 }
 </script>
+
 <style scoped>
   .center-align {
     display: block;