factor/basis/furnace/recaptcha/recaptcha.factor

64 lines
1.8 KiB
Factor
Raw Permalink Normal View History

2009-09-16 16:17:15 -04:00
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs furnace.actions furnace.conversations
furnace.redirection html.forms html.templates.chloe.compiler
html.templates.chloe.syntax http.client http.server
http.server.filters io.sockets json.reader kernel locals
namespaces sequences splitting urls validators xml.syntax ;
IN: furnace.recaptcha
2009-09-16 16:17:15 -04:00
TUPLE: recaptcha < filter-responder domain secret-key site-key ;
2009-09-16 16:17:15 -04:00
SYMBOL: recaptcha-error
2009-09-16 16:17:15 -04:00
: <recaptcha> ( responder -- recaptcha )
2009-09-16 16:17:15 -04:00
recaptcha new
swap >>responder ;
M: recaptcha call-responder*
dup recaptcha set
2009-09-16 16:17:15 -04:00
responder>> call-responder ;
<PRIVATE
: render-recaptcha ( recaptcha -- xml )
site-key>> [XML
2010-03-11 01:13:57 -05:00
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js" async="async" defer="defer">
2010-03-11 01:13:57 -05:00
</script>
<div class="g-recaptcha" data-sitekey=<->></div>
2010-03-11 01:13:57 -05:00
XML] ;
2009-09-16 16:17:15 -04:00
: parse-recaptcha-response ( string -- valid? error )
json> [ "success" of ] [ "error-codes" of ] bi ;
2009-09-16 16:17:15 -04:00
:: (validate-recaptcha) ( response recaptcha -- valid? error )
recaptcha secret-key>> :> secret-key
2009-09-16 16:17:15 -04:00
remote-address get host>> :> remote-ip
H{
{ "response" response }
{ "secret" secret-key }
2009-09-16 16:17:15 -04:00
{ "remoteip" remote-ip }
} URL" https://www.google.com/recaptcha/api/siteverify"
http-post nip parse-recaptcha-response ;
2009-09-16 16:17:15 -04:00
: validate-recaptcha-params ( -- )
{
{ "g-recaptcha-response" [ v-required ] }
} validate-params ;
2009-09-16 16:17:15 -04:00
PRIVATE>
CHLOE: recaptcha drop [ recaptcha get render-recaptcha ] [xml-code] ;
2009-09-16 16:17:15 -04:00
: validate-recaptcha ( -- )
begin-conversation
validate-recaptcha-params
"g-recaptcha-response" value
recaptcha get
(validate-recaptcha)
recaptcha-error cset
[ validation-failed ] unless ;