Compare commits

..

3 Commits

5 changed files with 22 additions and 14 deletions

View File

@ -19,8 +19,6 @@
-export([bootstrap/3, start/2, stop/1]). -export([bootstrap/3, start/2, stop/1]).
start(_Type, StartArgs) -> start(_Type, StartArgs) ->
crypto:rand_seed(),
dudeswave_backend_supervisor:start_link(StartArgs). dudeswave_backend_supervisor:start_link(StartArgs).
stop(_State) -> ok. stop(_State) -> ok.

View File

@ -5,6 +5,6 @@
dudeswave_users_supervisor, dudeswave_users_supervisor,
dudeswave_users_auth,dudeswave_users_user]}, dudeswave_users_auth,dudeswave_users_user]},
{registered,[]}, {registered,[]},
{applications,[kernel,stdlib,erts]}, {applications,[kernel,stdlib,erts,storage,crypto]},
{mod,{dudeswave_users_app,[]}}, {mod,{dudeswave_users_app,[]}},
{start_phases,[]}]}. {start_phases,[]}]}.

View File

@ -8,6 +8,7 @@ ERLFLAGS= -I ../../
OBJS= dudeswave_web.beam dudeswave_web_app.beam OBJS= dudeswave_web.beam dudeswave_web_app.beam
OBJS+= dudeswave_web_supervisor.beam dudeswave_web_handler.beam OBJS+= dudeswave_web_supervisor.beam dudeswave_web_handler.beam
OBJS+= dudeswave_web_user_handler.beam dudeswave_web_common.beam OBJS+= dudeswave_web_user_handler.beam dudeswave_web_common.beam
OBJS+= dudeswave_web_auth_handler.beam
all: ${OBJS} all: ${OBJS}

View File

@ -13,7 +13,7 @@
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
% %
-module(dudeswave_auth_handler). -module(dudeswave_web_auth_handler).
-moduledoc """ -moduledoc """
JSON API to authenticate users. JSON API to authenticate users.
@ -94,9 +94,10 @@ forbidden(Req, State) ->
<<"POST">> -> <<"POST">> ->
{false, Req, State}; {false, Req, State};
_ -> _ ->
{User, Auth} = dudeswave_web_common:auth_cookies(Req), {User, Cookie} = dudeswave_web_common:auth_cookies(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:auth(cookie, User, Cookie) of case dudeswave_backend:auth(cookie, User, Host, Cookie) of
{error, service_unavailable} -> exit(service_unavailable); {error, service_unavailable} -> exit(service_unavailable);
true -> {false, Req, State}; true -> {false, Req, State};
false -> {true, Req, State} false -> {true, Req, State}
@ -113,8 +114,9 @@ content_types_accepted(Req, State) ->
resource_exists(Req, State) -> resource_exists(Req, State) ->
{User, _} = dudeswave_web_common:auth_cookies(Req), {User, _} = dudeswave_web_common:auth_cookies(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:user_details(User) of case dudeswave_backend:user_details(User, Host) of
[] -> [] ->
{false, Req, State}; {false, Req, State};
{error, Reason} -> {error, Reason} ->
@ -136,8 +138,9 @@ allow_missing_post(Req, State) -> {false, Req, State}.
delete_resource(Req, State) -> delete_resource(Req, State) ->
{User, Auth} = dudeswave_web_common:auth_cookies(Req), {User, Auth} = dudeswave_web_common:auth_cookies(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:logout(User, Auth) of case dudeswave_backend:logout(User, Host, Auth) of
ok -> ok ->
{true, invalidate_cookies(Req), State}; {true, invalidate_cookies(Req), State};
{error, _} -> {error, _} ->
@ -152,8 +155,9 @@ delete_completed(Req, State) -> {false, Req, State}.
login(Req, State) -> login(Req, State) ->
{User, Pass, Req0} = read_login_data(Req), {User, Pass, Req0} = read_login_data(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:auth(password, User, Pass) of case dudeswave_backend:auth(password, User, Host, Pass) of
{true, Cookie, Validity} -> {true, Cookie, Validity} ->
{true, set_auth_cookies(Req, User, Cookie, Validity), State}; {true, set_auth_cookies(Req, User, Cookie, Validity), State};
false -> false ->

View File

@ -139,8 +139,9 @@ forbidden(Req, State) ->
{false, Req, State}; {false, Req, State};
_ -> _ ->
{User, Auth} = dudeswave_web_common:auth_cookies(Req), {User, Auth} = dudeswave_web_common:auth_cookies(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:auth(cookie, User, Auth) of case dudeswave_backend:auth(cookie, User, Host, Auth) of
{error, service_unavailable} -> {true, Req, State}; {error, service_unavailable} -> {true, Req, State};
true -> {false, Req, State}; true -> {false, Req, State};
false -> {true, Req, State} false -> {true, Req, State}
@ -169,8 +170,9 @@ content_types_accepted(Req, State) ->
resource_exists(Req, State) -> resource_exists(Req, State) ->
{User, _} = dudeswave_web_common:auth_cookies(Req), {User, _} = dudeswave_web_common:auth_cookies(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:user_details(User) of case dudeswave_backend:user_details(User, Host) of
[] -> [] ->
{false, Req, State}; {false, Req, State};
{error, _} -> {error, _} ->
@ -195,8 +197,9 @@ allow_missing_post(Req, State) -> {false, Req, State}.
delete_resource(Req, State) -> delete_resource(Req, State) ->
{User, _} = dudeswave_web_common:auth_cookies(Req), {User, _} = dudeswave_web_common:auth_cookies(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:delete_user(User) of case dudeswave_backend:delete_user(User, Host) of
ok -> {true, Req, State}; ok -> {true, Req, State};
{error, _} -> {false, Req, State} {error, _} -> {false, Req, State}
end. end.
@ -209,8 +212,9 @@ delete_completed(Req, State) -> {true, Req, State}.
create_user(Req, State) -> create_user(Req, State) ->
{User, Pass, Email, Req0} = read_new_user_data(Req), {User, Pass, Email, Req0} = read_new_user_data(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:new_user(User, Pass, Email) of case dudeswave_backend:new_user(User, Host, Pass, Email) of
ok -> {true, Req0, []}; ok -> {true, Req0, []};
{error, _} -> {false, Req0, State} {error, _} -> {false, Req0, State}
end. end.
@ -218,8 +222,9 @@ create_user(Req, State) ->
modify_user(Req, State) -> modify_user(Req, State) ->
{User, _} = dudeswave_web_common:auth_cookies(Req), {User, _} = dudeswave_web_common:auth_cookies(Req),
{Email, Desc, Name, Req0} = read_update_user_data(Req), {Email, Desc, Name, Req0} = read_update_user_data(Req),
Host = cowboy_req:host(Req),
case dudeswave_backend:update_user(User, Name, Email, Desc) of case dudeswave_backend:update_user(User, Host, Name, Email, Desc) of
ok -> {true, Req0, []}; ok -> {true, Req0, []};
{error, _} -> {false, Req0, State} {error, _} -> {false, Req0, State}
end. end.