Corrected the back-end calls to support multi-hosting.

main
absc 2024-09-15 19:22:47 +00:00
parent b784afdea5
commit 5b73df8999
3 changed files with 21 additions and 11 deletions

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.