0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Convert the basic auth example to a release

This commit is contained in:
Loïc Hoguin 2013-09-07 16:18:51 +02:00
parent 335676876b
commit 51ce122858
5 changed files with 27 additions and 48 deletions

View file

@ -1,24 +1,28 @@
Cowboy Basic Authorization Rest Hello World Basic authorization example using REST
=========================================== ======================================
To compile this example you need rebar in your PATH. To try this example, you need GNU `make`, `git` and
[relx](https://github.com/erlware/relx) in your PATH.
Type the following command: To build the example, run the following command:
```
$ rebar get-deps compile ``` bash
$ make
``` ```
You can then start the Erlang node with the following command: To start the release in the foreground:
```
./start.sh ``` bash
$ ./_rel/bin/hello_world_example console
``` ```
Then run any given command or point your browser to the indicated URL. Then point your browser at [http://localhost:8080](http://localhost:8080).
Examples Example output
-------- --------------
Request with no authentication:
### Get 401
``` bash ``` bash
$ curl -i http://localhost:8080 $ curl -i http://localhost:8080
HTTP/1.1 401 Unauthorized HTTP/1.1 401 Unauthorized
@ -26,10 +30,11 @@ connection: keep-alive
server: Cowboy server: Cowboy
date: Sun, 20 Jan 2013 14:10:27 GMT date: Sun, 20 Jan 2013 14:10:27 GMT
content-length: 0 content-length: 0
www-authenticate: Restricted www-authenticate: Basic realm="cowboy"
``` ```
### Get 200 Request with authentication:
``` bash ``` bash
$ curl -i -u "Alladin:open sesame" http://localhost:8080 $ curl -i -u "Alladin:open sesame" http://localhost:8080
HTTP/1.1 200 OK HTTP/1.1 200 OK

View file

@ -1,4 +0,0 @@
{deps, [
{cowboy, ".*",
{git, "git://github.com/extend/cowboy.git", "master"}}
]}.

View file

@ -1,15 +0,0 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(basic_auth).
%% API.
-export([start/0]).
%% API.
start() ->
ok = application:start(crypto),
ok = application:start(cowlib),
ok = application:start(ranch),
ok = application:start(cowboy),
ok = application:start(basic_auth).

View file

@ -1,32 +1,29 @@
%% Feel free to use, reuse and abuse the code in this file. %% Feel free to use, reuse and abuse the code in this file.
%% @doc Basic authorization Hello world handler. %% @doc Handler with basic HTTP authorization.
-module(toppage_handler). -module(toppage_handler).
-export([init/3]). -export([init/3]).
-export([content_types_provided/2]). -export([content_types_provided/2]).
-export([is_authorized/2]). -export([is_authorized/2]).
-export([hello_to_text/2]). -export([to_text/2]).
init(_Transport, _Req, []) -> init(_Transport, _Req, []) ->
{upgrade, protocol, cowboy_rest}. {upgrade, protocol, cowboy_rest}.
is_authorized(Req, State) ->
is_authorized(Req, S) ->
{ok, Auth, Req1} = cowboy_req:parse_header(<<"authorization">>, Req), {ok, Auth, Req1} = cowboy_req:parse_header(<<"authorization">>, Req),
case Auth of case Auth of
{<<"basic">>, {User = <<"Alladin">>, <<"open sesame">>}} -> {<<"basic">>, {User = <<"Alladin">>, <<"open sesame">>}} ->
{true, Req1, User}; {true, Req1, User};
_ -> _ ->
{{false, <<"Basic realm=\"cowboy\"">>}, Req1, S} {{false, <<"Basic realm=\"cowboy\"">>}, Req1, State}
end. end.
content_types_provided(Req, State) -> content_types_provided(Req, State) ->
{[ {[
{<<"text/plain">>, hello_to_text} {<<"text/plain">>, to_text}
], Req, State}. ], Req, State}.
to_text(Req, User) ->
hello_to_text(Req, User) -> {<< "Hello, ", User/binary, "!\n" >>, Req, User}.
{<< <<"Hello, ">>/binary, User/binary, <<"!\n">>/binary >>, Req, User}.

View file

@ -1,4 +0,0 @@
#!/bin/sh
erl -pa ebin deps/*/ebin -s basic_auth \
-eval "io:format(\"Get 401: curl -i http://localhost:8080~n\")." \
-eval "io:format(\"Get 200: curl -i -u \\\"Alladin:open sesame\\\" http://localhost:8080~n\")."