From fb95cada584198498cdd57456cf415d2a04af4f2 Mon Sep 17 00:00:00 2001 From: Guilherme Duarte Date: Sat, 20 Nov 2021 20:01:01 +0000 Subject: [PATCH] Add recaptcha v3 support --- lib/recaptcha.ex | 18 +++++++++++++++++- lib/recaptcha/http.ex | 5 ++++- lib/recaptcha/http/mock_http_client.ex | 4 +++- lib/recaptcha/response.ex | 4 ++-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/recaptcha.ex b/lib/recaptcha.ex index 41661b4..69beb5d 100644 --- a/lib/recaptcha.ex +++ b/lib/recaptcha.ex @@ -1,6 +1,6 @@ defmodule Recaptcha do @moduledoc """ - A module for verifying reCAPTCHA version 2.0 response strings. + A module for verifying reCAPTCHA response strings. See the [documentation](https://developers.google.com/recaptcha/docs/verify) for more details. @@ -41,6 +41,22 @@ defmodule Recaptcha do {:ok, %{"success" => false, "error-codes" => errors}} -> {:error, Enum.map(errors, &atomise_api_error/1)} + {:ok, + %{ + "success" => true, + "challenge_ts" => timestamp, + "hostname" => host, + "action" => action, + "score" => score + }} -> + {:ok, + %Response{ + challenge_ts: timestamp, + hostname: host, + action: action, + score: score + }} + {:ok, %{"success" => true, "challenge_ts" => timestamp, "hostname" => host}} -> {:ok, %Response{challenge_ts: timestamp, hostname: host}} diff --git a/lib/recaptcha/http.ex b/lib/recaptcha/http.ex index f2734d9..e799bd7 100644 --- a/lib/recaptcha/http.ex +++ b/lib/recaptcha/http.ex @@ -13,7 +13,7 @@ defmodule Recaptcha.Http do @default_verify_url "https://www.google.com/recaptcha/api/siteverify" @doc """ - Sends an HTTP request to the reCAPTCHA version 2.0 API. + Sends an HTTP request to the reCAPTCHA API. See the [docs](https://developers.google.com/recaptcha/docs/verify#api-response) for more details on the API response. @@ -28,6 +28,8 @@ defmodule Recaptcha.Http do "success" => success, "challenge_ts" => ts, "hostname" => host, + "score" => score, + "action" => action, "error-codes" => errors }} = Recaptcha.Http.request_verification(%{ secret: "secret", @@ -55,6 +57,7 @@ defmodule Recaptcha.Http do {:error, :invalid} -> {:error, [:invalid_api_response]} {:error, {:invalid, _reason}} -> {:error, [:invalid_api_response]} {:error, %{reason: reason}} -> {:error, [reason]} + {:error, %{"error-codes" => error_codes}} -> {:error, error_codes} end end end diff --git a/lib/recaptcha/http/mock_http_client.ex b/lib/recaptcha/http/mock_http_client.ex index 4158700..021fdb4 100644 --- a/lib/recaptcha/http/mock_http_client.ex +++ b/lib/recaptcha/http/mock_http_client.ex @@ -17,7 +17,9 @@ defmodule Recaptcha.Http.MockClient do %{ "success" => true, "challenge_ts" => "timestamp", - "hostname" => "localhost" + "hostname" => "localhost", + "score" => 1.0, + "action" => "mock" }} end diff --git a/lib/recaptcha/response.ex b/lib/recaptcha/response.ex index 2d190a1..6702a3e 100644 --- a/lib/recaptcha/response.ex +++ b/lib/recaptcha/response.ex @@ -2,7 +2,7 @@ defmodule Recaptcha.Response do @moduledoc """ A struct representing the successful recaptcha response from the reCAPTCHA API. """ - defstruct challenge_ts: "", hostname: "" + defstruct challenge_ts: "", hostname: "", action: "", score: 0.0 - @type t :: %__MODULE__{challenge_ts: String.t(), hostname: String.t()} + @type t :: %__MODULE__{challenge_ts: String.t(), hostname: String.t(), action: String.t(), score: Float.t()} end