Skip to content

Commit 90aa85f

Browse files
Verify captcha via API
1 parent 926948f commit 90aa85f

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

server/plugins/logging.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export default {
1717
'/public/js/locationMap.js',
1818
'/public/js/site.min.js',
1919
'/public/js/site.compat.min.js',
20-
'/public/js/locationMap.js',
2120
'/public/govuk-frontend.min.js',
2221
'/public/images/favicon.svg',
2322
'/public/images/favicon.ico',

server/routes/smell/find-address.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import constants from '../../utils/constants.js'
22
import { getErrorSummary } from '../../utils/helpers.js'
33
import config from '../../utils/config.js'
4+
import { post as postRequest } from '../../utils/util.js'
45

56
const postcodeRegExp = /^([A-Za-z][A-Ha-hJ-Yj-y]?\d[A-Za-z0-9]? ?\d[A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$/ // https://stackoverflow.com/a/51885364
67
const captchaSiteKey = config.captchaSiteKey
78

9+
// Put these somewhere more sensible
10+
const captchaVerifyUrl = 'https://global.frcapi.com/api/v2/captcha/siteverify'
11+
812
const 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',

server/views/smell/exceeded-attempts.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ <h1 class="govuk-heading-l">
1010
You have made too many searches
1111
</h1>
1212

13-
<p>You cannot search for more addresses right now.</p>
13+
<p class="govuk-body">You cannot search for more addresses right now.</p>
1414
<p class="govuk-body">
1515
<a href="{{ enterAddress }}" class="govuk-link">Enter address manually</a>
1616
</p>

0 commit comments

Comments
 (0)