-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
79 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule GraphQl.Middleware.Captcha do | ||
@behaviour Absinthe.Middleware | ||
import Absinthe.Resolution, only: [put_result: 2] | ||
|
||
def call(%{arguments: %{captcha: captcha}} = res, _) when is_binary(captcha) do | ||
case Recaptcha.verify(captcha) |> IO.inspect() do | ||
{:ok, _} -> res | ||
_ -> put_result(res, {:error, "captcha validation failed"}) | ||
end | ||
end | ||
def call(res, _), do: res | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,51 @@ defmodule GraphQl.UserMutationTest do | |
assert found["jwt"] | ||
end | ||
|
||
test "it will validate captchas" do | ||
{:ok, user} = Users.create_user(%{ | ||
name: "Michael Guarino", | ||
email: "[email protected]", | ||
password: "super strong password" | ||
}) | ||
|
||
{:ok, %{data: %{"login" => found}}} = run_query(""" | ||
mutation Login($email: String!, $password: String!, $captcha: String!) { | ||
login(email: $email, password: $password, captcha: $captcha) { | ||
id | ||
jwt | ||
} | ||
} | ||
""", %{ | ||
"email" => "[email protected]", | ||
"password" => "super strong password", | ||
"captcha" => "valid_response" | ||
}, %{origin: "https://app.plural.sh"}) | ||
|
||
assert found["id"] == user.id | ||
assert found["jwt"] | ||
end | ||
|
||
test "it will fail on invalid captchas" do | ||
{:ok, user} = Users.create_user(%{ | ||
name: "Michael Guarino", | ||
email: "[email protected]", | ||
password: "super strong password" | ||
}) | ||
|
||
{:ok, %{errors: [_ | _]}} = run_query(""" | ||
mutation Login($email: String!, $password: String!, $captcha: String!) { | ||
login(email: $email, password: $password, captcha: $captcha) { | ||
id | ||
jwt | ||
} | ||
} | ||
""", %{ | ||
"email" => "[email protected]", | ||
"password" => "super strong password", | ||
"captcha" => "invalid_response" | ||
}, %{origin: "https://app.plural.sh"}) | ||
end | ||
|
||
test "invalid origins fail" do | ||
{:ok, _} = Users.create_user(%{ | ||
name: "Michael Guarino", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,3 +93,7 @@ config :core, | |
sysbox_emails: ["[email protected]"], | ||
cockroach_parameters: [sslmode: "allow"], | ||
bootstrap_ssl: false | ||
|
||
config :recaptcha, | ||
http_client: Recaptcha.Http.MockClient, | ||
secret: "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters