2013-01-30 00:30:05 +04:00
|
|
|
%% Feel free to use, reuse and abuse the code in this file.
|
|
|
|
|
2013-09-07 16:18:51 +02:00
|
|
|
%% @doc Handler with basic HTTP authorization.
|
2018-09-07 13:48:43 -04:00
|
|
|
-module(toppage_h).
|
2013-01-30 00:30:05 +04:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-export([init/2]).
|
2013-01-30 00:30:05 +04:00
|
|
|
-export([content_types_provided/2]).
|
|
|
|
-export([is_authorized/2]).
|
2013-09-07 16:18:51 +02:00
|
|
|
-export([to_text/2]).
|
2013-01-30 00:30:05 +04:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
init(Req, Opts) ->
|
2014-09-30 20:12:13 +03:00
|
|
|
{cowboy_rest, Req, Opts}.
|
2013-01-30 00:30:05 +04:00
|
|
|
|
2013-09-07 16:18:51 +02:00
|
|
|
is_authorized(Req, State) ->
|
2014-09-23 16:43:29 +03:00
|
|
|
case cowboy_req:parse_header(<<"authorization">>, Req) of
|
2016-01-14 20:00:50 +01:00
|
|
|
{basic, User = <<"Alladin">>, <<"open sesame">>} ->
|
2014-09-23 16:43:29 +03:00
|
|
|
{true, Req, User};
|
2013-01-30 00:30:05 +04:00
|
|
|
_ ->
|
2014-09-23 16:43:29 +03:00
|
|
|
{{false, <<"Basic realm=\"cowboy\"">>}, Req, State}
|
2013-01-30 00:30:05 +04:00
|
|
|
end.
|
|
|
|
|
|
|
|
content_types_provided(Req, State) ->
|
|
|
|
{[
|
2013-09-07 16:18:51 +02:00
|
|
|
{<<"text/plain">>, to_text}
|
2013-01-30 00:30:05 +04:00
|
|
|
], Req, State}.
|
|
|
|
|
2013-09-07 16:18:51 +02:00
|
|
|
to_text(Req, User) ->
|
|
|
|
{<< "Hello, ", User/binary, "!\n" >>, Req, User}.
|