From 701a13ded7f917deccc67b090e0c78c46944c936 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Thu, 19 Dec 2019 14:57:23 -0500 Subject: [PATCH] authenticator verification working --- src/_services/user.service.js | 13 ++++- src/_store/account.module.js | 19 ++++++- src/_store/users.module.js | 20 ++++++- src/account/profile/PolicyPage.vue | 85 ++++++++++++++++++------------ 4 files changed, 100 insertions(+), 37 deletions(-) diff --git a/src/_services/user.service.js b/src/_services/user.service.js index fee6515..f43bc4f 100644 --- a/src/_services/user.service.js +++ b/src/_services/user.service.js @@ -14,7 +14,8 @@ export const userService = { update, delete: _delete, approveAction, - denyAction + denyAction, + sendAuthenticatorCode }; function setSessionUser (user) { @@ -83,6 +84,16 @@ function approveAction(id, code, messages, errors) { .then(setSessionUser); } +function sendAuthenticatorCode(id, code, verifyOnly, messages, errors) { + return fetch(`${config.apiUrl}/auth/authenticator`, postWithAuth({ + account: id, + token: parseInt(code), + verify: verifyOnly + })) + .then(handleCrudResponse(messages, errors)) + .then(setSessionUser); +} + function denyAction(id, code, messages, errors) { return fetch(`${config.apiUrl}/auth/deny/${code}`, postWithAuth()).then(handleCrudResponse(messages, errors)); } diff --git a/src/_store/account.module.js b/src/_store/account.module.js index db262ac..ed51b1f 100644 --- a/src/_store/account.module.js +++ b/src/_store/account.module.js @@ -87,7 +87,14 @@ const actions = { error => commit('denyActionFailure', error) ); }, - + sendAuthenticatorCode({ commit }, {uuid, code, verifyOnly, messages, errors}) { + commit('sendAuthenticatorCodeRequest'); + userService.sendAuthenticatorCode(uuid, code, verifyOnly, messages, errors) + .then( + policy => commit('sendAuthenticatorCodeSuccess', policy), + error => commit('sendAuthenticatorCodeFailure', error) + ); + } }; const mutations = { @@ -140,6 +147,16 @@ const mutations = { }, denyActionFailure(state, error) { state.actionStatus = { error: error, type: 'deny' }; + }, + sendAuthenticatorCodeRequest(state) { + state.actionStatus = { requesting: true, type: 'approve' }; + }, + sendAuthenticatorCodeSuccess(state, user) { + state.actionStatus = { success: true, type: 'approve', result: user }; + if (user.token) state.user = user; + }, + sendAuthenticatorCodeFailure(state, error) { + state.actionStatus = { error: error, type: 'approve' }; } }; diff --git a/src/_store/users.module.js b/src/_store/users.module.js index e7b777d..f71f3d4 100644 --- a/src/_store/users.module.js +++ b/src/_store/users.module.js @@ -7,9 +7,23 @@ const state = { policy: {}, policyStatus: {}, contact: null, - authenticator: null + authenticator: {} }; +function setAuthenticator(policy) { + if (policy && policy.accountContacts) { + const contacts = policy.accountContacts; + for (let i=0; i{{messages.message_true}} -
+
- +
{{messages.message_verify_authenticator_preamble}}
+
{{messages.message_verify_authenticator_backupCodes}}
- +

{{messages.message_verify_authenticator_backupCodes_description}} @@ -125,21 +126,21 @@
{{ errors.first('token') }}
- +
- + {{messages.field_label_policy_contact_authFactor_name_required}} - + {{messages.field_label_policy_contact_authFactor_name_sufficient}} - + {{messages.field_label_policy_contact_authFactor_name_not_required}} @@ -399,7 +400,7 @@ } }, methods: { - ...mapActions('account', ['approveAction', 'denyAction']), + ...mapActions('account', ['approveAction', 'denyAction', 'sendAuthenticatorCode']), ...mapActions('users', [ 'getPolicyByUuid', 'updatePolicyByUuid', 'addPolicyContactByUuid', 'removePolicyContactByUuid', ]), @@ -452,22 +453,6 @@ startVerifyContact(contact) { console.log('startVerifyContact: '+JSON.stringify(contact)); this.verifyingContact = contact.uuid; - if (contact.type === 'authenticator') { - const canvas = document.getElementById('canvas_'+contact.uuid); - QRCode.toCanvas(canvas, this.authenticator.key, function (error) { - if (error) { - console.error('QR generation error: '+error); - } else { - console.log('QR generation success'); - } - }); - const backupCodes = document.getElementById('backupCodes_'+contact.uuid); - if (backupCodes != null && typeof this.authenticator.backupCodes !== 'undefined' && this.authenticator.backupCodes != null && this.authenticator.backupCodes.length > 0) { - backupCodes.innerText = this.authenticator.backupCodes.join(' '); - } else { - console.log('backupCodes element not found, or no backupCodes defined'); - } - } return false; // do not follow the click }, cancelVerifyContact() { @@ -475,19 +460,31 @@ this.errors.clear(); return false; // do not follow the click }, - submitVerification(uuid) { + submitVerification(contact) { + const uuid = contact.uuid; + const type = contact.type; const codeElementId = 'verifyContactCode_'+uuid; const codeElement = document.getElementById(codeElementId); if (codeElement != null) { const code = codeElement.value; this.errors.clear(); - this.approveAction({ - uuid: this.currentUser.uuid, - code: code, - messages: this.messages, - errors: this.errors - }); - console.log('submitVerification: would submit: ' + code); + if (type === 'authenticator') { + // console.log('submitVerification: sending authenticator code: '+code); + this.sendAuthenticatorCode({ + uuid: this.currentUser.uuid, + code: code, + verifyOnly: true, + messages: this.messages, + errors: this.errors + }); + } else { + this.approveAction({ + uuid: this.currentUser.uuid, + code: code, + messages: this.messages, + errors: this.errors + }); + } } else { console.log('submitVerification: DOM element not found: '+codeElementId); } @@ -524,10 +521,32 @@ } }, actionStatus (status) { - console.log('watch.actionStatus: received: '+JSON.stringify(status)); + // console.log('watch.actionStatus: received: '+JSON.stringify(status)); if (status.success) { this.getPolicyByUuid({uuid: this.currentUser.uuid, messages: this.messages, errors: this.errors}); } + }, + authenticator (auth) { + // console.log('watch.authenticator: received: '+JSON.stringify(auth)); + if (auth.url) { + const checkExist = setInterval(function() { + if (document.getElementById('authenticator_qr_canvas') != null) { + clearInterval(checkExist); + const canvas = document.getElementById('authenticator_qr_canvas'); + if (canvas !== null) { + QRCode.toCanvas(canvas, auth.url, function (error) { + if (error) console.error('QR generation error: ' + error); + }); + const backupCodes = document.getElementById('authenticator_backupCodes'); + if (backupCodes != null && typeof auth.backupCodes !== 'undefined' && auth.backupCodes != null && auth.backupCodes.length > 0) { + backupCodes.innerHTML = auth.backupCodes.join('
'); + } else { + console.log('backupCodes element not found, or no backupCodes defined'); + } + } + } + }, 200); + } } }, created () {