-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhello_recaptcha_test.opa
144 lines (125 loc) · 4.72 KB
/
hello_recaptcha_test.opa
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright © 2011, 2012 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
//Implement [Client.load]
//reCaptcha: api/recaptcha
import stdlib.web.client
bug = {true} //Replace by {false} to obtain something that works
type Recaptcha.success = {captcha_solved}
type Recaptcha.failure =
{captcha_not_reachable}
/ {upstream: string} /**Upstream error, code may be used for debugging purposes.*/
/ {unknown: list(string)}
/ {empty_answer}
type Recaptcha.result = {success: Recaptcha.success} / {failure: Recaptcha.failure}
type Recaptcha.config =
{
private:
{
privkey: string
}
public:
{
pubkey: string
theme: option(string)
}
}
@abstract type Recaptcha.validator = (string, string, (Recaptcha.result -> void) -> void)
Recaptcha =
{{
make(config: Recaptcha.config): (Recaptcha.validator, xhtml) =
(
id = Dom.fresh_id()
xhtml = <div id={id} onready={_ -> onready(id, config.public.pubkey, config.public.theme?"red")}/>
(validator(config.private.privkey), xhtml)
)
validate(validator: Recaptcha.validator, callback: Recaptcha.result -> void): void =
(
(a, b) = get_token()
validator(a, b, callback)
)
@private path_api_uri = Option.get(Parser.try_parse(Uri.uri_parser, "http://www.google.com/recaptcha/api/verify"))
@private path_js = "http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"
@private onready(id: string, pubkey: string, theme: string): void =
(
do Log.info("onready", "start")
Client.Script.load_from_then(path_js, ->
do Log.info("onready", "loaded")
do (%% Recaptcha.init %%)(id, pubkey, theme)
do Log.info("onready", "initialized")
void
)
)
@private validator(privkey: string): Recaptcha.validator =
(
payload(challenge, response, callback:Recaptcha.result -> void) =
(
if String.is_empty(challenge) || String.is_empty(response) then callback({failure = {empty_answer}})
else
(
data = [("privatekey", privkey),
("remoteip", "{HttpRequest.Generic.get_ip(Option.get(thread_context().request))}"),
("challenge", challenge),
("response", response)]
with_result =
(
| {failure = _} -> callback({failure = {captcha_not_reachable}})
| ~{success} ->
details = String.explode("\n", success.content)
match details with
| ["true" | _] -> callback({success = {captcha_solved}})
| ["false", code | _] -> callback({failure = {upstream = code}})
| _ -> callback({failure = {unknown = details}})
)
if bug then WebClient.Post.try_post_with_options_async(path_api_uri,
WebClient.Post.of_form({WebClient.Post.default_options with content = {some = data}}),
with_result) // <= this version doesn't work, callback is called but cannot perform server->client calls
else
with_result(WebClient.Post.try_post_with_options(path_api_uri,
WebClient.Post.of_form({WebClient.Post.default_options with content = {some = data}})))
// <= this version works, callback is called but cannot perform server->client calls
)
)
payload
)
@private get_token(): (string, string) =
(
result = ((%%Recaptcha.get_challenge%%)(), (%%Recaptcha.get_response%%)())
do (%%Recaptcha.destroy%%)()
result
)
}}
config = {
private = {
privkey = "6LeeR8QSAAAAADSnODF238nFuhfPQ13OH9sMib6o"
}
public = {
pubkey = "6LeeR8QSAAAAANEPtPmwgSATS64g1p4qOg0fpEJH"
theme = {some = "red"}
}
}
after_validation =
| {success = _} ->
do Log.info("Success", "success")
Dom.transform([#status <- <>success</>])
| {~failure} ->
do Log.info("Failure", "failure")
Dom.transform([#status <- <>failure</>])
server = one_page_server("Hi", ->
(validator, recaptcha) = Recaptcha.make(config)
<div>
{recaptcha}
<button onclick={_ -> Recaptcha.validate(validator, after_validation)} > Submit </button>
<div id=#status></div>
</div>
)