diff --git a/dudeswave_backend/src/Makefile b/dudeswave_backend/src/Makefile index 8dab13c..0088ead 100644 --- a/dudeswave_backend/src/Makefile +++ b/dudeswave_backend/src/Makefile @@ -7,7 +7,6 @@ ERLFLAGS= -I ../../ OBJS= dudeswave_backend.beam dudeswave_backend_app.beam OBJS+= dudeswave_backend_supervisor.beam -OBJS+= dudeswave_backend_auth.beam dudeswave_backend_user.beam all: ${OBJS} diff --git a/dudeswave_backend/src/dudeswave_backend.erl b/dudeswave_backend/src/dudeswave_backend.erl index e2a4879..b367303 100644 --- a/dudeswave_backend/src/dudeswave_backend.erl +++ b/dudeswave_backend/src/dudeswave_backend.erl @@ -43,6 +43,10 @@ init([]) -> % Public APIs % +% +% Users management +% + auth(cookie, User, Cookie) -> gen_server:call({local, ?MODULE}, {cookie, User, Cookie}); @@ -69,7 +73,7 @@ delete_user(User) -> % handle_call({cookie, User, Cookie}, _From, State) -> - case dudeswave_backend_auth:authenticate(cookie, User, Cookie) of + case dudeswave_users:authenticate(cookie, User, Cookie) of true -> {reply, true, State}; false -> @@ -79,7 +83,7 @@ handle_call({cookie, User, Cookie}, _From, State) -> end; handle_call({password, User, Password}, _From, State) -> - case dudeswave_backend_auth:authenticate(password, User, Password) of + case dudeswave_users:authenticate(password, User, Password) of {true, Cookie, Validity} -> {reply, {true, Cookie, Validity}, State}; false -> @@ -89,32 +93,32 @@ handle_call({password, User, Password}, _From, State) -> end; handle_call({logout, User, Cookie}, _From, State) -> - case dudeswave_backend_auth:logout(User, Cookie) of + case dudeswave_users:logout(User, Cookie) of ok -> {reply, ok, State}; {error, Reason} -> {reply, {error, Reason}, State} end; handle_call({user_details, User}, _From, State) -> - case dudeswave_backend_user:details(User) of + case dudeswave_users:details(User) of {error, not_found} -> {reply, not_found, State}; {error, Reason} -> {reply, {error, Reason}, State}; Val -> {reply, Val, State} end; handle_call({new_user, User, Password, Email}, _From, State) -> - case dudeswave_backend_user:new(User, Password, Email) of + case dudeswave_users:new(User, Password, Email) of ok -> {reply, ok, State}; {error, Reason} -> {reply, {error, Reason}, State} end; handle_call({update_user, User, Name, Email, Desc}, _From, State) -> - case dudeswave_backend_user:update(User, Name, Email, Desc) of + case dudeswave_users:update(User, Name, Email, Desc) of ok -> {reply, ok, State}; {error, Reason} -> {reply, {error, Reason}, State} end; handle_call({delete_user, User}, _From, State) -> - case dudeswave_backend_user:delete(User) of + case dudeswave_users:delete(User) of ok -> {reply, ok, State}; {error, Reason} -> {reply, {error, Reason}, State} end. diff --git a/dudeswave_users/Makefile b/dudeswave_users/Makefile new file mode 100644 index 0000000..160022d --- /dev/null +++ b/dudeswave_users/Makefile @@ -0,0 +1,7 @@ +.PHONY: all clean + +all: + ${MAKE} -C src + +clean: + ${MAKE} -C src clean diff --git a/dudeswave_users/ebin/dudeswave_users.app b/dudeswave_users/ebin/dudeswave_users.app new file mode 100644 index 0000000..5590616 --- /dev/null +++ b/dudeswave_users/ebin/dudeswave_users.app @@ -0,0 +1,10 @@ +{application,dudeswave_users, + [{description,"The dudeswave users management module"}, + {vsn,"1.0.0"}, + {modules,[dudeswave_users,dudeswave_users_app, + dudeswave_users_supervisor, + dudeswave_users_auth,dudeswave_users_user]}, + {registered,[]}, + {applications,[kernel,stdlib,erts,cowboy,ranch]}, + {mod,{dudeswave_users_app,[]}}, + {start_phases,[]}]}. diff --git a/dudeswave_users/include/defines.hrl b/dudeswave_users/include/defines.hrl new file mode 100644 index 0000000..48c12ba --- /dev/null +++ b/dudeswave_users/include/defines.hrl @@ -0,0 +1,23 @@ +% +% Copyright (c) 2024 Andrea Biscuola +% +% Permission to use, copy, modify, and distribute this software for any +% purpose with or without fee is hereby granted, provided that the above +% copyright notice and this permission notice appear in all copies. +% +% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +% + +-define(APPBUCK, dudeswave). +-define(USERSBUCK, dudes). +-define(COOKIESBUCK, cookies). +-define(RANDBYTES, 32). +-define(DEFVALIDITY, 365). +-define(DUDENAME, "dudename"). +-define(DUDEAUTH, "dudeauth"). diff --git a/dudeswave_users/src/Makefile b/dudeswave_users/src/Makefile new file mode 100644 index 0000000..296b5dd --- /dev/null +++ b/dudeswave_users/src/Makefile @@ -0,0 +1,19 @@ +.PHONY: all clean +.SUFFIXES: .erl .beam + +ERLC?= erlc -server + +ERLFLAGS= -I ../../ + +OBJS= dudeswave_users.beam dudeswave_users_app.beam +OBJS+= dudeswave_users_supervisor.beam +OBJS+= dudeswave_users_auth.beam dudeswave_users_user.beam + +all: ${OBJS} + +.erl.beam: + ${ERLC} ${ERLOPTS} ${ERLFLAGS} $< + +clean: + rm -f *.beam + diff --git a/dudeswave_users/src/dudeswave_users.erl b/dudeswave_users/src/dudeswave_users.erl new file mode 100644 index 0000000..317e686 --- /dev/null +++ b/dudeswave_users/src/dudeswave_users.erl @@ -0,0 +1,125 @@ +% +% Copyright (c) 2024 Andrea Biscuola +% +% Permission to use, copy, modify, and distribute this software for any +% purpose with or without fee is hereby granted, provided that the above +% copyright notice and this permission notice appear in all copies. +% +% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +% +-module(dudeswave_users). + +-behaviour(gen_server). + +% Server start function +-export([start_link/0]). + +% Module callbacks +-export([init/1, handle_call/3, handle_cast/2, terminate/2]). + +% Public API exports +-export([auth/3, logout/2, details/1, new/3, + update/4, delete/1]). + +% +% Startup functions +% + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +init([]) -> + process_flag(trap_exit, true), + + {ok, 0}. + +% +% Public APIs +% + +auth(cookie, User, Cookie) -> + gen_server:call({local, ?MODULE}, {cookie, User, Cookie}); + +auth(password, User, Password) -> + gen_server:call({local, ?MODULE}, {password, User, Password}). + +logout(User, Cookie) -> + gen_server:call({local, ?MODULE}, {logout, User, Cookie}). + +details(User) -> + gen_server:call({local, ?MODULE}, {details, User}). + +new(User, Password, Email) -> + gen_server:call({local, ?MODULE}, {new, User, Password, Email}). + +update(User, Name, Email, Desc) -> + gen_server:call({local, ?MODULE}, {update, User, Name, Email, Desc}). + +delete(User) -> + gen_server:call({local, ?MODULE}, {delete, User}). + +% +% Callbacks +% + +handle_call({cookie, User, Cookie}, _From, State) -> + case dudeswave_users_auth:authenticate(cookie, User, Cookie) of + true -> + {reply, true, State}; + false -> + {reply, false, State}; + {error, Reason} -> + {reply, {error, Reason}, State} + end; + +handle_call({password, User, Password}, _From, State) -> + case dudeswave_users_auth:authenticate(password, User, Password) of + {true, Cookie, Validity} -> + {reply, {true, Cookie, Validity}, State}; + false -> + {reply, false, State}; + {error, Reason} -> + {reply, {error, Reason}, State} + end; + +handle_call({logout, User, Cookie}, _From, State) -> + case dudeswave_users_auth:logout(User, Cookie) of + ok -> {reply, ok, State}; + {error, Reason} -> {reply, {error, Reason}, State} + end; + +handle_call({details, User}, _From, State) -> + case dudeswave_users_user:details(User) of + {error, not_found} -> {reply, not_found, State}; + {error, Reason} -> {reply, {error, Reason}, State}; + Val -> {reply, Val, State} + end; + +handle_call({new, User, Password, Email}, _From, State) -> + case dudeswave_users_user:new(User, Password, Email) of + ok -> {reply, ok, State}; + {error, Reason} -> {reply, {error, Reason}, State} + end; + +handle_call({update, User, Name, Email, Desc}, _From, State) -> + case dudeswave_users_user:update(User, Name, Email, Desc) of + ok -> {reply, ok, State}; + {error, Reason} -> {reply, {error, Reason}, State} + end; + +handle_call({delete, User}, _From, State) -> + case dudeswave_users_user:delete(User) of + ok -> {reply, ok, State}; + {error, Reason} -> {reply, {error, Reason}, State} + end. + + +handle_cast(_Msg, State) -> {noreply, State}. + +terminate(_Reason, _N) -> ok. diff --git a/dudeswave_users/src/dudeswave_users_app.erl b/dudeswave_users/src/dudeswave_users_app.erl new file mode 100644 index 0000000..d3a0e0c --- /dev/null +++ b/dudeswave_users/src/dudeswave_users_app.erl @@ -0,0 +1,31 @@ +% +% Copyright (c) 2024 Andrea Biscuola +% +% Permission to use, copy, modify, and distribute this software for any +% purpose with or without fee is hereby granted, provided that the above +% copyright notice and this permission notice appear in all copies. +% +% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +% +-module(dudeswave_users_app). +-behaviour(application). + +-export([bootstrap/3, start/2, stop/1]). + +start(_Type, StartArgs) -> + crypto:rand_seed(), + + dudeswave_users_supervisor:start_link(StartArgs). + +stop(_State) -> ok. + +% +% Bootstrap procedure to be completed +% +bootstrap(_Admin, _Password, _Nodes) -> ok. diff --git a/dudeswave_backend/src/dudeswave_backend_auth.erl b/dudeswave_users/src/dudeswave_users_auth.erl similarity index 97% rename from dudeswave_backend/src/dudeswave_backend_auth.erl rename to dudeswave_users/src/dudeswave_users_auth.erl index c6b563a..6240fda 100644 --- a/dudeswave_backend/src/dudeswave_backend_auth.erl +++ b/dudeswave_users/src/dudeswave_users_auth.erl @@ -13,14 +13,14 @@ % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. % --module(dudeswave_backend_auth). +-module(dudeswave_users_auth). -moduledoc """ Dudes authentication module Here lives all the functions for the APIs needed to handle users authentication. """. --include_lib("dudeswave_backend/include/defines.hrl"). +-include_lib("dudeswave_users/include/defines.hrl"). -include_lib("storage/include/storage.hrl"). -export([authenticate/3, logout/2]). diff --git a/dudeswave_users/src/dudeswave_users_supervisor.erl b/dudeswave_users/src/dudeswave_users_supervisor.erl new file mode 100644 index 0000000..e3dcc9b --- /dev/null +++ b/dudeswave_users/src/dudeswave_users_supervisor.erl @@ -0,0 +1,31 @@ +% +% Copyright (c) 2024 Andrea Biscuola +% +% Permission to use, copy, modify, and distribute this software for any +% purpose with or without fee is hereby granted, provided that the above +% copyright notice and this permission notice appear in all copies. +% +% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +% +-module(dudeswave_users_supervisor). +-behaviour(supervisor). + +-export([start/0, + start_link/1, + init/1]). + +start() -> + spawn(fun() -> supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg = []) end). + +start_link(Args) -> + supervisor:start_link({local, ?MODULE}, ?MODULE, Args). + +init([]) -> + {ok, {{one_for_one, 3, 10}, [{tag1, {dudeswave_users, start_link, []}, permanent, + 10000, worker, [dudeswave_users]}]}}. \ No newline at end of file diff --git a/dudeswave_backend/src/dudeswave_backend_user.erl b/dudeswave_users/src/dudeswave_users_user.erl similarity index 97% rename from dudeswave_backend/src/dudeswave_backend_user.erl rename to dudeswave_users/src/dudeswave_users_user.erl index 907a561..eeb6eae 100644 --- a/dudeswave_backend/src/dudeswave_backend_user.erl +++ b/dudeswave_users/src/dudeswave_users_user.erl @@ -13,9 +13,9 @@ % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. % --module(dudeswave_backend_user). +-module(dudeswave_users_user). --include_lib("dudeswave_backend/include/defines.hrl"). +-include_lib("dudeswave_users/include/defines.hrl"). -include_lib("storage/include/storage.hrl"). -export([details/1, new/3, update/4, delete/1]).