@mindful-web/marko-web-auth0-identity-x 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Parameter1 LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ IdentityX + Auth0
2
+ ===
@@ -0,0 +1,42 @@
1
+ const { decode } = require('jsonwebtoken');
2
+
3
+ const { log } = console;
4
+
5
+ /**
6
+ * Syncs Auth0 and IdentityX user states
7
+ *
8
+ * @param {RequestContext} req
9
+ * @param {ResponseContext} res
10
+ * @param {Object} session
11
+ * @returns Object the Auth0 user session object
12
+ */
13
+ module.exports = async (req, _, session) => {
14
+ // Only handle if Auth0 & IdentityX are loaded
15
+ if (!req.identityX) throw new Error('IdentityX must be enabled and configured!');
16
+
17
+ const { identityX: service } = req;
18
+ const { token } = service;
19
+ const user = await decode(session.id_token);
20
+
21
+ // If there's no Auth0 context, or an IdX context already exists, there's nothing to do here.
22
+ if (!user || token) return session;
23
+
24
+ // Destroy A0 context if no email is present
25
+ const { email, email_verified: ev } = user;
26
+ if (!email || !ev) throw new Error('Auth0 user must provide a verified email address.');
27
+
28
+ // Upsert the IdentityX AppUser
29
+ const appUser = await service.createAppUser({ email });
30
+
31
+ // federate trusted verification state to IdX and log in via impersonation api
32
+ try {
33
+ await service.impersonateAppUser({ userId: appUser.id });
34
+ log('A0+IdX.cb', 'impersonated', appUser.id);
35
+ } catch (e) {
36
+ log('A0+IdX.cb', 'autherr', e);
37
+ throw e;
38
+ }
39
+
40
+ // Return the user session
41
+ return session;
42
+ };
package/index.js ADDED
@@ -0,0 +1,42 @@
1
+ const Joi = require('@parameter1/joi');
2
+ const { validate } = require('@parameter1/joi/utils');
3
+ const auth0 = require('@mindful-web/marko-web-auth0');
4
+ const identityX = require('@mindful-web/marko-web-identity-x');
5
+ const IdXConfig = require('@mindful-web/marko-web-identity-x/config');
6
+ const middleware = require('./middleware');
7
+ const afterCallback = require('./after-callback');
8
+
9
+ module.exports = (app, params = {}) => {
10
+ const {
11
+ // Auth0 Configs
12
+ baseURL,
13
+ clientID,
14
+ issuerBaseURL,
15
+ clientSecret,
16
+ // IdentityX Config
17
+ idxConfig,
18
+ idxRouteTemplates,
19
+ } = validate(Joi.object({
20
+ baseURL: Joi.string().required().description('The application\'s currently available URL.'),
21
+ clientID: Joi.string().required().description('The application\'s Auth0 ClientID'),
22
+ clientSecret: Joi.string().required().description('The application\'s Auth0 Client Secert'),
23
+ issuerBaseURL: Joi.string().required().description('The Auth0 tenant URL'),
24
+ idxConfig: Joi.object().required().instance(IdXConfig),
25
+ idxRouteTemplates: Joi.object().required(),
26
+ }), params);
27
+
28
+ // install identity x
29
+ identityX(app, idxConfig, { templates: idxRouteTemplates });
30
+
31
+ // install auth0 middleware
32
+ auth0(app, {
33
+ baseURL,
34
+ clientID,
35
+ issuerBaseURL,
36
+ secret: clientSecret,
37
+ afterCallback,
38
+ });
39
+
40
+ // Load A0+IdX middleware
41
+ app.use(middleware);
42
+ };
package/middleware.js ADDED
@@ -0,0 +1,71 @@
1
+ const { asyncRoute } = require('@mindful-web/utils');
2
+
3
+ const isEmpty = (v) => v == null || v === '';
4
+
5
+ /**
6
+ * Determines if user input is required.
7
+ *
8
+ * @param {*} service The IdentityX service instance
9
+ * @returns Boolean
10
+ */
11
+ const isInputRequired = async (service) => {
12
+ const { user: activeUser, application } = await service.loadActiveContext({ forceQuery: true });
13
+ const user = activeUser || {};
14
+
15
+ // Check that all requires fields (from IdentityX config) are set
16
+ const requiredFields = service.config.getRequiredServerFields();
17
+ const requiresUserInput = requiredFields.some((key) => isEmpty(user[key]));
18
+ if (requiresUserInput) return true;
19
+
20
+ // Check that the user does not need to reverify their profile
21
+ const mustReverify = Boolean(user.mustReVerifyProfile);
22
+ if (mustReverify) return true;
23
+
24
+ // Check that all regional consent policies are agreed to
25
+ const { regionalConsentPolicies } = application.organization;
26
+ const matchingPolicies = regionalConsentPolicies.filter((policy) => {
27
+ const countryCodes = policy.countries.map((country) => country.id);
28
+ return countryCodes.includes(user.countryCode);
29
+ });
30
+ const policiesAnswered = user.regionalConsentAnswers
31
+ .reduce((o, answer) => ({ ...o, [answer.id]: true }), {});
32
+ const hasRequiredAnswers = matchingPolicies.length
33
+ ? matchingPolicies.every((policy) => policiesAnswered[policy.id])
34
+ : true;
35
+
36
+ return !hasRequiredAnswers;
37
+ };
38
+
39
+ module.exports = asyncRoute(async (req, res, next) => {
40
+ // Only handle if Auth0 & IdentityX are loaded
41
+ if (!req.oidc || !req.identityX) throw new Error('Auth0 and IdentityX must be enabled!');
42
+
43
+ const { identityX: idxSvc, originalUrl } = req;
44
+ const { user } = req.oidc;
45
+
46
+ // the Auth0 user has been logged out, log out the IdentityX user.
47
+ if (!user && idxSvc.token) {
48
+ await idxSvc.logoutAppUser();
49
+ }
50
+
51
+ if (idxSvc.token && req.query.isAuth0Login) {
52
+ const profile = idxSvc.config.getEndpointFor('profile');
53
+ const url = new URL(`${req.protocol}://${req.get('host')}${originalUrl}`);
54
+ url.searchParams.delete('isAuth0Login');
55
+ const returnTo = `${url}`;
56
+
57
+ if (await isInputRequired(idxSvc)) {
58
+ // If the user attempted to access the profile page, don't redirect after submission.
59
+ if (url.pathname === profile) {
60
+ res.redirect(302, profile);
61
+ } else {
62
+ res.redirect(302, `${profile}?returnTo=${encodeURIComponent(returnTo)}`);
63
+ }
64
+ } else {
65
+ // Strip the query parameter
66
+ res.redirect(302, returnTo);
67
+ }
68
+ } else {
69
+ next();
70
+ }
71
+ });
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@mindful-web/marko-web-auth0-identity-x",
3
+ "version": "1.0.0",
4
+ "author": "Josh Worden <josh@parameter1.com>",
5
+ "repository": "https://github.com/parameter1/mindful-web/tree/main/packages/marko-web-auth0-identity-x",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "lint:fix": "yarn lint --fix",
9
+ "lint": "eslint --ext .js --ext .vue --max-warnings 5 --ignore-path ../../.eslintignore ./",
10
+ "test": "yarn lint"
11
+ },
12
+ "dependencies": {
13
+ "@mindful-web/marko-web-auth0": "^1.0.0",
14
+ "@mindful-web/marko-web-identity-x": "^1.0.0",
15
+ "@mindful-web/utils": "^1.0.0",
16
+ "@parameter1/joi": "^1.2.10",
17
+ "jsonwebtoken": "^8.5.1"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "gitHead": "0b77cab713eb5841202bb86c7119949866bc68b5"
23
+ }