-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (35 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict'
const request = require('request-promise-native')
const URL = 'https://www.google.com/recaptcha/api/siteverify'
class Recaptcha {
constructor(secretKey, proxy) {
this._secretkey = secretKey
this._proxy = proxy || null
}
validate(token) {
let requestObject = {
uri: URL,
form: {
secret: this._secretkey,
response: token
}
}
if (this._proxy) {
let auth = this._proxy.username ? `${this._proxy.username}:${this._proxy.password}@` : ''
let port = this._proxy.port ? `:${this._proxy.port}` : ''
let protocol = (this._proxy.protocol || 'http') + '://'
requestObject.proxy = `${protocol}${auth}${this._proxy.host}${port}`
}
return request.post(requestObject).then(response => {
let parsed = JSON.parse(response)
if (!parsed.success) {
let errorCodes = parsed['error-codes']
let error = new Error('failed to verify: ' + errorCodes.join(','))
error.errorCodes = errorCodes
return Promise.reject(error)
}
return true
})
}
}
module.exports = Recaptcha