forked from martinrehfeld/elli_basicauth
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
623 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
ebin | ||
deps | ||
.rebar3 | ||
_* | ||
.eunit | ||
*.o | ||
*.beam | ||
*.plt | ||
*.swp | ||
*.swo | ||
.erlang.cookie | ||
ebin | ||
log | ||
erl_crash.dump | ||
.rebar | ||
logs | ||
_build | ||
.idea |
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,18 @@ | ||
sudo: false | ||
language: erlang | ||
install: true | ||
before_script: | ||
- wget https://s3.amazonaws.com/rebar3/rebar3 | ||
- chmod +x rebar3 | ||
env: PATH=$PATH:. | ||
cache: | ||
directories: | ||
- $HOME/.cache/rebar3/ | ||
otp_release: | ||
- 19.3 | ||
- 19.0 | ||
- 18.3 | ||
- 18.0 | ||
script: | ||
- rebar3 as test do xref, dialyzer, eunit | ||
- rebar3 as test coveralls send |
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,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2013 Martin Rehfeld | ||
Copyright (c) 2018 elli-lib team | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -1,40 +1,68 @@ | ||
# Basic Authentication Middleware for Elli | ||
# elli_basicauth | ||
|
||
Use it together with the [Elli webserver](https://github.com/knutin/elli) | ||
like this: | ||
*Basic authentication middleware for [elli][]* | ||
|
||
[![Hex.pm][hex badge]][hex package] | ||
[![Erlang][erlang badge]][erlang downloads] | ||
[![Travis CI][travis badge]][travis builds] | ||
[![Coverage Status][coveralls badge]][coveralls link] | ||
|
||
[elli]: https://github.com/elli-lib/elli | ||
[hex badge]: https://img.shields.io/hexpm/v/elli_basicauth.svg | ||
[hex package]: https://hex.pm/packages/elli_basicauth | ||
[erlang badge]: https://img.shields.io/badge/erlang-%E2%89%A518.0-red.svg | ||
[erlang downloads]: http://www.erlang.org/downloads | ||
[travis builds]: https://travis-ci.org/elli-lib/elli_basicauth | ||
[travis badge]: https://travis-ci.org/elli-lib/elli_basicauth.svg | ||
[coveralls badge]: https://coveralls.io/repos/github/elli-lib/elli_basicauth/badge.svg?branch=develop | ||
[coveralls link]: https://coveralls.io/github/elli-lib/elli_basicauth?branch=develop | ||
[license badge]: https://img.shields.io/hexpm/l/elli_basicauth.svg | ||
|
||
|
||
## Installation | ||
|
||
```erlang | ||
-module(my_elli_stuff). | ||
-export([start_link/0, auth_fun/3]). | ||
|
||
start_link() -> | ||
BasicauthConfig = [ | ||
{auth_fun, fun my_elli_stuff:auth_fun/3}, | ||
{auth_realm, <<"Admin Area">>} % optional | ||
], | ||
|
||
Config = [ | ||
{mods, [ | ||
{elli_basicauth, BasicauthConfig}, | ||
{elli_example_callback, []} | ||
]} | ||
], | ||
|
||
elli:start_link([{callback, elli_middleware}, | ||
{callback_args, Config}]). | ||
|
||
|
||
auth_fun(Req, User, Password) -> | ||
case elli_request:path(Req) of | ||
[<<"protected">>] -> password_check(User, Password); | ||
_ -> ok | ||
end. | ||
|
||
|
||
password_check(User, Password) -> | ||
case {User, Password} of | ||
{undefined, undefined} -> unauthorized; | ||
{<<"admin">>, <<"secret">>} -> ok; | ||
{User, Password} -> forbidden | ||
end. | ||
{deps, [ | ||
{elli, "2.0.2"}, | ||
{elli_basicauth, "0.1.0"} | ||
]}. | ||
``` | ||
|
||
|
||
## Example | ||
|
||
- Start an Erlang shell with elli and elli_basicauth loaded. | ||
|
||
```fish | ||
rebar3 as test shell | ||
``` | ||
|
||
- Start [elli_basicauth_example](./test/elli_basicauth_example.erl). | ||
|
||
```erlang | ||
1> {ok, Pid} = elli_basicauth_example:start_link(). | ||
``` | ||
|
||
- Make requests, e.g. using [HTTPie](https://httpie.org/). | ||
```fish | ||
http :8080/protected | ||
``` | ||
```http | ||
HTTP/1.1 401 Unauthorized | ||
Connection: Keep-Alive | ||
Content-Length: 12 | ||
WWW-Authenticate: Basic realm="Admin Area" | ||
Unauthorized | ||
``` | ||
|
||
```fish | ||
http -a user:pass :8080/protected | ||
``` | ||
```http | ||
HTTP/1.1 403 Forbidden | ||
Connection: Keep-Alive | ||
Content-Length: 9 | ||
Forbidden | ||
``` |
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,11 @@ | ||
|
||
|
||
# The elli_basicauth application # | ||
|
||
|
||
## Modules ## | ||
|
||
|
||
<table width="100%" border="0" summary="list of modules"> | ||
<tr><td><a href="elli_basicauth.md" class="module">elli_basicauth</a></td></tr></table> | ||
|
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,3 @@ | ||
%% encoding: UTF-8 | ||
{application,elli_basicauth}. | ||
{modules,[elli_basicauth]}. |
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,129 @@ | ||
|
||
|
||
# Module elli_basicauth # | ||
* [Description](#description) | ||
* [Data Types](#types) | ||
* [Function Index](#index) | ||
* [Function Details](#functions) | ||
|
||
Elli basicauth middleware. | ||
|
||
Copyright (c) 2013, Martin Rehfeld; 2018, elli-lib team | ||
|
||
This middleware provides basic authentication to protect | ||
requests, based on a user-configured authentication function. | ||
|
||
__Behaviours:__ [`elli_handler`](https://github.com/elli-lib/elli/blob/develop/doc/elli_handler.md). | ||
|
||
__Authors:__ Martin Rehfeld, Eric Bailey. | ||
|
||
<a name="types"></a> | ||
|
||
## Data Types ## | ||
|
||
|
||
|
||
|
||
### <a name="type-auth_fun">auth_fun()</a> ### | ||
|
||
|
||
__abstract datatype__: `auth_fun()` | ||
|
||
A user-configurable authentication function. | ||
|
||
|
||
|
||
### <a name="type-auth_status">auth_status()</a> ### | ||
|
||
|
||
__abstract datatype__: `auth_status()` | ||
|
||
The result of an <code><a href="#type-auth_fun">auth_fun()</a></code>. | ||
|
||
|
||
|
||
### <a name="type-config">config()</a> ### | ||
|
||
|
||
__abstract datatype__: `config()` | ||
|
||
A property list of options. | ||
The configurable options are: | ||
|
||
|
||
|
||
<dt><code>auth_fun</code></dt> | ||
|
||
|
||
|
||
|
||
<dd>An <code><a href="#type-auth_fun">auth_fun()</a></code></dd> | ||
|
||
|
||
|
||
|
||
<dt><code>auth_realm</code></dt> | ||
|
||
|
||
|
||
|
||
<dd>A binary <a href="https://tools.ietf.org.md/rfc1945#section-11">realm</a>.</dd> | ||
|
||
|
||
|
||
|
||
|
||
### <a name="type-credentials">credentials()</a> ### | ||
|
||
|
||
<pre><code> | ||
credentials() = {undefined, undefined} | {Username::binary(), Password::binary()} | ||
</code></pre> | ||
|
||
<a name="index"></a> | ||
|
||
## Function Index ## | ||
|
||
|
||
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#default_auth_fun-2">default_auth_fun/2</a></td><td>Default to <code>forbidden</code>, in case of missing <code>auth_fun</code> config.</td></tr><tr><td valign="top"><a href="#handle-2">handle/2</a></td><td>Protect <code>Req</code> based on the configured <code>auth_fun</code>.</td></tr><tr><td valign="top"><a href="#handle_event-3">handle_event/3</a></td><td>No-op to satisfy the <code>elli_handler</code> behaviour.</td></tr></table> | ||
|
||
|
||
<a name="functions"></a> | ||
|
||
## Function Details ## | ||
|
||
<a name="default_auth_fun-2"></a> | ||
|
||
### default_auth_fun/2 ### | ||
|
||
<pre><code> | ||
default_auth_fun(Req, Credentials) -> AuthStatus | ||
</code></pre> | ||
|
||
<ul class="definitions"><li><code>Req = <a href="http://raw.github.com/elli-lib/elli/develop/doc/elli.md#type-req">elli:req()</a></code></li><li><code>Credentials = <a href="#type-credentials">credentials()</a></code></li><li><code>AuthStatus = <a href="#type-auth_status">auth_status()</a></code></li></ul> | ||
|
||
Default to `forbidden`, in case of missing `auth_fun` config. | ||
|
||
<a name="handle-2"></a> | ||
|
||
### handle/2 ### | ||
|
||
<pre><code> | ||
handle(Req::<a href="http://raw.github.com/elli-lib/elli/develop/doc/elli.md#type-req">elli:req()</a>, Config::<a href="#type-config">config()</a>) -> <a href="http://raw.github.com/elli-lib/elli/develop/doc/elli_handler.md#type-result">elli_handler:result()</a> | ||
</code></pre> | ||
<br /> | ||
|
||
Protect `Req` based on the configured `auth_fun`. | ||
If none is given, the default authentication is `forbidden`. | ||
|
||
<a name="handle_event-3"></a> | ||
|
||
### handle_event/3 ### | ||
|
||
<pre><code> | ||
handle_event(Event::<a href="http://raw.github.com/elli-lib/elli/develop/doc/elli_handler.md#type-event">elli_handler:event()</a>, Args::list(), Config::<a href="#type-config">config()</a>) -> ok | ||
</code></pre> | ||
<br /> | ||
|
||
No-op to satisfy the `elli_handler` behaviour. Return `ok`. | ||
|
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,55 @@ | ||
/* standard EDoc style sheet */ | ||
body { | ||
font-family: Verdana, Arial, Helvetica, sans-serif; | ||
margin-left: .25in; | ||
margin-right: .2in; | ||
margin-top: 0.2in; | ||
margin-bottom: 0.2in; | ||
color: #000000; | ||
background-color: #ffffff; | ||
} | ||
h1,h2 { | ||
margin-left: -0.2in; | ||
} | ||
div.navbar { | ||
background-color: #add8e6; | ||
padding: 0.2em; | ||
} | ||
h2.indextitle { | ||
padding: 0.4em; | ||
background-color: #add8e6; | ||
} | ||
h3.function,h3.typedecl { | ||
background-color: #add8e6; | ||
padding-left: 1em; | ||
} | ||
div.spec { | ||
margin-left: 2em; | ||
background-color: #eeeeee; | ||
} | ||
a.module { | ||
text-decoration:none | ||
} | ||
a.module:hover { | ||
background-color: #eeeeee; | ||
} | ||
ul.definitions { | ||
list-style-type: none; | ||
} | ||
ul.index { | ||
list-style-type: none; | ||
background-color: #eeeeee; | ||
} | ||
|
||
/* | ||
* Minor style tweaks | ||
*/ | ||
ul { | ||
list-style-type: square; | ||
} | ||
table { | ||
border-collapse: collapse; | ||
} | ||
td { | ||
padding: 3 | ||
} |
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,22 @@ | ||
[ | ||
{ | ||
elvis, | ||
[ | ||
{config, | ||
[#{dirs => ["src"], | ||
filter => "*.erl", | ||
ruleset => erl_files | ||
}, | ||
#{dirs => ["."], | ||
filter => "rebar.config", | ||
ruleset => rebar_config | ||
}, | ||
#{dirs => ["."], | ||
filter => "elvis.config", | ||
ruleset => elvis_config | ||
} | ||
] | ||
} | ||
] | ||
} | ||
]. |
Oops, something went wrong.