1
- -- we have multiple scenarious.
2
- -- base case everything is allowed
3
- -- no cookie allowed in frontend
4
- -- no js allowed in frontend
5
- --
6
-
7
1
-- BASE CASE
8
2
-- Token should be saved to sessionStorage
9
3
-- Refresh-token should be saved in cookie which only server can setup.
10
4
11
- create or replace function login (email text , password text , jwt_cookie boolean DEFAULT false, rt_cookie boolean DEFAULT false, csrf boolean DEFAULT true, csrf_token text DEFAULT null ) returns json as $$
5
+ create or replace function login (email text , password text , cookie boolean DEFAULT false, csrf text DEFAULT null ) returns json as $$
12
6
declare
13
7
usr record;
14
8
ses record;
@@ -23,68 +17,40 @@ begin
23
17
raise invalid_password using detail = ' Invalid email or password' , hint = ' Make sure your email and password are correct!' ;
24
18
else
25
19
head := request .header (' host' ) || ' -' || request .header (' user-agent' );
26
- if jwt_cookie is true then
27
- token := pgjwt .sign (
28
- json_build_object(
29
- ' role' , usr .role ,
30
- ' user_id' , usr .id ,
31
- ' exp' , extract(epoch from now())::integer + settings .get (' jwt_lifetime' )::int ,
32
- ' user-agent' , head
33
- ),
34
- settings .get (' jwt_secret' )
35
- );
36
- else
37
- token := pgjwt .sign (
38
- json_build_object(
39
- ' role' , usr .role ,
40
- ' user_id' , usr .id ,
41
- ' exp' , extract(epoch from now())::integer + settings .get (' jwt_lifetime' )::int
42
- ),
43
- settings .get (' jwt_secret' )
44
- );
45
- end if;
46
-
47
- if csrf_token is not null then
48
- delete from data." session" where csrf= csrf_token; -- remove old refresh token for this user with device
20
+ token := pgjwt .sign (
21
+ json_build_object(
22
+ ' role' , usr .role ,
23
+ ' user_id' , usr .id ,
24
+ ' exp' , extract(epoch from now())::integer + settings .get (' jwt_lifetime' )::int
25
+ ),
26
+ settings .get (' jwt_secret' )
27
+ );
28
+
29
+ if csrf is not null then
30
+ delete from data." session" where csrf= csrf; -- remove old refresh token for this user with device
49
31
end if;
50
32
51
33
-- TODO add more info, like IP address, Location to logs, and to session.
52
34
insert into data." session" as s
53
- (user_id, device_name, csrf, exp) values (usr .id , head, util . ifnull (csrf, pgjwt .url_encode (convert_to(replace(uuid_generate_v4()::text , ' -' , ' ' ), ' utf8' ) )), extract(epoch from now())::integer + settings .get (' refresh_token_lifetime' )::int )
35
+ (user_id, device_name, csrf, exp) values (usr .id , head, pgjwt .url_encode (convert_to(replace(uuid_generate_v4()::text , ' -' , ' ' ), ' utf8' )), extract(epoch from now())::integer + settings .get (' refresh_token_lifetime' )::int )
54
36
returning *
55
37
into ses;
56
38
57
- if jwt_cookie is true then
58
- perform response .set_cookie (' JWTTOKEN' , token, settings .get (' jwt_lifetime' )::int ,' /' );
59
- end if;
60
-
61
- if rt_cookie is true then
39
+ if cookie is true then
62
40
perform response .set_cookie (' REFRESHTOKEN' , ses .id ::text , settings .get (' refresh_token_lifetime' )::int ,' /' ::text );
63
41
end if;
64
42
65
- if csrf is true then
66
- return json_build_object(
67
- ' id' , usr .id ,
68
- ' name' , usr .name ,
69
- ' email' , usr .email ,
70
- ' role' , usr .role ::text ,
71
- ' token' , token::text ,
72
- ' refresh_token' , ses .id ::text ,
73
- ' csrf' , ses .csrf ::text
74
- );
75
- else
76
- return json_build_object(
77
- ' id' , usr .id ,
78
- ' name' , usr .name ,
79
- ' email' , usr .email ,
80
- ' role' , usr .role ::text ,
81
- ' token' , token::text ,
82
- ' refresh_token' , ses .id ::text
83
- );
84
- end if;
85
-
43
+ return json_build_object(
44
+ ' id' , usr .id ,
45
+ ' name' , usr .name ,
46
+ ' email' , usr .email ,
47
+ ' role' , usr .role ::text ,
48
+ ' token' , token::text ,
49
+ ' refresh_token' , ses .id ::text ,
50
+ ' csrf' , ses .csrf ::text
51
+ );
86
52
end if;
87
53
end
88
54
$$ volatile security definer language plpgsql;
89
55
-- by default all functions are accessible to the public, we need to remove that and define our specific access rules
90
- revoke all privileges on function login(text , text , boolean , boolean , boolean , text ) from public;
56
+ revoke all privileges on function login(text , text , boolean , text ) from public;
0 commit comments