Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sajmoon/mellon
Authentication module for Plug applications
https://github.com/sajmoon/mellon
Last synced: 4 days ago
JSON representation
Authentication module for Plug applications
- Host: GitHub
- URL: https://github.com/sajmoon/mellon
- Owner: sajmoon
- Created: 2015-04-09T21:04:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-04-07T21:50:01.000Z (over 8 years ago)
- Last Synced: 2024-10-08T17:12:44.007Z (27 days ago)
- Language: Elixir
- Homepage:
- Size: 25.4 KB
- Stars: 16
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - An authentication module for Plug applications. (Framework Components)
- fucking-awesome-elixir - mellon - An authentication module for Plug applications. (Framework Components)
- awesome-elixir - mellon - An authentication module for Plug applications. (Framework Components)
README
Mellon
======[![Build Status](https://travis-ci.org/sajmoon/mellon.svg?branch=master)](https://travis-ci.org/sajmoon/mellon)
An authentication module for Plug applications.
## Intallation
```elixir
defp deps do
[{:mellon, "~> 0.1.1"}]
end
```## How to use
See /examples for a working example.
```elixir
defmodule MyApp do
import Plug.Conn
use Plug.Builderplug Mellon, validator: {MyApp, :validate, []}, header: "X-AUTH"
plug :index
def validate({conn, token}) do
case token do
"ValidToken" -> {:ok, {"userdata"}, conn}
_ -> {:error, [], conn}
enddef index(conn, _opts) do
send_resp(conn, 200, "Secure area")
end
end
```To authenticated for this example using curl you might do the following:
```bash
curl --header "X-AUTH: Token: ValidToken" localhost:4000/hello
```## Configuration
You can configure some parameters while initializing Mellon.**Required**
`validator`: The function that validates the token. Must return {:ok, userdata, conn} if valid and {:error, conn} if not.
**Optional**
`header`: The http header used for tokens. Will default to 'Authorization'.
`block`: Boolean representing if we should return a 401 and stop the chain, if the user is unauthenticated. set block: false, and you can handle displaying a message, oredirecting from the controller.
## Return object from validator
The validator can return some options.All requests that are authenticated should return
```
{:ok, cargo, conn}
```cargo can be any object that you would like to pass along. it will be assigned to the request so you can access it later in your controller.
It will be assigned to `:credentials`. To access it later you could do the following: `conn.assigns[:credentials]`.If authentication fails you should return `{:error, options, conn}`.
Where options is a `Keyword` containing `status:` and `message`.
Both are optional.In case you want a custom Unauthenticated message include `[message: 'Get out of here!']`