Current section
7 Versions
Jump to
Current section
7 Versions
Compare versions
4
files changed
+27
additions
-27
deletions
| @@ -2,7 +2,6 @@ | |
| 2 2 | |
| 3 3 | [](https://travis-ci.org/fertapric/access_token) |
| 4 4 | [](http://inch-ci.org/github/fertapric/access_token) |
| 5 | - [](https://beta.hexfaktor.org/github/fertapric/access_token) |
| 6 5 | |
| 7 6 | |
| 8 7 | A plug for extracting the access token from the request. |
| @@ -15,7 +14,7 @@ Add Access Token to your project's dependencies in `mix.exs`: | |
| 15 14 | |
| 16 15 | ```elixir |
| 17 16 | def deps do |
| 18 | - [{:access_token, "~> 0.1.0"}] |
| 17 | + [{:access_token, "~> 0.2.0"}] |
| 19 18 | end |
| 20 19 | ``` |
| 21 20 | |
| @@ -42,21 +41,21 @@ conn.assigns[:access_token] | |
| 42 41 | ### Options |
| 43 42 | |
| 44 43 | * `:param` - The name of the HTTP *request* parameter to check for |
| 45 | - the access token. Default value is "access_token". |
| 44 | + the access token. Default value is `access_token`. |
| 46 45 | |
| 47 46 | ```elixir |
| 48 47 | plug AccessToken, param: "token" |
| 49 48 | ``` |
| 50 49 | |
| 51 50 | * `:http_header` - The name of the HTTP *request* header to check for |
| 52 | - the access token. Default value is "authorization". |
| 51 | + the access token. Default value is `authorization`. |
| 53 52 | |
| 54 53 | ```elixir |
| 55 54 | plug AccessToken, http_header: "custom-authorization" |
| 56 55 | ``` |
| 57 56 | |
| 58 57 | * `:http_header_prefix` - The prefix of the HTTP *request* authorization header. |
| 59 | - Default value is "Bearer". |
| 58 | + Default value is `Bearer`. |
| 60 59 | |
| 61 60 | ```elixir |
| 62 61 | plug AccessToken, http_header_prefix: "Token" |
| @@ -11,5 +11,5 @@ | |
| 11 11 | [[{<<"app">>,<<"plug">>}, |
| 12 12 | {<<"name">>,<<"plug">>}, |
| 13 13 | {<<"optional">>,false}, |
| 14 | - {<<"requirement">>,<<">= 0.0.0">>}]]}. |
| 15 | - {<<"version">>,<<"0.1.0">>}. |
| 14 | + {<<"requirement">>,<<"~> 1.3.3 or ~> 1.4">>}]]}. |
| 15 | + {<<"version">>,<<"0.2.0">>}. |
| @@ -16,19 +16,19 @@ defmodule AccessToken do | |
| 16 16 | ## Options |
| 17 17 | |
| 18 18 | * `:param` - The name of the HTTP *request* parameter to check for |
| 19 | - the access token. Default value is "access_token". |
| 19 | + the access token. Default value is `access_token`. |
| 20 20 | |
| 21 | - plug AccessToken, param: "token" |
| 21 | + plug AccessToken, param: "token" |
| 22 22 | |
| 23 23 | * `:http_header` - The name of the HTTP *request* header to check for |
| 24 | - the access token. Default value is "authorization". |
| 24 | + the access token. Default value is `authorization`. |
| 25 25 | |
| 26 | - plug AccessToken, http_header: "custom-authorization" |
| 26 | + plug AccessToken, http_header: "custom-authorization" |
| 27 27 | |
| 28 28 | * `:http_header_prefix` - The prefix of the HTTP *request* authorization header. |
| 29 | - Default value is "Bearer". |
| 29 | + Default value is `Bearer`. |
| 30 30 | |
| 31 | - plug AccessToken, http_header_prefix: "Token" |
| 31 | + plug AccessToken, http_header_prefix: "Token" |
| 32 32 | |
| 33 33 | * `:assign_to` - The name of the key to assign the access token. |
| 34 34 | Defaults to `:access_token` |
| @@ -39,22 +39,23 @@ defmodule AccessToken do | |
| 39 39 | present. The status can be `nil`, an integer or an atom. The list of allowed atoms |
| 40 40 | is available in `Plug.Conn.Status`. Defaults to `:unauthorized` |
| 41 41 | |
| 42 | - plug AccessToken, error_status: :forbidden |
| 42 | + plug AccessToken, error_status: :bad_request |
| 43 43 | |
| 44 44 | * `:error_handler` - The function to be called in case the access token is not |
| 45 | - present. The `:error_handler` is set using a `{module, function, args}` tuple. |
| 45 | + present. The `:error_handler` is set using a `{module, function, args}` triple. |
| 46 46 | The function will receive the `conn` followed by the list of `args` provided. |
| 47 47 | |
| 48 | - plug AccessToken, |
| 49 | - error_handler: {Phoenix.Controller, :render, [MyAppWeb.ErrorView, "401.json"]} |
| 48 | + plug AccessToken, |
| 49 | + error_handler: {Phoenix.Controller, :render, [MyAppWeb.ErrorView, "401.json"]} |
| 50 50 | """ |
| 51 51 | |
| 52 52 | @behaviour Plug |
| 53 53 | |
| 54 | - def init(opts) do |
| 54 | + def init(opts \\ []) do |
| 55 55 | %{ |
| 56 | - http_header: Keyword.get(opts, :http_header, "authorization"), |
| 57 56 | param: Keyword.get(opts, :param, "access_token"), |
| 57 | + http_header: Keyword.get(opts, :http_header, "authorization"), |
| 58 | + http_header_prefix: Keyword.get(opts, :http_header_prefix, "Bearer"), |
| 58 59 | assign_key: Keyword.get(opts, :assign_to, :access_token), |
| 59 60 | error_status: Keyword.get(opts, :error_status, :unauthorized), |
| 60 61 | error_handler: Keyword.get(opts, :error_handler) |
| @@ -69,19 +70,19 @@ defmodule AccessToken do | |
| 69 70 | |> apply_error_handler(config) |
| 70 71 | |> Plug.Conn.halt() |
| 71 72 | access_token -> |
| 72 | - Plug.Conn.assign(conn, config.assign_key, access_token) |
| 73 | + Plug.Conn.assign(conn, config.assign_key, String.trim(access_token)) |
| 73 74 | end |
| 74 75 | end |
| 75 76 | |
| 76 | - def apply_error_handler(conn, %{error_handler: nil}), do: conn |
| 77 | - def apply_error_handler(conn, %{error_handler: {mod, fun, args}}) do |
| 78 | - apply(mod, fun, [conn] + args) |
| 77 | + defp apply_error_handler(conn, %{error_handler: nil}), do: conn |
| 78 | + defp apply_error_handler(conn, %{error_handler: {mod, fun, args}}) do |
| 79 | + apply(mod, fun, [conn] ++ args) |
| 79 80 | end |
| 80 81 | |
| 81 82 | defp get_access_token_from_headers(conn, config) do |
| 82 83 | with [header] <- Plug.Conn.get_req_header(conn, config.http_header), |
| 83 84 | true <- String.starts_with?(header, config.http_header_prefix) do |
| 84 | - header |> String.replace_leading(config.http_header_prefix) |> String.trim() |
| 85 | + String.replace_leading(header, config.http_header_prefix, "") |
| 85 86 | else |
| 86 87 | _ -> nil |
| 87 88 | end |
| @@ -1,7 +1,7 @@ | |
| 1 1 | defmodule AccessToken.Mixfile do |
| 2 2 | use Mix.Project |
| 3 3 | |
| 4 | - @version "0.1.0" |
| 4 | + @version "0.2.0" |
| 5 5 | |
| 6 6 | def project do |
| 7 7 | [ |
| @@ -25,8 +25,8 @@ defmodule AccessToken.Mixfile do | |
| 25 25 | |
| 26 26 | defp deps do |
| 27 27 | [ |
| 28 | - {:plug, ">= 0.0.0"}, |
| 29 | - {:ex_doc, "~> 0.14", only: [:test, :docs], runtime: false}, |
| 28 | + {:plug, "~> 1.3.3 or ~> 1.4"}, |
| 29 | + {:ex_doc, "~> 0.16", only: [:test, :docs], runtime: false}, |
| 30 30 | {:inch_ex, ">= 0.0.0", only: [:dev, :docs], runtime: false} |
| 31 31 | ] |
| 32 32 | end |