Abstract setting the cookie for the client request.

main
absc 2024-08-15 21:20:38 +00:00
parent c9fcd47064
commit 38d8e29734
2 changed files with 49 additions and 7 deletions

View File

@ -25,7 +25,8 @@ from the dudeswave database.
-include_lib("storage/include/storage.hrl"). -include_lib("storage/include/storage.hrl").
-export([authenticate/2, details/1, new/3, -export([authenticate/2, details/1, new/3,
update/4, delete/1, logout/2, auth_cookies/1, invalidate_cookies/1]). update/4, delete/1, logout/2, auth_cookies/1, invalidate_cookies/1,
set_auth_cookies/4]).
-doc """ -doc """
Verify a session with an existing cookie. Verify a session with an existing cookie.
@ -266,6 +267,20 @@ auth_cookies(Req) ->
{User, Cookie}. {User, Cookie}.
-doc """
Invalidate the cookies in the passed request.
Spec:
```
-spec invalidate_cookies(Req) -> Req0 when
Req :: cowboy_req:req(),
Req0 :: cowboy_req:req().
```
A new request `Req0` is returned to the caller with the cookies zeroed and
completely invalidated.
""".
-spec invalidate_cookies(Req) -> Req0 when -spec invalidate_cookies(Req) -> Req0 when
Req :: cowboy_req:req(), Req :: cowboy_req:req(),
Req0 :: cowboy_req:req(). Req0 :: cowboy_req:req().
@ -276,4 +291,35 @@ invalidate_cookies(Req) ->
Req1 = cowboy_req:set_resp_cookie(<<"dudename">>, <<"">>, Req0, Req1 = cowboy_req:set_resp_cookie(<<"dudename">>, <<"">>, Req0,
#{max_age => 0}), #{max_age => 0}),
Req1. Req1.
-doc """
Set the authentication cookies for the provided clien request
Spec:
```
-spec set_auth_cookies(Req, User, Cookie, Validity) -> Req0 when
Req :: cowboy_req:req(),
User :: binary(),
Cookie :: binary(),
Validity :: pos_integer(),
Req0 :: cowboy_req:req().
```
A new request object `Req0`is returned, with the user and auth cookies set.
""".
-spec set_auth_cookies(Req, User, Cookie, Validity) -> Req0 when
Req :: cowboy_req:req(),
User :: binary(),
Cookie :: binary(),
Validity :: pos_integer(),
Req0 :: cowboy_req:req().
set_auth_cookies(Req, User, Cookie, Validity) ->
Req0 = cowboy_req:set_resp_cookie(<<"dudeauth">>, Cookie, Req,
#{max_age => Validity}),
Req1 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req0,
#{max_age => Validity}),
Req1.

View File

@ -156,11 +156,7 @@ login(Req, State) ->
case dudeswave_auth:authenticate(User, {password, Pass}) of case dudeswave_auth:authenticate(User, {password, Pass}) of
{true, Cookie, Validity} -> {true, Cookie, Validity} ->
Req1 = cowboy_req:set_resp_cookie(<<"dudeauth">>, Cookie, Req0, {true, dudeswave_auth:set_auth_cookies(Req, User, Cookie, Validity), State};
#{max_age => Validity}),
Req2 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req1,
#{max_age => Validity}),
{true, Req2, State};
false -> false ->
{false, Req0, State}; {false, Req0, State};
{error, _} -> {error, _} ->