Use proplists instead of lists.

main
absc 2024-08-15 22:39:22 +00:00
parent e95e91b645
commit 18a430614f
1 changed files with 9 additions and 7 deletions

View File

@ -59,7 +59,7 @@ authenticate(User, {cookie, Cookie}) ->
{ok, [R]} -> {ok, [R]} ->
CurTime = calendar:now_to_universal_time(erlang:timestamp()), CurTime = calendar:now_to_universal_time(erlang:timestamp()),
CookieTime = R#object.value, CookieTime = R#object.value,
CookieUser = lists:keyfind(user, 1, R#object.metadata), {user, CookieUser} = proplists:lookup(user, R#object.metadata),
if if
CookieTime >= CurTime -> CookieTime >= CurTime ->
@ -83,11 +83,14 @@ authenticate(User, {password, Password}) ->
erlang:system_time(seconds) + ?DEFVALIDITY * 86400 erlang:system_time(seconds) + ?DEFVALIDITY * 86400
end, end,
{ok, Hash} = lists:keyfind(hash, 1, R#object.metadata), {hash, Hash} = proplists:lookup(hash, R#object.metadata),
{ok, Salt} = lists:keyfind(salt, 1, R#object.metadata), {salt, Salt} = proplists:lookup(salt, R#object.metadata),
{approved, Appr} = proplists:lookup(approved, R#object.metadata),
Auth = crypto:hash(sha256, <<Password/binary, Salt/binary>>), Auth = crypto:hash(sha256, <<Password/binary, Salt/binary>>),
if if
Appr =/= true -> false;
Auth =:= Hash -> Auth =:= Hash ->
Cookie = base64:encode(rand:bytes(64)), Cookie = base64:encode(rand:bytes(64)),
case storage:write(?COOKIESBUCK, <<Cookie/binary>>, case storage:write(?COOKIESBUCK, <<Cookie/binary>>,
@ -123,7 +126,7 @@ Invalidate and delete `Cookie` associated with `User` from the system.
logout(User, Cookie) -> logout(User, Cookie) ->
case storage:read(?COOKIESBUCK, Cookie) of case storage:read(?COOKIESBUCK, Cookie) of
{ok, [R]} -> {ok, [R]} ->
{user, User} = lists:keyfind(user, 1, R#object.metadata), {user, User} = proplists:lookup(user, R#object.metadata),
storage:delete(?COOKIESBUCK, Cookie); storage:delete(?COOKIESBUCK, Cookie);
{ok, []} -> {ok, []} ->
{error, not_found}; {error, not_found};
@ -171,8 +174,7 @@ Spec:
The `User` is created, and stored in the application's users bucket The `User` is created, and stored in the application's users bucket
`Password` is salted and hashed with SHA256 before being stored. `Password` is salted and hashed with SHA256 before being stored.
The new user is saved with a metadata `status` of `waiting_confirmation`, The new user is saved with a metadata `approved` of `false`,
based on the application settings, the confirmation method may vary.
""". """.
-spec new(User, Password, Email) -> ok | {error, Reason} when -spec new(User, Password, Email) -> ok | {error, Reason} when
User :: binary(), User :: binary(),
@ -185,7 +187,7 @@ new(User, Password, Email) ->
Hash = crypto:hash(sha256, <<Password/binary, Salt/binary>>), Hash = crypto:hash(sha256, <<Password/binary, Salt/binary>>),
Data = #{<<"email">> => Email}, Data = #{<<"email">> => Email},
Metadata = [{salt, Salt}, {hash, Hash}, {status, waiting_confirmation}], Metadata = [{salt, Salt}, {hash, Hash}, {approved, false}],
storage:write(?USERSBUCK, User, Data, Metadata). storage:write(?USERSBUCK, User, Data, Metadata).