Improve the registration bit.

Now, we return a proper answer when creating a user through a
POST request.

Maybe in the future we will convert the APIs to use JSON, like
all cool kids do. However, it doesn't look like it makes much
sense in this case.
main
absc 2024-08-03 01:13:47 +02:00
parent fa66ec399d
commit 96781451b0
1 changed files with 14 additions and 2 deletions

View File

@ -27,7 +27,7 @@ Register a new user.
% %
-export([allowed_methods/2, content_types_accepted/2, -export([allowed_methods/2, content_types_accepted/2,
known_methods/2, resource_exists/2, is_conflict/2, known_methods/2, resource_exists/2, is_conflict/2,
create_user/2]). previously_existed/2, allow_missing_post/2, create_user/2]).
-include_lib("storage/include/storage.hrl"). -include_lib("storage/include/storage.hrl").
@ -68,14 +68,26 @@ is_conflict(Req, user_exists) -> {true, Req, []};
is_conflict(Req, State) -> {false, Req, State}. is_conflict(Req, State) -> {false, Req, State}.
previously_existed(Req, State) ->
{false, Req, State}.
allow_missing_post(Req, State) ->
{true, Req, State}.
create_user(Req, {Bucket, [{name, Name}, {username, User}, {password, Pass}]}) -> create_user(Req, {Bucket, [{name, Name}, {username, User}, {password, Pass}]}) ->
crypto:rand_seed(), crypto:rand_seed(),
Salt = rand:bytes(32), Salt = rand:bytes(32),
Hash = crypto:hash(sha256, <<Pass/binary, Salt/binary>>), Hash = crypto:hash(sha256, <<Pass/binary, Salt/binary>>),
URI = uri_string:recompose(#{
scheme => cowboy_req:scheme(Req),
host => cowboy_req:host(Req),
path => lists:flatten(["/user/", User])
}),
case storage:write(Bucket, User, Hash, [{salt, Salt}, {name, Name}]) of case storage:write(Bucket, User, Hash, [{salt, Salt}, {name, Name}]) of
ok -> ok ->
{true, Req, []}; {{true, list_to_binary(URI)}, Req, []};
{error, Reason} -> {error, Reason} ->
{false, Req, Reason} {false, Req, Reason}
end. end.