-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.iced
81 lines (65 loc) · 2.2 KB
/
index.iced
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
https = require 'https'
querystring = require 'querystring'
module.exports = exports = class NoCaptcha
constructor: (publicKey, privateKey, isSecure) ->
@PUBLIC_KEY = publicKey
@PRIVATE_KEY = privateKey
@IS_SECURE = isSecure? is yes
toHTML: (options)->
script_src = "www.google.com/recaptcha/api.js"
script_src = (if @IS_SECURE then 'https://' else 'http://') + script_src
div = "<div class='g-recaptcha' data-sitekey='#{@PUBLIC_KEY}'"
if options?
exOptions = {}
if options.onload
exOptions.options = options.onload
if options.render
exOptions.options = options.render
if options.hl
exOptions.options = options.hl
external = querystring.stringify exOptions
script_src = script_src+'?'+external
if options.theme is 'dark' or options.theme is 'light'
div = div+" data-theme='#{options.theme}'"
if options.type is 'audio' or options.type is 'image'
div = div+" data-type='#{options.type}'"
if options.size is 'normal' or options.size is 'compact'
div = div+" data-size='#{options.size}'"
if options.tabindex
div = div+" data-tabindex='#{options.tabindex}'"
if options.callback
div = div+" data-callback='#{options.callback}'"
if options["expired-callback"]
div = div+" data-expired-callback='"+options["expired-callback"]+"'"
div = div + "></div>"
script = "<script src='#{script_src}' async defer></script>"
return script+div
verify: ({response, remoteip}, callback)->
if not callback
return new Error 'No callback'
if not response?
return callback new Error 'No response'
externalObj =
secret: @PRIVATE_KEY
response: response
if remoteip
externalObj["remoteip"] = remoteip
external = querystring.stringify externalObj
reqOption = {
host: 'www.google.com'
path: '/recaptcha/api/siteverify?'+external
port: 443
}
request = https.get("https://www.google.com/recaptcha/api/siteverify?"+external, (resp)->
data = ''
resp.on 'data', (chunk)->
data += chunk
resp.on 'end', ()->
res = JSON.parse data
if res.success is true
return callback null, res
else
return callback new Error(res['error-codes'] or "Invalide response")
)
.on 'error', (err)->
return callback err