ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - samueljseay/recaptcha: A simple reCaptcha 2 library for Elixir applications.
A simple reCaptcha 2 library for Elixir applications. - samueljseay/recaptcha
Visit Site

GitHub - samueljseay/recaptcha: A simple reCaptcha 2 library for Elixir applications.

GitHub - samueljseay/recaptcha: A simple reCaptcha 2 library for Elixir applications.

Recaptcha

Build Status Coverage Status Module Version Hex Docs Total Download License Last Updated

A simple Elixir package for implementing reCAPTCHA in Elixir applications.

Migration from 1.x

Breaking Changes

  1. Template functionality is now in a separate module: Recaptcha.Template. Please note: in future templating may move to a Phoenix specific package.
  2. verify API has changed, see the code for documentation of the new API.

Most other questions about 2.x should be answered by looking over the documentation and the code. Please raise an issue if you have any problems with migrating.

Installation

Add :recaptcha to your mix.exs dependencies:

  defp deps do
    [
      {:recaptcha, "~> 3.0"},
    ]
  end

List :recaptcha as an application dependency:

  def application do
    [
      extra_applications: [:recaptcha]
    ]
  end

Run mix do deps.get, compile

Config

By default the public and private keys are loaded via the RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY environment variables.

config :recaptcha,
  public_key: {:system, "RECAPTCHA_PUBLIC_KEY"},
  secret: {:system, "RECAPTCHA_PRIVATE_KEY"}

JSON Decoding

By default reCaptcha will use Jason to decode JSON responses, this can be changed as such:

config :recaptcha, :json_library, Poison

Usage

Render the Widget

Use raw (if you're using Phoenix.HTML) and Recaptcha.Template.display/1 methods to render the captcha widget.

For recaptcha with checkbox:

<form name="someform" method="post" action="/somewhere">
  ...
  <%= raw Recaptcha.Template.display %>
  ...
</form>

For invisible recaptcha:

<form name="someform" method="post" action="/somewhere">
  ...
  <%= raw Recaptcha.Template.display(size: "invisible") %>
</form>
  ...

To change the position of the invisible recaptcha, use an option badge. See https://developers.google.com/recaptcha/docs/invisible on the date-badge.

Since recaptcha loads JavaScript code asynchronously, you cannot immediately submit the captcha form. If you have logic that needs to know if the captcha code has already been loaded (for example disabling submit button until fully loaded), it is possible to pass in a JS-callback that will be called once the captcha has finished loading. This can be done as follows:

<form name="someform" method="post" action="/somewhere">
  ...
  <%= raw Recaptcha.Template.display(onload: "myOnLoadCallback") %>
</form>
  ...

And then in your JS code:

function myOnLoadCallback() {
  // perform extra actions here
}

display method accepts additional options as a keyword list, the options are:

Option Action Default
noscript Renders default noscript code provided by google false
public_key Sets key to the data-sitekey reCaptcha div attribute Public key from the config file
hl Sets the language of the reCaptcha en

Verify API

Recaptcha provides the verify/2 method. Below is an example using a Phoenix controller action:

def create(conn, params) do
  case Recaptcha.verify(params["g-recaptcha-response"]) do
    {:ok, response} -> do_something
    {:error, errors} -> handle_error
  end
end

verify method sends a POST request to the reCAPTCHA API and returns 2 possible values:

{:ok, %Recaptcha.Response{challenge_ts: timestamp, hostname: host}} -> The captcha is valid, see the documentation for more details.

{:error, errors} -> errors contains atomised versions of the errors returned by the API, See the error documentation for more details. Errors caused by timeouts in HTTPoison or Jason encoding are also returned as atoms. If the recaptcha request succeeds but the challenge is failed, a :challenge_failed error is returned.

verify method also accepts a keyword list as the third parameter with the following options:

Option Action Default
timeout Time to wait before timeout 5000 (ms)
secret Private key to send as a parameter of the API request Private key from the config file
remote_ip Optional. The user's IP address, used by reCaptcha no default

Testing

In order to test your endpoints you should set the secret key to the following value in order to receive a positive result from all queries to the Recaptcha engine.

config :recaptcha,
  secret: "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"

Setting up tests without network access can be done also. When configured as such a positive or negative result can be generated locally.

config :recaptcha,
  http_client: Recaptcha.Http.MockClient,
  secret: "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"


  {:ok, _details} = Recaptcha.verify("valid_response")

  {:error, _details} = Recaptcha.verify("invalid_response")

Contributing

Check out CONTRIBUTING.md if you want to help.

Copyright and License

Copyright (c) 2016 Samuel Seay

This library is released under the MIT License. See the LICENSE.md file for further details.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory