furance.recaptcha: update to reCAPTCHA2

handle-patch-and-put
Benjamin Pollack 2018-08-14 20:07:04 -04:00
parent b246c06345
commit 84d480700c
6 changed files with 25 additions and 48 deletions

View File

@ -1 +1,2 @@
Doug Coleman Doug Coleman
Benjamin Pollack

View File

@ -20,8 +20,8 @@ TUPLE: recaptcha-app < dispatcher recaptcha ;
: <test-recaptcha> ( responder -- recaptcha ) : <test-recaptcha> ( responder -- recaptcha )
<recaptcha> <recaptcha>
"concatenative.org" >>domain "concatenative.org" >>domain
"6LeJWQgAAAAAAFlYV7SuBClE9uSpGtV_ZS-qVON7" >>public-key "6LeJWQgAAAAAAFlYV7SuBClE9uSpGtV_ZS-qVON7" >>site-key
"6LeJWQgAAAAAALh-XJgSSQ6xKygRgJ8-029Ip2Xv" >>private-key ; "6LeJWQgAAAAAALh-XJgSSQ6xKygRgJ8-029Ip2Xv" >>secret-key ;
: <recaptcha-app> ( -- obj ) : <recaptcha-app> ( -- obj )
\ recaptcha-app new-dispatcher \ recaptcha-app new-dispatcher

View File

@ -1,5 +1,5 @@
<?xml version='1.0' ?> <?xml version='1.0' ?>
<!DOCTYPE html> <!DOCTYPE html>
<t:chloe xmlns:t="http://factorcode.org/chloe/1.0"> <t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
<html><body><t:form t:action=""><t:recaptcha/></t:form></body></html> <html><body><t:form t:action=""><t:recaptcha/><input type="SUBMIT" value="Done" /></t:form></body></html>
</t:chloe> </t:chloe>

View File

@ -36,7 +36,7 @@ $nl
ARTICLE: "furnace.recaptcha" "Recaptcha support for Furnace" ARTICLE: "furnace.recaptcha" "Recaptcha support for Furnace"
"The " { $vocab-link "furnace.recaptcha" } " vocabulary implements support for the recaptcha. Recaptcha is a web service that provides the user with a captcha, a test that is easy to solve by visual inspection, but hard to solve by writing a computer program. Use a captcha to protect forms from abusive users." $nl "The " { $vocab-link "furnace.recaptcha" } " vocabulary implements support for the recaptcha. Recaptcha is a web service that provides the user with a captcha, a test that is easy to solve by visual inspection, but hard to solve by writing a computer program. Use a captcha to protect forms from abusive users." $nl
"The recaptcha responder is a " { $link filter-responder } " that wraps another responder. Set the " { $slot "domain" } ", " { $slot "public-key" } ", and " { $slot "private-key" } " slots of this responder to your recaptcha account information." $nl "The recaptcha responder is a " { $link filter-responder } " that wraps another responder. Set the " { $slot "domain" } ", " { $slot "site-key" } ", and " { $slot "secret-key" } " slots of this responder to your recaptcha account information." $nl
"Wrapping a responder with recaptcha support:" "Wrapping a responder with recaptcha support:"
{ $subsections <recaptcha> } { $subsections <recaptcha> }

View File

@ -1,10 +1,5 @@
USING: furnace.recaptcha.private tools.test urls ; USING: furnace.recaptcha.private tools.test urls ;
IN: furnace.recaptcha.tests IN: furnace.recaptcha.tests
{ { t f } [ "{\"success\": true, \"challenge_ts\": \"2018-09-14T21:12:17Z\", \"hostname\": \"localhost\"}" parse-recaptcha-response ] unit-test
URL" http://www.google.com/recaptcha/api/challenge" { f { "invalid-input-secret" } } [ "{\"success\": false, \"error-codes\": [\"invalid-input-secret\"]}" parse-recaptcha-response ] unit-test
URL" https://www.google.com/recaptcha/api/challenge"
} [
f recaptcha-url
t recaptcha-url
] unit-test

View File

@ -1,13 +1,13 @@
! Copyright (C) 2009 Doug Coleman. ! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors furnace.actions furnace.redirection html.forms USING: accessors assocs furnace.actions furnace.conversations
html.templates.chloe.compiler html.templates.chloe.syntax furnace.redirection html.forms html.templates.chloe.compiler
http.client http.server http.server.filters io.sockets kernel html.templates.chloe.syntax http.client http.server
locals namespaces sequences splitting urls validators http.server.filters io.sockets json.reader kernel locals
xml.syntax furnace.conversations ; namespaces sequences splitting urls validators xml.syntax ;
IN: furnace.recaptcha IN: furnace.recaptcha
TUPLE: recaptcha < filter-responder domain public-key private-key ; TUPLE: recaptcha < filter-responder domain secret-key site-key ;
SYMBOL: recaptcha-error SYMBOL: recaptcha-error
@ -21,61 +21,42 @@ M: recaptcha call-responder*
<PRIVATE <PRIVATE
: (render-recaptcha) ( url -- xml ) : render-recaptcha ( recaptcha -- xml )
dup site-key>> [XML
[XML
<script type="text/javascript" <script type="text/javascript"
src=<->> src="https://www.google.com/recaptcha/api.js" async="async" defer="defer">
</script> </script>
<noscript> <div class="g-recaptcha" data-sitekey=<->></div>
<iframe src=<->
height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge"/>
</noscript>
XML] ; XML] ;
: recaptcha-url ( secure? -- ? )
"https" "http" ? "://www.google.com/recaptcha/api/challenge" append
recaptcha-error cget [ "?error=" glue ] when* >url ;
: render-recaptcha ( -- xml )
secure-connection? recaptcha-url
recaptcha get public-key>> "k" set-query-param (render-recaptcha) ;
: parse-recaptcha-response ( string -- valid? error ) : parse-recaptcha-response ( string -- valid? error )
"\n" split first2 [ "true" = ] dip ; json> [ "success" of ] [ "error-codes" of ] bi ;
:: (validate-recaptcha) ( challenge response recaptcha -- valid? error ) :: (validate-recaptcha) ( response recaptcha -- valid? error )
recaptcha private-key>> :> private-key recaptcha secret-key>> :> secret-key
remote-address get host>> :> remote-ip remote-address get host>> :> remote-ip
H{ H{
{ "challenge" challenge }
{ "response" response } { "response" response }
{ "privatekey" private-key } { "secret" secret-key }
{ "remoteip" remote-ip } { "remoteip" remote-ip }
} URL" http://api-verify.recaptcha.net/verify" } URL" https://www.google.com/recaptcha/api/siteverify"
http-post nip parse-recaptcha-response ; http-post nip parse-recaptcha-response ;
: validate-recaptcha-params ( -- ) : validate-recaptcha-params ( -- )
{ {
{ "recaptcha_challenge_field" [ v-required ] } { "g-recaptcha-response" [ v-required ] }
{ "recaptcha_response_field" [ v-required ] }
} validate-params ; } validate-params ;
PRIVATE> PRIVATE>
CHLOE: recaptcha drop [ render-recaptcha ] [xml-code] ; CHLOE: recaptcha drop [ recaptcha get render-recaptcha ] [xml-code] ;
: validate-recaptcha ( -- ) : validate-recaptcha ( -- )
begin-conversation begin-conversation
validate-recaptcha-params validate-recaptcha-params
"recaptcha_challenge_field" value "g-recaptcha-response" value
"recaptcha_response_field" value
recaptcha get recaptcha get
(validate-recaptcha) (validate-recaptcha)
recaptcha-error cset recaptcha-error cset