From 3ea338e39e68d6f37080c3c9e447442b72d6c261 Mon Sep 17 00:00:00 2001 From: Tom Doe <41571384+ttntm@users.noreply.github.com> Date: Tue, 28 Nov 2023 16:02:07 +0000 Subject: [PATCH] add: verifyGRC() (#4) * add: verifyGRC helper fn * changes from master | docs * update: utils_full --- README.md | 12 ++++++++++++ include.txt | 3 ++- src/verifyGRC.js | 25 +++++++++++++++++++++++++ utils_full.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/verifyGRC.js diff --git a/README.md b/README.md index c7a02f9..b019f18 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,18 @@ Checks and object for existence and value.length > 1 of the specified keys. _Boolean_ +## [verifyGRC](./src/verifyGRC.js) + +Verify a Google ReCaptcha payload. See: https://developers.google.com/recaptcha/docs/verify#api-response + +### Params + +- `grcToken`: _string_ - A ReCaptcha token from the front end + +### Returns + +_Boolean_ + # License MIT; see [./LICENSE](./LICENSE) diff --git a/include.txt b/include.txt index abbb0b6..fb4bf57 100644 --- a/include.txt +++ b/include.txt @@ -15,4 +15,5 @@ ./src/updateSalesforceObject.js ./src/upsertDataExtRow.js ./src/useApexREST.js -./src/validateInput.js \ No newline at end of file +./src/validateInput.js +./src/verifyGRC.js \ No newline at end of file diff --git a/src/verifyGRC.js b/src/verifyGRC.js new file mode 100644 index 0000000..01b4424 --- /dev/null +++ b/src/verifyGRC.js @@ -0,0 +1,25 @@ +/** + * Verify a Google ReCaptcha payload. + * See: https://developers.google.com/recaptcha/docs/verify#api-response + * @param {string} grcToken A ReCaptcha token from the front end + * @returns {boolean} + */ +function verifyGRC(grcToken) { + if (!grcToken) { + return false + } + + var grcPayload = 'secret=YOUR_SECRET' + '&response=' + grcToken + var target = 'https://www.google.com/recaptcha/api/siteverify' + + var res = HTTP.Post(target, 'application/x-www-form-urlencoded', grcPayload) + + if (res && res.StatusCode == 200) { + var parsed = Platform.Function.ParseJSON(res.Response[0]) + return parsed && parsed.success + ? parsed.success + : false + } else { + return false + } +} \ No newline at end of file diff --git a/utils_full.js b/utils_full.js index f0fec4f..72313b4 100644 --- a/utils_full.js +++ b/utils_full.js @@ -199,7 +199,7 @@ function sfmcUtils() { * @returns {object | undefined} */ function getToken(auth, mid) { - if (!auth || !auth - client_id || !auth.client_secret) { + if (!auth || !auth.client_id || !auth.client_secret) { return undefined } @@ -569,6 +569,32 @@ function sfmcUtils() { return count <= 0 } + /** + * Verify a Google ReCaptcha payload. + * See: https://developers.google.com/recaptcha/docs/verify#api-response + * @param {string} grcToken A ReCaptcha token from the front end + * @returns {boolean} + */ + function verifyGRC(grcToken) { + if (!grcToken) { + return false + } + + var grcPayload = 'secret=YOUR_SECRET' + '&response=' + grcToken + var target = 'https://www.google.com/recaptcha/api/siteverify' + + var res = HTTP.Post(target, 'application/x-www-form-urlencoded', grcPayload) + + if (res && res.StatusCode == 200) { + var parsed = Platform.Function.ParseJSON(res.Response[0]) + return parsed && parsed.success + ? parsed.success + : false + } else { + return false + } + } + return { createLogRow: createLogRow , createSalesforceObject: createSalesforceObject @@ -588,5 +614,6 @@ function sfmcUtils() { , upsertDataExtRow: upsertDataExtRow , useApexREST: useApexREST , validateInput: validateInput + , verifyGRC: verifyGRC } }