Last active
August 16, 2018 02:30
-
-
Save benjsicam/5545658 to your computer and use it in GitHub Desktop.
A suitelet that acts as a proxy for reCaptcha verification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var CAPTCHA_VERIFICATION_URL = 'https://www.google.com/recaptcha/api/verify'; //This is the verification URL | |
| var CAPTCHA_PRIVATE_KEY = '6Lf1-OASAAAAAFkwdm0fLSAPErgLRw54VzF4aPrO'; //This is the private key we've obtained from Part 1 of this tutorial | |
| /** | |
| * | |
| * @param {nlobjRequest} request Request object | |
| * @param {nlobjResponse} response Response object | |
| * @returns {Void} Any output is written via response object | |
| */ | |
| function main(request, response) { | |
| var jsonResponse = new JSONResponse(); | |
| try { | |
| if (request.getMethod() == 'POST') { | |
| var clientIpAddress = request.getHeader('NS-Client-IP'); //Use this to get the IP Address of the client | |
| var captchaChallenge = request.getParameter('challenge'); //This parameter should be included on the request | |
| var captchaResponse = request.getParameter('response'); //This parameter should be included on the request | |
| /* | |
| * Build the post data. | |
| */ | |
| var postData = { | |
| privatekey: CAPTCHA_PRIVATE_KEY, | |
| remoteip: clientIpAddress, | |
| challenge: captchaChallenge, | |
| response: captchaResponse | |
| }; | |
| var captchaVerificationResponse = nlapiRequestURL(CAPTCHA_VERIFICATION_URL, postData); //Send the POST Request to Google for verification. | |
| /* | |
| * Test the response. If the server does not respond with an HTTP 200 Status code, then it was not verified. | |
| * Google also returns true when the challenge and the user's response matches. | |
| */ | |
| if (captchaVerificationResponse.getCode() != 200 || captchaVerificationResponse.getBody().indexOf('true') == -1) { | |
| jsonResponse.status.isSuccess = false; | |
| jsonResponse.status.errorMessage = 'Failed Verification'; | |
| } | |
| } | |
| } catch (e) { //Catch any exception and fail the verification. | |
| jsonResponse.status.isSuccess = false; | |
| jsonResponse.status.errorMessage = 'An unexpected error has occurred. Error: ' + e.message; | |
| } | |
| response.setContentType('JSON'); //Sets the response Content Type to application/json | |
| response.write(JSON.stringify(jsonResponse)); //Serialize the JSONResponse object and return it. | |
| } | |
| /** | |
| * Encapsulate the JSON Response | |
| * @returns {JSONResponse} instance of JSONResponse object | |
| */ | |
| function JSONResponse() { | |
| this.status = { | |
| isSuccess: true, | |
| errorMessage: '' | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment