Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recaptcha v3 support #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/recaptcha.ex
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}}
Expand Down
5 changes: 4 additions & 1 deletion lib/recaptcha/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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",
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/recaptcha/http/mock_http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ defmodule Recaptcha.Http.MockClient do
%{
"success" => true,
"challenge_ts" => "timestamp",
"hostname" => "localhost"
"hostname" => "localhost",
"score" => 1.0,
"action" => "mock"
}}
end

Expand Down
4 changes: 2 additions & 2 deletions lib/recaptcha/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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