From 22c9b5d3e76e36f99cce0a61e54e55edbaccbb1e Mon Sep 17 00:00:00 2001 From: nsdeschenes Date: Fri, 11 Jun 2021 08:42:52 -0300 Subject: [PATCH 1/3] implement new verified required function --- .../auth/__tests__/verified-required.test.js | 114 ++++++++++++++++++ api-js/src/auth/verified-required.js | 18 +++ 2 files changed, 132 insertions(+) create mode 100644 api-js/src/auth/__tests__/verified-required.test.js create mode 100644 api-js/src/auth/verified-required.js diff --git a/api-js/src/auth/__tests__/verified-required.test.js b/api-js/src/auth/__tests__/verified-required.test.js new file mode 100644 index 0000000000..581c0addeb --- /dev/null +++ b/api-js/src/auth/__tests__/verified-required.test.js @@ -0,0 +1,114 @@ +import { setupI18n } from '@lingui/core' + +import { verifiedRequired } from '../index' +import englishMessages from '../../locale/en/messages' +import frenchMessages from '../../locale/fr/messages' + +describe('given the verifiedRequired function', () => { + let i18n, user + + const consoleOutput = [] + const mockedWarn = (output) => consoleOutput.push(output) + + beforeAll(() => { + console.warn = mockedWarn + }) + afterEach(() => { + consoleOutput.length = 0 + }) + + describe('provided an email validated user', () => { + beforeEach(() => { + user = { + userName: 'test.account@istio.actually.exists', + displayName: 'Test Account', + preferredLang: 'french', + tfaValidated: false, + emailValidated: true, + } + }) + it('returns true', () => { + const verifiedFunc = verifiedRequired({}) + + const verifiedUser = verifiedFunc({ user }) + + expect(verifiedUser).toBe(true) + }) + }) + describe('language is set to english', () => { + beforeAll(() => { + i18n = setupI18n({ + locale: 'en', + localeData: { + en: { plurals: {} }, + fr: { plurals: {} }, + }, + locales: ['en', 'fr'], + messages: { + en: englishMessages.messages, + fr: frenchMessages.messages, + }, + }) + }) + describe('user is not email validated', () => { + beforeEach(() => { + user = { + userName: 'test.account@istio.actually.exists', + displayName: 'Test Account', + preferredLang: 'french', + tfaValidated: false, + emailValidated: false, + } + }) + it('throws an error', () => { + const verifiedFunc = verifiedRequired({ i18n }) + + try { + verifiedFunc({ user }) + } catch (err) { + expect(err).toEqual( + new Error( + 'Verification error. Please verify your account via email to access content.', + ), + ) + } + }) + }) + }) + describe('language is set to french', () => { + beforeAll(() => { + i18n = setupI18n({ + locale: 'fr', + localeData: { + en: { plurals: {} }, + fr: { plurals: {} }, + }, + locales: ['en', 'fr'], + messages: { + en: englishMessages.messages, + fr: frenchMessages.messages, + }, + }) + }) + describe('user is not email validated', () => { + beforeEach(() => { + user = { + userName: 'test.account@istio.actually.exists', + displayName: 'Test Account', + preferredLang: 'french', + tfaValidated: false, + emailValidated: false, + } + }) + it('throws an error', () => { + const verifiedFunc = verifiedRequired({ i18n }) + + try { + verifiedFunc({ user }) + } catch (err) { + expect(err).toEqual(new Error('todo')) + } + }) + }) + }) +}) diff --git a/api-js/src/auth/verified-required.js b/api-js/src/auth/verified-required.js new file mode 100644 index 0000000000..6d1cbaedb5 --- /dev/null +++ b/api-js/src/auth/verified-required.js @@ -0,0 +1,18 @@ +import { t } from '@lingui/macro' + +export const verifiedRequired = + ({ i18n }) => + ({ user }) => { + if (user.emailValidated) { + return true + } + + console.warn( + `User: ${user._key} attempted to access controlled functionality without verification.`, + ) + throw new Error( + i18n._( + t`Verification error. Please verify your account via email to access content.`, + ), + ) + } From 4c8acdea5f99e8554c0c190888a7e92a8f7df33e Mon Sep 17 00:00:00 2001 From: nsdeschenes Date: Fri, 11 Jun 2021 08:43:00 -0300 Subject: [PATCH 2/3] export verifiedRequired --- api-js/src/auth/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/api-js/src/auth/index.js b/api-js/src/auth/index.js index a1745721f7..ea91e93efc 100644 --- a/api-js/src/auth/index.js +++ b/api-js/src/auth/index.js @@ -5,4 +5,5 @@ export * from './check-super-admin' export * from './check-user-is-admin-for-user' export * from './generate-jwt' export * from './user-required' +export * from './verified-required' export * from './verify-jwt' From 0b059e1bb6229da3f5deceb9058a4dbcf59efdaa Mon Sep 17 00:00:00 2001 From: nsdeschenes Date: Fri, 11 Jun 2021 08:43:06 -0300 Subject: [PATCH 3/3] update translations --- api-js/src/locale/en/messages.js | 4 ++- api-js/src/locale/en/messages.po | 45 +++++++++++++++++--------------- api-js/src/locale/fr/messages.js | 4 ++- api-js/src/locale/fr/messages.po | 45 +++++++++++++++++--------------- 4 files changed, 54 insertions(+), 44 deletions(-) diff --git a/api-js/src/locale/en/messages.js b/api-js/src/locale/en/messages.js index 8eafe65288..d65ef17aaf 100644 --- a/api-js/src/locale/en/messages.js +++ b/api-js/src/locale/en/messages.js @@ -6,6 +6,7 @@ 'Authentication error. Please sign in.', 'Cannot query affiliations on organization without admin permission or higher.': 'Cannot query affiliations on organization without admin permission or higher.', + 'Email already in use.': 'Email already in use.', 'Error when retrieving dmarc report information. Please try again.': 'Error when retrieving dmarc report information. Please try again.', 'If an account with this username is found, a password reset link will be found in your inbox.': @@ -501,9 +502,10 @@ 'User could not be queried.': 'User could not be queried.', 'User role was updated successfully.': 'User role was updated successfully.', - 'Email already in use.': 'Email already in use.', 'Username not available, please try another.': 'Username not available, please try another.', + 'Verification error. Please verify your account via email to access content.': + 'Verification error. Please verify your account via email to access content.', 'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.': 'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.', 'You must provide a `first` or `last` value to properly paginate the `DKIM` connection.': diff --git a/api-js/src/locale/en/messages.po b/api-js/src/locale/en/messages.po index 5a5edbb094..5962b3e7bf 100644 --- a/api-js/src/locale/en/messages.po +++ b/api-js/src/locale/en/messages.po @@ -26,6 +26,10 @@ msgstr "Authentication error. Please sign in." msgid "Cannot query affiliations on organization without admin permission or higher." msgstr "Cannot query affiliations on organization without admin permission or higher." +#: src/user/mutations/sign-up.js:105 +msgid "Email already in use." +msgstr "Email already in use." + #: src/auth/check-domain-ownership.js:31 #: src/auth/check-domain-ownership.js:45 #: src/auth/check-domain-ownership.js:73 @@ -227,7 +231,7 @@ msgstr "Permission Denied: Please contact organization user for help with creati msgid "Permission Denied: Please contact organization user for help with retrieving this domain." msgstr "Permission Denied: Please contact organization user for help with retrieving this domain." -#: src/domain/mutations/request-scan.js:74 +#: src/domain/mutations/request-scan.js:67 msgid "Permission Denied: Please contact organization user for help with scanning this domain." msgstr "Permission Denied: Please contact organization user for help with scanning this domain." @@ -350,11 +354,11 @@ msgstr "Requesting {amount} records on the `SPF` connection exceeds the `{argSet msgid "Requesting {amount} records on the `SSL` connection exceeds the `{argSet}` limit of 100 records." msgstr "Requesting {amount} records on the `SSL` connection exceeds the `{argSet}` limit of 100 records." -#: src/domain/mutations/request-scan.js:134 +#: src/domain/mutations/request-scan.js:125 msgid "Successfully dispatched one time scan." msgstr "Successfully dispatched one time scan." -#: src/user/mutations/verify-account.js:122 +#: src/user/mutations/verify-account.js:111 msgid "Successfully email verified account, and set TFA send method to email." msgstr "Successfully email verified account, and set TFA send method to email." @@ -429,9 +433,9 @@ msgstr "Unable to create domain, organization has already claimed it." #: src/domain/mutations/create-domain.js:128 #: src/domain/mutations/create-domain.js:179 #: src/domain/mutations/create-domain.js:199 -#: src/domain/mutations/create-domain.js:225 -#: src/domain/mutations/create-domain.js:243 -#: src/domain/mutations/create-domain.js:253 +#: src/domain/mutations/create-domain.js:229 +#: src/domain/mutations/create-domain.js:247 +#: src/domain/mutations/create-domain.js:257 msgid "Unable to create domain. Please try again." msgstr "Unable to create domain. Please try again." @@ -441,9 +445,9 @@ msgstr "Unable to create domain. Please try again." msgid "Unable to create organization. Please try again." msgstr "Unable to create organization. Please try again." -#: src/domain/mutations/request-scan.js:97 -#: src/domain/mutations/request-scan.js:111 -#: src/domain/mutations/request-scan.js:125 +#: src/domain/mutations/request-scan.js:89 +#: src/domain/mutations/request-scan.js:103 +#: src/domain/mutations/request-scan.js:117 msgid "Unable to dispatch one time scan. Please try again." msgstr "Unable to dispatch one time scan. Please try again." @@ -657,8 +661,8 @@ msgstr "Unable to load mail summary. Please try again." #: src/organization/loaders/load-organization-by-key.js:33 #: src/organization/loaders/load-organization-by-key.js:47 -#: src/organization/loaders/load-organization-by-slug.js:34 -#: src/organization/loaders/load-organization-by-slug.js:48 +#: src/organization/loaders/load-organization-by-slug.js:36 +#: src/organization/loaders/load-organization-by-slug.js:51 #: src/organization/loaders/load-organization-connections-by-domain-id.js:516 #: src/organization/loaders/load-organization-connections-by-domain-id.js:528 #: src/organization/loaders/load-organization-connections-by-user-id.js:507 @@ -765,7 +769,7 @@ msgstr "Unable to remove user from this organization. Please try again." msgid "Unable to remove user from unknown organization." msgstr "Unable to remove user from unknown organization." -#: src/domain/mutations/request-scan.js:61 +#: src/domain/mutations/request-scan.js:54 msgid "Unable to request a one time scan on an unknown domain." msgstr "Unable to request a one time scan on an unknown domain." @@ -910,14 +914,13 @@ msgstr "Unable to update user's role. Please try again." msgid "Unable to update your own role." msgstr "Unable to update your own role." -#: src/user/mutations/verify-account.js:80 -#: src/user/mutations/verify-account.js:94 +#: src/user/mutations/verify-account.js:53 +#: src/user/mutations/verify-account.js:70 +#: src/user/mutations/verify-account.js:83 msgid "Unable to verify account. Please request a new email." msgstr "Unable to verify account. Please request a new email." -#: src/user/mutations/verify-account.js:46 -#: src/user/mutations/verify-account.js:61 -#: src/user/mutations/verify-account.js:112 +#: src/user/mutations/verify-account.js:101 msgid "Unable to verify account. Please try again." msgstr "Unable to verify account. Please try again." @@ -946,14 +949,14 @@ msgstr "User could not be queried." msgid "User role was updated successfully." msgstr "User role was updated successfully." -#: src/user/mutations/sign-up.js:105 -msgid "Username already in use." -msgstr "Username already in use." - #: src/user/mutations/update-user-profile.js:71 msgid "Username not available, please try another." msgstr "Username not available, please try another." +#: src/auth/verified-required.js:15 +msgid "Verification error. Please verify your account via email to access content." +msgstr "Verification error. Please verify your account via email to access content." + #: src/email-scan/loaders/load-dkim-results-connections-by-dkim-id.js:86 msgid "You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection." msgstr "You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection." diff --git a/api-js/src/locale/fr/messages.js b/api-js/src/locale/fr/messages.js index c7d2f3e332..a6e5e55832 100644 --- a/api-js/src/locale/fr/messages.js +++ b/api-js/src/locale/fr/messages.js @@ -4,6 +4,7 @@ 'Authentication error. Please sign in.': 'todo', 'Cannot query affiliations on organization without admin permission or higher.': 'todo', + 'Email already in use.': 'todo', 'Error when retrieving dmarc report information. Please try again.': 'todo', 'If an account with this username is found, a password reset link will be found in your inbox.': 'todo', @@ -261,8 +262,9 @@ 'Unable to verify unknown organization.': 'todo', 'User could not be queried.': 'todo', 'User role was updated successfully.': 'todo', - 'Email already in use.': 'todo', 'Username not available, please try another.': 'todo', + 'Verification error. Please verify your account via email to access content.': + 'todo', 'You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection.': 'todo', 'You must provide a `first` or `last` value to properly paginate the `DKIM` connection.': diff --git a/api-js/src/locale/fr/messages.po b/api-js/src/locale/fr/messages.po index a96992b401..73b90c7ff8 100644 --- a/api-js/src/locale/fr/messages.po +++ b/api-js/src/locale/fr/messages.po @@ -26,6 +26,10 @@ msgstr "todo" msgid "Cannot query affiliations on organization without admin permission or higher." msgstr "todo" +#: src/user/mutations/sign-up.js:105 +msgid "Email already in use." +msgstr "todo" + #: src/auth/check-domain-ownership.js:31 #: src/auth/check-domain-ownership.js:45 #: src/auth/check-domain-ownership.js:73 @@ -227,7 +231,7 @@ msgstr "todo" msgid "Permission Denied: Please contact organization user for help with retrieving this domain." msgstr "todo" -#: src/domain/mutations/request-scan.js:74 +#: src/domain/mutations/request-scan.js:67 msgid "Permission Denied: Please contact organization user for help with scanning this domain." msgstr "todo" @@ -350,11 +354,11 @@ msgstr "todo" msgid "Requesting {amount} records on the `SSL` connection exceeds the `{argSet}` limit of 100 records." msgstr "todo" -#: src/domain/mutations/request-scan.js:134 +#: src/domain/mutations/request-scan.js:125 msgid "Successfully dispatched one time scan." msgstr "todo" -#: src/user/mutations/verify-account.js:122 +#: src/user/mutations/verify-account.js:111 msgid "Successfully email verified account, and set TFA send method to email." msgstr "todo" @@ -429,9 +433,9 @@ msgstr "todo" #: src/domain/mutations/create-domain.js:128 #: src/domain/mutations/create-domain.js:179 #: src/domain/mutations/create-domain.js:199 -#: src/domain/mutations/create-domain.js:225 -#: src/domain/mutations/create-domain.js:243 -#: src/domain/mutations/create-domain.js:253 +#: src/domain/mutations/create-domain.js:229 +#: src/domain/mutations/create-domain.js:247 +#: src/domain/mutations/create-domain.js:257 msgid "Unable to create domain. Please try again." msgstr "todo" @@ -441,9 +445,9 @@ msgstr "todo" msgid "Unable to create organization. Please try again." msgstr "todo" -#: src/domain/mutations/request-scan.js:97 -#: src/domain/mutations/request-scan.js:111 -#: src/domain/mutations/request-scan.js:125 +#: src/domain/mutations/request-scan.js:89 +#: src/domain/mutations/request-scan.js:103 +#: src/domain/mutations/request-scan.js:117 msgid "Unable to dispatch one time scan. Please try again." msgstr "todo" @@ -657,8 +661,8 @@ msgstr "todo" #: src/organization/loaders/load-organization-by-key.js:33 #: src/organization/loaders/load-organization-by-key.js:47 -#: src/organization/loaders/load-organization-by-slug.js:34 -#: src/organization/loaders/load-organization-by-slug.js:48 +#: src/organization/loaders/load-organization-by-slug.js:36 +#: src/organization/loaders/load-organization-by-slug.js:51 #: src/organization/loaders/load-organization-connections-by-domain-id.js:516 #: src/organization/loaders/load-organization-connections-by-domain-id.js:528 #: src/organization/loaders/load-organization-connections-by-user-id.js:507 @@ -765,7 +769,7 @@ msgstr "todo" msgid "Unable to remove user from unknown organization." msgstr "todo" -#: src/domain/mutations/request-scan.js:61 +#: src/domain/mutations/request-scan.js:54 msgid "Unable to request a one time scan on an unknown domain." msgstr "todo" @@ -910,14 +914,13 @@ msgstr "todo" msgid "Unable to update your own role." msgstr "todo" -#: src/user/mutations/verify-account.js:80 -#: src/user/mutations/verify-account.js:94 +#: src/user/mutations/verify-account.js:53 +#: src/user/mutations/verify-account.js:70 +#: src/user/mutations/verify-account.js:83 msgid "Unable to verify account. Please request a new email." msgstr "todo" -#: src/user/mutations/verify-account.js:46 -#: src/user/mutations/verify-account.js:61 -#: src/user/mutations/verify-account.js:112 +#: src/user/mutations/verify-account.js:101 msgid "Unable to verify account. Please try again." msgstr "todo" @@ -946,14 +949,14 @@ msgstr "todo" msgid "User role was updated successfully." msgstr "todo" -#: src/user/mutations/sign-up.js:105 -msgid "Username already in use." -msgstr "todo" - #: src/user/mutations/update-user-profile.js:71 msgid "Username not available, please try another." msgstr "todo" +#: src/auth/verified-required.js:15 +msgid "Verification error. Please verify your account via email to access content." +msgstr "todo" + #: src/email-scan/loaders/load-dkim-results-connections-by-dkim-id.js:86 msgid "You must provide a `first` or `last` value to properly paginate the `DKIMResults` connection." msgstr "todo"