Google Recaptcha v0.2.0 GoogleRecaptcha View Source

Provides access interface to Google Recaptcha API.

GoogleRecaptcha need the API keys to work, which you can generate here.

After create the API keys, you should set the given keys in the config file:

config :google_recaptcha,
  api_url: "https://www.google.com/recaptcha/api/siteverify",
  public_key: "YOUR_PUBLIC_KEY",
  secret_key: "YOUR_SECRET_KEY"

Check the Google Recaptcha Docs for more information

Usage

Verifying captcha response:

# the captcha response is generated by the recaptcha widget.
# When the captcha is correct:
GoogleRecaptcha.verify("correct_captcha_response", "127.0.0.1")
:ok

GoogleRecaptcha.verify("wrong_capcha", "127.0.0.1")
{:error, :invalid_captcha}

Link to this section Summary

Functions

Helper function to check if the captcha is enabled.

Public key to be used in google recaptcha widget.

Check if the given captcha is correct, returns true if is valid, otherwise false.

Check in the Recaptcha API if the given captcha response is correct.

Link to this section Functions

Specs

enabled?() :: boolean()

Helper function to check if the captcha is enabled.

Used for development purpouse, captcha is enabled by default. You can change it overriding the Recaptcha configuration:

# config/dev.exs
config :google_recaptcha, enabled: false

Example

iex> GoogleRecaptcha.enabled?()
false

Specs

public_key() :: String.t()

Public key to be used in google recaptcha widget.

You can set the public key simply setting the recaptcha configuration:

# config/dev.exs
config :google_recaptcha, public_key: "YOUR_PUBLIC_KEY"

For more information how to generate/display the recaptcha widget, check here.

Example

iex> GoogleRecaptcha.public_key()
"..."
Link to this function

valid?(captcha_response, remote_ip \\ nil)

View Source

Specs

valid?(
  captcha_response :: String.t(),
  remote_ip :: :inet.ip_address() | String.t() | nil
) :: boolean()

Check if the given captcha is correct, returns true if is valid, otherwise false.

The captcha_response is usually set in the "g-recaptcha-response" parameter. A remote_ip can be passed to verify based on client IP.

For specific and detailed error, check verify/2.

Examples

captcha_response = params["g-recaptcha-response"]
valid?(captcha_response, "127.0.0.1")
true

# Wrong captcha:
valid?("wrong_capcha", "127.0.0.1")
false
Link to this function

verify(captcha_response, remote_ip \\ nil)

View Source

Specs

verify(
  captcha_response :: String.t(),
  remote_ip :: :inet.ip_address() | String.t() | nil
) ::
  :ok
  | {:error,
     :missing_secret
     | :invalid_secret
     | :missing_captcha
     | :invalid_captcha
     | :captcha_request_failed
     | :captcha_expired
     | :invalid_keys
     | :unmapped_captcha_error
     | :unknown_captcha_error
     | :request_failed
     | :invalid_request_response}

Check in the Recaptcha API if the given captcha response is correct.

Examples

# Doc how to generate the captcha widget: https://developers.google.com/recaptcha/docs/display#auto_render
captcha_response = params["g-recaptcha-response"]
verify(captcha_response, conn.remote_ip)
:ok

# Wrong captcha:
verify("wrong_capcha", "127.0.0.1")
{:error, :invalid_captcha}

# Invalid secret key:
verify("captcha_response", "127.0.0.1")
{:error, :invalid_secret}

# Invalid secret and public keys, or keys does not match.
verify("catpcha_response", "70.1.1.0")
{:error, :invalid_keys}