Debian Codegraph @

examples/rest_basic_auth/src/toppage_h.erl

raw
1 %% Feel free to use, reuse and abuse the code in this file.
2
3 %% @doc Handler with basic HTTP authorization.
4 -module(toppage_h).
5
6 -export([init/2]).
7 -export([content_types_provided/2]).
8 -export([is_authorized/2]).
9 -export([to_text/2]).
10
11 init(Req, Opts) ->
12 {cowboy_rest, Req, Opts}.
13
14 is_authorized(Req, State) ->
15 case cowboy_req:parse_header(<<"authorization">>, Req) of
16 {basic, User = <<"Alladin">>, <<"open sesame">>} ->
17 {true, Req, User};
18 _ ->
19 {{false, <<"Basic realm=\"cowboy\"">>}, Req, State}
20 end.
21
22 content_types_provided(Req, State) ->
23 {[
24 {<<"text/plain">>, to_text}
25 ], Req, State}.
26
27 to_text(Req, User) ->
28 {<< "Hello, ", User/binary, "!\n" >>, Req, User}.