From 1caed664b6ffb59e712b0289f3ebb66f6b9ba33d Mon Sep 17 00:00:00 2001 From: Josh Worden Date: Tue, 26 Jul 2022 14:32:32 -0500 Subject: [PATCH] Add Joi valildation to middleware, hooks --- .../add-integration-hooks.js | 52 +++++++++++-------- packages/marko-web-omeda-identity-x/index.js | 8 +-- .../on-authentication-success.js | 17 ++++-- .../integration-hooks/on-login-link-sent.js | 40 ++++++++------ .../integration-hooks/on-logout.js | 9 +++- .../on-user-profile-update.js | 28 ++++++---- 6 files changed, 98 insertions(+), 56 deletions(-) diff --git a/packages/marko-web-omeda-identity-x/add-integration-hooks.js b/packages/marko-web-omeda-identity-x/add-integration-hooks.js index e69676ef3..d3e42e939 100644 --- a/packages/marko-web-omeda-identity-x/add-integration-hooks.js +++ b/packages/marko-web-omeda-identity-x/add-integration-hooks.js @@ -1,4 +1,7 @@ - +const Joi = require('@parameter1/joi'); +const { validate } = require('@parameter1/joi/utils'); +const { get } = require('@parameter1/base-cms-object-path'); +const IdXConfig = require('@parameter1/base-cms-marko-web-identity-x/config'); const { onAuthenticationSuccess, onLoginLinkSent, @@ -11,25 +14,33 @@ const { * @param {object} params * @param {IdentityXConfiguration} params.idxConfig */ -module.exports = ({ - idxConfig, - brandKey, - omedaGraphQLProp = '$omedaGraphQLClient', - idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify', -} = {}) => { - if (!idxConfig) throw new Error('The IdentityX configuration instances is required to add Omeda+IdentityX integration hooks.'); +module.exports = (params = {}) => { + const { + idxConfig, + brandKey, + omedaGraphQLClientProp, + idxOmedaRapidIdentifyProp, + omedaPromoCodeCookieName, + omedaPromoCodeDefault, + } = validate(Joi.object({ + brandKey: Joi.string().required(), + idxConfig: Joi.object().instance(IdXConfig).required(), + idxOmedaRapidIdentifyProp: Joi.string().required(), + omedaGraphQLClientProp: Joi.string().required(), + omedaPromoCodeCookieName: Joi.string().required(), + omedaPromoCodeDefault: Joi.string(), + }), params); + idxConfig.addHook({ name: 'onLoginLinkSent', shouldAwait: false, fn: async args => onLoginLinkSent({ ...args, brandKey, - omedaGraphQLProp, - idxOmedaRapidIdentifyProp, - - req: args.req, - service: args.service, - user: args.user, + idxOmedaRapidIdentify: get(args, `req.${idxOmedaRapidIdentifyProp}`), + omedaGraphQLClient: get(args, `req.${omedaGraphQLClientProp}`), + omedaPromoCodeCookieName, + omedaPromoCodeDefault, }), }); @@ -39,9 +50,6 @@ module.exports = ({ fn: async args => onAuthenticationSuccess({ ...args, brandKey, - res: args.res, - req: args.req, - user: args.user, }), }); @@ -50,16 +58,18 @@ module.exports = ({ shouldAwait: false, fn: async args => onUserProfileUpdate({ ...args, - idxOmedaRapidIdentifyProp, - req: args.req, - user: args.user, + idxOmedaRapidIdentify: get(args, `req.${idxOmedaRapidIdentifyProp}`), + omedaPromoCodeCookieName, + omedaPromoCodeDefault, }), }); idxConfig.addHook({ name: 'onLogout', shouldAwait: true, - fn: async ({ res }) => onLogout({ res }), + fn: async args => onLogout({ + ...args, + }), }); return idxConfig; diff --git a/packages/marko-web-omeda-identity-x/index.js b/packages/marko-web-omeda-identity-x/index.js index 27c7c0efa..8fcd07478 100644 --- a/packages/marko-web-omeda-identity-x/index.js +++ b/packages/marko-web-omeda-identity-x/index.js @@ -61,10 +61,12 @@ module.exports = (app, params = {}) => { // add appropiate identity-x to omeda integration hooks addOmedaHooksToIdentityXConfig({ - idxConfig, brandKey, - productId: rapidIdentProductId, - omedaGraphQLProp: omedaGraphQLClientProp, + idxConfig, + idxOmedaRapidIdentifyProp, + omedaGraphQLClientProp, + omedaPromoCodeCookieName, + omedaPromoCodeDefault, }); // attach the identity-x rapid identification wrapper middleware diff --git a/packages/marko-web-omeda-identity-x/integration-hooks/on-authentication-success.js b/packages/marko-web-omeda-identity-x/integration-hooks/on-authentication-success.js index 46a234cf0..9a56045f5 100644 --- a/packages/marko-web-omeda-identity-x/integration-hooks/on-authentication-success.js +++ b/packages/marko-web-omeda-identity-x/integration-hooks/on-authentication-success.js @@ -1,11 +1,18 @@ +const Joi = require('@parameter1/joi'); +const { validate } = require('@parameter1/joi/utils'); const olyticsCookie = require('@parameter1/base-cms-marko-web-omeda/olytics/customer-cookie'); const findEncryptedId = require('../external-id/find-encrypted-customer-id'); -module.exports = async ({ - brandKey, - user, - res, -}) => { +module.exports = async (params = {}) => { + const { + brandKey, + user, + res, + } = validate(Joi.object({ + brandKey: Joi.string().required(), + user: Joi.object().required(), + res: Joi.object().required(), + }).unknown(true), params); const encryptedId = findEncryptedId({ externalIds: user.externalIds, brandKey }); if (!encryptedId) return; olyticsCookie.setTo(res, encryptedId); diff --git a/packages/marko-web-omeda-identity-x/integration-hooks/on-login-link-sent.js b/packages/marko-web-omeda-identity-x/integration-hooks/on-login-link-sent.js index 961ec9481..c6df88523 100644 --- a/packages/marko-web-omeda-identity-x/integration-hooks/on-login-link-sent.js +++ b/packages/marko-web-omeda-identity-x/integration-hooks/on-login-link-sent.js @@ -1,3 +1,5 @@ +const Joi = require('@parameter1/joi'); +const { validate } = require('@parameter1/joi/utils'); const extractPromoCode = require('../utils/extract-promo-code'); const { getAnsweredQuestionMap, @@ -6,22 +8,28 @@ const { updateIdentityX, } = require('../omeda-data'); -module.exports = async ({ - brandKey, - omedaGraphQLProp = '$omedaGraphQLClient', - idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify', - omedaPromoCodeCookieName = 'omeda_promo_code', - omedaPromoCodeDefault, - promoCode: hookDataPromoCode, - - req, - service: identityX, - user, -}) => { - const omedaGraphQLClient = req[omedaGraphQLProp]; - if (!omedaGraphQLClient) throw new Error(`Unable to load the Omeda GraphQL API from the request using prop ${omedaGraphQLProp}`); - const idxOmedaRapidIdentify = req[idxOmedaRapidIdentifyProp]; - if (!idxOmedaRapidIdentify) throw new Error(`Unable to find the IdentityX+Omeda rapid identifier on the request using ${idxOmedaRapidIdentifyProp}`); +module.exports = async (params = {}) => { + const { + brandKey, + idxOmedaRapidIdentify, + omedaGraphQLClient, + omedaPromoCodeCookieName, + omedaPromoCodeDefault, + promoCode: hookDataPromoCode, + req, + service: identityX, + user, + } = validate(Joi.object({ + brandKey: Joi.string().required(), + idxOmedaRapidIdentify: Joi.function().required(), + omedaGraphQLClient: Joi.object().required(), + omedaPromoCodeCookieName: Joi.string().required(), + omedaPromoCodeDefault: Joi.string(), + promoCode: Joi.string(), + req: Joi.object().required(), + service: Joi.object().required(), + user: Joi.object().required(), + }).unknown(), params); const promoCode = extractPromoCode({ promoCode: hookDataPromoCode, diff --git a/packages/marko-web-omeda-identity-x/integration-hooks/on-logout.js b/packages/marko-web-omeda-identity-x/integration-hooks/on-logout.js index 78b547673..518483949 100644 --- a/packages/marko-web-omeda-identity-x/integration-hooks/on-logout.js +++ b/packages/marko-web-omeda-identity-x/integration-hooks/on-logout.js @@ -1,5 +1,12 @@ +const Joi = require('@parameter1/joi'); +const { validate } = require('@parameter1/joi/utils'); const olyticsCookie = require('@parameter1/base-cms-marko-web-omeda/olytics/customer-cookie'); -module.exports = ({ res }) => { +module.exports = (params = {}) => { + const { + res, + } = validate(Joi.object({ + res: Joi.object().required(), + }).unknown(), params); olyticsCookie.clearFrom(res); }; diff --git a/packages/marko-web-omeda-identity-x/integration-hooks/on-user-profile-update.js b/packages/marko-web-omeda-identity-x/integration-hooks/on-user-profile-update.js index a1b2e49cd..54d31bbec 100644 --- a/packages/marko-web-omeda-identity-x/integration-hooks/on-user-profile-update.js +++ b/packages/marko-web-omeda-identity-x/integration-hooks/on-user-profile-update.js @@ -1,15 +1,23 @@ +const Joi = require('@parameter1/joi'); +const { validate } = require('@parameter1/joi/utils'); const extractPromoCode = require('../utils/extract-promo-code'); -module.exports = async ({ - idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify', - omedaPromoCodeCookieName = 'omeda_promo_code', - omedaPromoCodeDefault, - req, - user, - promoCode: hookDataPromoCode, -}) => { - const idxOmedaRapidIdentify = req[idxOmedaRapidIdentifyProp]; - if (!idxOmedaRapidIdentify) throw new Error(`Unable to find the IdentityX+Omeda rapid identifier on the request using ${idxOmedaRapidIdentifyProp}`); +module.exports = async (params = {}) => { + const { + idxOmedaRapidIdentify, + omedaPromoCodeCookieName, + omedaPromoCodeDefault, + promoCode: hookDataPromoCode, + req, + user, + } = validate(Joi.object({ + idxOmedaRapidIdentify: Joi.function().required(), + omedaPromoCodeCookieName: Joi.string().required(), + omedaPromoCodeDefault: Joi.string(), + promoCode: Joi.string(), + req: Joi.object().required(), + user: Joi.object().required(), + }).unknown(), params); const promoCode = extractPromoCode({ promoCode: hookDataPromoCode,