toppage_handler.erl 740 B

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