|
| 1 | +# Copyright Thomas T. Jarløv (TTJ) - ttj@ttj.dk |
| 2 | + |
| 3 | +import |
| 4 | + std/[ |
| 5 | + httpclient, |
| 6 | + json, |
| 7 | + os |
| 8 | + ] |
| 9 | + |
| 10 | + |
| 11 | +const |
| 12 | + VerifyUrl: string = "https://www.google.com/recaptcha/api/siteverify" |
| 13 | + |
| 14 | +type |
| 15 | + ReCaptcha* = object |
| 16 | + secret: string |
| 17 | + siteKey: string |
| 18 | + CaptchaVerificationError* = object of Exception |
| 19 | + |
| 20 | +proc initReCaptcha*(secret, siteKey: string): ReCaptcha = |
| 21 | + result = ReCaptcha( |
| 22 | + secret: secret, |
| 23 | + siteKey: siteKey |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +proc checkVerification(mpd: MultipartData): bool = |
| 28 | + let |
| 29 | + client = newHttpClient() |
| 30 | + response = client.post(VerifyUrl, multipart=mpd) |
| 31 | + jsonContent = parseJson(response.body) |
| 32 | + success = jsonContent.getOrDefault("success") |
| 33 | + errors = jsonContent.getOrDefault("error-codes") |
| 34 | + |
| 35 | + if errors != nil: |
| 36 | + for err in errors.items(): |
| 37 | + case err.getStr() |
| 38 | + of "missing-input-secret": |
| 39 | + raise newException(CaptchaVerificationError, "The secret parameter is missing.") |
| 40 | + of "invalid-input-secret": |
| 41 | + raise newException(CaptchaVerificationError, "The secret parameter is invalid or malformed.") |
| 42 | + of "missing-input-response": |
| 43 | + raise newException(CaptchaVerificationError, "The response parameter is missing.") |
| 44 | + of "invalid-input-response": |
| 45 | + raise newException(CaptchaVerificationError, "The response parameter is invalid or malformed.") |
| 46 | + else: discard |
| 47 | + |
| 48 | + result = if success != nil: success.getBool() else: false |
| 49 | + |
| 50 | +proc verify*(rc: ReCaptcha, reCaptchaResponse, remoteIp: string): bool = |
| 51 | + let multiPart = newMultipartData({ |
| 52 | + "secret": rc.secret, |
| 53 | + "response": reCaptchaResponse, |
| 54 | + "remoteip": remoteIp |
| 55 | + }) |
| 56 | + result = checkVerification(multiPart) |
| 57 | + |
| 58 | +proc verify*(rc: ReCaptcha, reCaptchaResponse: string): bool = |
| 59 | + let multiPart = newMultipartData({ |
| 60 | + "secret": rc.secret, |
| 61 | + "response": reCaptchaResponse, |
| 62 | + }) |
| 63 | + result = checkVerification(multiPart) |
| 64 | + |
| 65 | + |
| 66 | +proc checkReCaptcha*(antibot, userIP: string): bool = |
| 67 | + let secret = getEnv("GOOGLE_RECAPTCHA_SECRET") |
| 68 | + let siteKey = getEnv("GOOGLE_RECAPTCHA_SITE_KEY") |
| 69 | + |
| 70 | + if secret == "" or siteKey == "": |
| 71 | + return true |
| 72 | + if secret == "NONE" or siteKey == "NONE": |
| 73 | + return true |
| 74 | + |
| 75 | + let captcha = initReCaptcha(secret, siteKey) |
| 76 | + |
| 77 | + |
| 78 | + var captchaValid: bool = false |
| 79 | + try: |
| 80 | + captchaValid = captcha.verify(antibot, userIP) |
| 81 | + except: |
| 82 | + echo("checkReCaptcha(): Error checking captcha. UserIP: " & userIP & " - ErrMsg: " & getCurrentExceptionMsg()) |
| 83 | + return false |
| 84 | + |
| 85 | + return captchaValid |
| 86 | + |
| 87 | + |
0 commit comments