11import constants from '../../utils/constants.js'
22import { getErrorSummary } from '../../utils/helpers.js'
33import config from '../../utils/config.js'
4+ import { post as postRequest } from '../../utils/util.js'
45
56const postcodeRegExp = / ^ ( [ A - Z a - z ] [ A - H a - h J - Y j - y ] ? \d [ A - Z a - z 0 - 9 ] ? ? \d [ A - Z a - z ] { 2 } | [ G g ] [ I i ] [ R r ] ? 0 [ A a ] { 2 } ) $ / // https://stackoverflow.com/a/51885364
67const captchaSiteKey = config . captchaSiteKey
78
9+ // Put these somewhere more sensible
10+ const captchaVerifyUrl = 'https://global.frcapi.com/api/v2/captcha/siteverify'
11+
812const handlers = {
913 get : async ( request , h ) => {
1014 const counterVal = request . yar . get ( constants . redisKeys . COUNTER )
@@ -20,6 +24,37 @@ const handlers = {
2024 } )
2125 } ,
2226 post : async ( request , h ) => {
27+ console . log ( '---CAPTCHA---' )
28+ const captchaResponse = request . payload [ 'frc-captcha-response' ]
29+ let captchaSuccess = true // FIXME this needed as the else in the following if, find a nicer way
30+
31+ if ( captchaResponse ) {
32+ console . log ( `Captcha response: ${ captchaResponse } ` )
33+ console . log ( 'Verifying response with external API ...' )
34+
35+ const captchaVerifyResponse = await postRequest (
36+ captchaVerifyUrl ,
37+ {
38+ headers : {
39+ 'X-API-Key' : config . captchaApiKey ,
40+ 'Content-Type' : 'application/json' ,
41+ Accept : 'application/json'
42+ } ,
43+ payload : {
44+ response : captchaResponse ,
45+ sitekey : config . captchaSiteKey
46+ } ,
47+ json : true
48+ }
49+ )
50+
51+ captchaSuccess = captchaVerifyResponse . success
52+ console . log ( `Success: ${ captchaVerifyResponse . success } ` )
53+ } else {
54+ console . log ( 'No response from Captcha, ignoring' )
55+ }
56+ console . log ( '---END CAPTCHA---' )
57+
2358 let { buildingDetails, postcode } = request . payload
2459
2560 // cleanse postcode for special characters https://design-system.service.gov.uk/patterns/addresses/#allow-different-postcode-formats
@@ -28,7 +63,7 @@ const handlers = {
2863 }
2964
3065 // validate payload
31- const errorSummary = validatePayload ( buildingDetails , postcode )
66+ const errorSummary = validatePayload ( buildingDetails , postcode , captchaSuccess )
3267 if ( errorSummary . errorList . length > 0 ) {
3368 return h . view ( constants . views . SMELL_FIND_ADDRESS , {
3469 errorSummary,
@@ -42,7 +77,9 @@ const handlers = {
4277 request . yar . set ( constants . redisKeys . COUNTER , counterVal + 1 )
4378
4479 // handle redirects
45- if ( counterVal > 10 ) {
80+ const counterLimit = 100000 // FIXME: CORRECT THIS, for debugging
81+
82+ if ( counterVal > counterLimit ) {
4683 return h . redirect ( constants . routes . SMELL_EXCEEDED_ATTEMPTS )
4784 } else {
4885 request . yar . set ( constants . redisKeys . SMELL_FIND_ADDRESS , buildAnswers ( buildingDetails , postcode ) )
@@ -61,8 +98,15 @@ const getContext = (request) => {
6198 }
6299}
63100
64- const validatePayload = ( buildingDetails , postcode ) => {
101+ const validatePayload = ( buildingDetails , postcode , captchaSuccess ) => {
65102 const errorSummary = getErrorSummary ( )
103+ if ( ! captchaSuccess ) {
104+ errorSummary . errorList . push ( {
105+ text : 'Failed Captcha check' ,
106+ href : '#' // FIXME: add this
107+ } )
108+ }
109+
66110 if ( ! buildingDetails ) {
67111 errorSummary . errorList . push ( {
68112 text : 'Enter a building number or name' ,
0 commit comments