Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/captcha-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ captchaProvider: {
}
},
```

## hCaptcha

```
captchaProvider: {
name: 'hcaptcha',
config: {
secretKey: 'your-key',
minimumScore: 0.5
}
},
```
55 changes: 55 additions & 0 deletions server/services/captcha-providers/hcaptcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'
const axios = require('axios')

module.exports = ({strapi}) => ({
async validate(token) {
if (!token) {
strapi.log.error('Missing hCaptcha Token')
return {
valid: false,
message: 'Missing token',
code: 400
}
}
const secret_key = strapi.config.get('plugin.ezforms.captchaProvider.config.secretKey')
const url = `https://api.hcaptcha.com/siteverify?secret=${secret_key}&response=${token}`
let hcaptcha_verify
try {
hcaptcha_verify = await axios.post(url)
} catch (e) {
strapi.log.error(e)
return {

valid: false,
message: 'Unable to verify captcha',
code: 500

}
}

if (!hcaptcha_verify.data.success) {
strapi.log.error('hcaptcha_verify')
strapi.log.error(hcaptcha_verify)
return {
valid: false,
message: 'Unable to verify captcha',
code: 500
}
}

if (hcaptcha_verify.data.score < strapi.config.get('plugin.ezforms.captchaProvider.config.score')) {
return {

valid: false,
message: 'Score Not High Enough',
code: 400

}
}
return {
score: hcaptcha_verify.data.score,
valid: true
}
},
})

2 changes: 2 additions & 0 deletions server/services/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict'

const recaptcha = require('./captcha-providers/recaptcha')
const hcaptcha = require('./captcha-providers/hcaptcha')
const email = require('./notification-providers/email')
const twilio = require('./notification-providers/twilio')
const formatData = require('./utils/formatData')

module.exports = {
recaptcha,
hcaptcha,
email,
twilio,
formatData
Expand Down
105 changes: 105 additions & 0 deletions tests/hcaptcha.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const hcaptcha = require('../server/services/captcha-providers/hcaptcha')
const axios = require('axios')


describe('hCaptcha Captcha Provider', function () {
let strapi

beforeEach(async function () {
strapi = {
config: {
get: jest.fn()
},
log: {
error: jest.fn()
}
}

})

test('should return error if no token is provided', async function () {
let result = await hcaptcha({strapi}).validate()

expect(result).toEqual({
valid: false,
message: 'Missing token',
code: 400
})

})
test('should return error if captcha post failed', async function () {

jest.spyOn(axios, 'post').mockRejectedValueOnce(new Error('Unable to verify captcha'))

let result = await hcaptcha({strapi}).validate('fakeToken')

await expect(result).toEqual({
valid: false,
message: 'Unable to verify captcha',
code: 500
})

})

test('should return error if captcha is unsuccessful', async function () {

jest.spyOn(axios, 'post').mockResolvedValueOnce({
data: {
success: false,
}
})

let result = await hcaptcha({strapi}).validate('fakeToken')

await expect(result).toEqual({
valid: false,
message: 'Unable to verify captcha',
code: 500
})

})
test('should reject due to low score', async function () {

jest.spyOn(axios, 'post').mockResolvedValueOnce({
data: {
success: true,
score: 0.4,
}
})
strapi.config.get = jest.fn(() => {
return .5
})

let result = await hcaptcha({strapi}).validate('fakeToken')

await expect(result).toEqual({

valid: false,
message: 'Score Not High Enough',
code: 400

})

})
test('should be valid captcha', async function () {

jest.spyOn(axios, 'post').mockResolvedValueOnce({
data: {
success: true,
score: 0.8,
}
})
strapi.config.get = jest.fn(() => {
return .5
})

let result = await hcaptcha({strapi}).validate('fakeToken')

await expect(result).toEqual({
score: .8,
valid: true
})

})

})