Skip to content

Commit

Permalink
Basic implementation of web admin basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrk committed Mar 17, 2014
1 parent aa18f52 commit c4bc178
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
75 changes: 68 additions & 7 deletions src/web/web_admin_req_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ init(_Transport, Req, []) ->
{ok, Req, undefined}.

handle(Req, State) ->

Username = "",
Password = "",

% check login credentials
{Username1, Password1, Req1} = credentials(Req),
{ok, Req2} = case {Username, Password} of
{?USERNAME, ?PASSWORD} ->
authorized(Req1, State);
_ ->
unauthorized(Req)
end,
{ok, Req2, State}.

terminate(_Reason, _Req, _State) ->
ok.

%%=============================================================================
%% Internal functions
%%=============================================================================
authorized(Req, State) ->
% check body
case cowboy_req:has_body(Req) of
true ->
Expand All @@ -32,12 +53,6 @@ handle(Req, State) ->
{ok, Req, State}
end.

terminate(_Reason, _Req, _State) ->
ok.

%%=============================================================================
%% Internal functions
%%=============================================================================
handle_request(Body, Req, State) ->
% get method and params
{[{<<"method">>, Method},{<<"params">>, Params}]} = jiffy:decode(Body),
Expand Down Expand Up @@ -187,4 +202,50 @@ handle_request(Body, Req, State) ->

%% @doc Format plugins
format_plugins_helper(Plugins) ->
list_to_binary([Lang ++ " " ++ Name ++ " " ++ Path ++ "\n" || {plugin, Lang, Name, Path} <- Plugins]).
list_to_binary([Lang ++ " " ++ Name ++ " " ++ Path ++ "\n" || {plugin, Lang, Name, Path} <- Plugins]).

%% Authorization helpers
credentials(Req) ->
{AuthorizationHeader, Req} = cowboy_http_req:header('Authorization', Req),
case AuthorizationHeader of
undefined ->
{undefined, undefined, Req};
_ ->
{Username, Password} = credentials_from_header(AuthorizationHeader),
{Username, Password, Req}
end.

credentials_from_header(AuthorizationHeader) ->
case binary:split(AuthorizationHeader, <<$ >>) of
[<<"Basic">>, EncodedCredentials] ->
decoded_credentials(EncodedCredentials);
_ ->
{undefined, undefined}
end.

decoded_credentials(EncodedCredentials) ->
case binary:split(base64:decode(EncodedCredentials), <<$:>>) of
[Username, Password] ->
{Username, Password};
_ ->
{undefined, undefined}
end.

unauthorized(Req) ->
{ok, Req} =
cowboy_http_req:set_resp_header(<<"Www-Authenticate">>,
<<"Basic realm=\"Secure Area\"">>, Req),
{ok, Req} = cowboy_http_req:set_resp_body(unauthorized_body(), Req),
cowboy_http_req:reply(401, Req).

unauthorized_body() ->
<<"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dt\">
<HTML>
<HEAD>
<TITLE>Error</TITLE>
<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=ISO-8859-1\">
</HEAD>
<BODY><H1>401 Unauthorized.</H1></BODY>
</HTML>
">>.
5 changes: 2 additions & 3 deletions src/web/ybot_web_admin_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% @doc Start http server
%% @end
-spec start_web_admin() -> {ok, Pid :: pid()} | {error, Reason :: term()}.
Expand All @@ -43,12 +43,11 @@ init([]) ->

% http server child process
ChildSpec = [

{web_admin,
{web_admin, start_link, []},
temporary, 2000, worker, []
}
],

% init
{ok,{{simple_one_for_one, 10, 60}, ChildSpec}}.
{ok,{{simple_one_for_one, 10, 60}, ChildSpec}}.
8 changes: 6 additions & 2 deletions ybot.config
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,17 @@
% Ybot web interface
%
{web_admin,
[
[
% use web admin or not
{use_web_admin, true},
% Web interface host
{webadmin_host, <<"localhost">>},
% Web interface port
{webadmin_port, 8000}
{webadmin_port, 8000},
% Web interface authorization
{webadmin_auth, true},
{webadmin_auth_user, "foo"},
{webadmin_auth_passwd, "bar"}
]
},

Expand Down

0 comments on commit c4bc178

Please sign in to comment.