recaptcha-0.1.0.3/0000755000000000000000000000000012400663016011767 5ustar0000000000000000recaptcha-0.1.0.3/LICENSE0000644000000000000000000000271312400663016012777 0ustar0000000000000000Copyright (c) 2008 John MacFarlane All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of John MacFarlane nor the names of this software's contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. recaptcha-0.1.0.3/recaptcha.cabal0000644000000000000000000000242312400663016014706 0ustar0000000000000000Name: recaptcha Version: 0.1.0.3 Cabal-Version: >= 1.6 Build-Type: Custom License: BSD3 License-File: LICENSE Copyright: (c) 2008 John MacFarlane Author: John MacFarlane Maintainer: John MacFarlane Stability: alpha Category: Network Tested-With: GHC Homepage: http://github.com/jgm/recaptcha/tree/master Synopsis: Functions for using the reCAPTCHA service in web applications. Description: reCAPTCHA (http://recaptcha.net/) is a service that provides captchas for preventing automated spam in web applications. recaptcha-hs provides functions for using reCAPTCHA in Haskell web applications. Source-repository head type: git location: git://github.com/jgm/recaptcha.git Flag network-uri Description: Get Network.URI from the network-uri package Default: True Library Build-Depends: base < 5, HTTP >= 3001.1.4, xhtml >= 3000.0 if flag(network-uri) Build-Depends: network-uri >= 2.6 && < 2.7, network >= 2.6 else Build-Depends: network >= 2 && < 2.6 Hs-Source-Dirs: . Exposed-Modules: Network.Captcha.ReCaptcha Ghc-Options: -Wall Ghc-Prof-Options: -auto-all recaptcha-0.1.0.3/Setup.lhs0000644000000000000000000000011412400663016013573 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain recaptcha-0.1.0.3/Network/0000755000000000000000000000000012400663016013420 5ustar0000000000000000recaptcha-0.1.0.3/Network/Captcha/0000755000000000000000000000000012400663016014763 5ustar0000000000000000recaptcha-0.1.0.3/Network/Captcha/ReCaptcha.hs0000644000000000000000000001052112400663016017150 0ustar0000000000000000{- Copyright (c) 2008 John MacFarlane All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of John MacFarlane nor the names of this software's contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -} module Network.Captcha.ReCaptcha ( captchaFields , validateCaptcha ) where import Text.XHtml import Network.Browser import Network.HTTP import Network.URI -- | Returns HTML element to be inserted in the form for which a CAPTCHA is wanted. captchaFields :: String -- ^ reCAPTCHA public key -> Maybe String -- ^ @Nothing@ or @Just@ an error message returned by previous validate attempt -> Html captchaFields recaptchaPublicKey mbErrorMsg = (script ! [src (captchaURL "challenge"), thetype "text/javascript"] << noHtml) +++ noscript << [ iframe ! [src (captchaURL "noscript"), height "300", width "500", frameborder 0] << noHtml , br , textarea ! [name "recaptcha_challenge_field", rows "3", cols "40"] << noHtml , input ! [thetype "hidden", name "recaptcha_response_field", value "manual_challenge"] ] where captchaURL s = "https://www.google.com/recaptcha/api/" ++ s ++ "?k=" ++ recaptchaPublicKey ++ case mbErrorMsg of Just e -> "?error=" ++ e Nothing -> "" -- | Verify a CAPTCHA. validateCaptcha :: String -- ^ reCAPTCHA private key -> String -- ^ IP address of the user who solved the CAPTCHA -> String -- ^ value of the recaptcha_challenge_field -> String -- ^ value of the recaptcha_response_field -> IO (Either String ()) -- ^ @Left@ error message, or @Right ()@ for success validateCaptcha recaptchaPrivateKey ipaddress challenge response = do let verifyURIString = "http://www.google.com/recaptcha/api/verify" let verifyURI = case parseURI verifyURIString of Just uri -> uri Nothing -> error $ "Could not parse URI: " ++ verifyURIString let contents = urlEncodeVars [("privatekey", recaptchaPrivateKey), ("remoteip", ipaddress), ("challenge", challenge), ("response", response)] let req = Request { rqURI = verifyURI, rqMethod = POST, rqHeaders = [ Header HdrContentType "application/x-www-form-urlencoded", Header HdrContentLength (show $ length contents) ], rqBody = contents } (_, resp) <- browse (request req) if rspCode resp == (2,0,0) then do let respLines = lines $ rspBody resp if null respLines then return $ Left "response-body-empty" else if head respLines == "true" then return $ Right () else if length respLines >= 2 then return $ Left $ respLines !! 1 else return $ Left "no-error-message" else return $ Left "response-code-not-200"