Skip to content

Instantly share code, notes, and snippets.

@pedrosancao
Created July 28, 2019 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrosancao/4893654efc2092b360f8093ecb58c3c3 to your computer and use it in GitHub Desktop.
Save pedrosancao/4893654efc2092b360f8093ecb58c3c3 to your computer and use it in GitHub Desktop.
validate captcha and show data for google cloud functions with node
const https = require('https')
const querystring = require('querystring')
exports.showdata = (req, res) => {
const data = '___________________'
const captchaSecret = '________________________________'
const allowedOrigins = ['______________________________']
const verifyData = querystring.stringify({
secret : captchaSecret,
response : req.query.response || req.body.response,
remoteip : req.ip
})
const verifyReq = https.request({
hostname: 'www.google.com',
path: '/recaptcha/api/siteverify',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(verifyData)
}
}, verifyRes => {
let responseData = ''
verifyRes.on('data', d => {
responseData += d
})
verifyRes.on('end', () => {
let response = JSON.parse(responseData)
console.log(response);
if (!response.success) {
res.status(401).send('unauthorized')
} else {
res.status(200).send(data)
}
})
})
verifyReq.on('error', (error) => {
console.error(error)
res.status(401).send('unauthorized')
})
verifyReq.write(verifyData)
verifyReq.end()
if (allowedOrigins.indexOf(req.get('origin')) >= 0) {
res.header('Access-Control-Allow-Origin', '*')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment