Skip to content

Commit

Permalink
add: verifyGRC() (#4)
Browse files Browse the repository at this point in the history
* add: verifyGRC helper fn

* changes from master | docs

* update: utils_full
  • Loading branch information
ttntm committed Nov 28, 2023
1 parent a8a2917 commit 3ea338e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion include.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
./src/updateSalesforceObject.js
./src/upsertDataExtRow.js
./src/useApexREST.js
./src/validateInput.js
./src/validateInput.js
./src/verifyGRC.js
25 changes: 25 additions & 0 deletions src/verifyGRC.js
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 28 additions & 1 deletion utils_full.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -588,5 +614,6 @@ function sfmcUtils() {
, upsertDataExtRow: upsertDataExtRow
, useApexREST: useApexREST
, validateInput: validateInput
, verifyGRC: verifyGRC
}
}

0 comments on commit 3ea338e

Please sign in to comment.