Skip to content

Commit d57d839

Browse files
committed
Delete sessions after password change/reset
1 parent 9f02e58 commit d57d839

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

@app/db/migrations/committed/000001.sql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--! Previous: -
2-
--! Hash: sha1:fbd6ffb5be53e7e0bfc63929a52cfc13e698c5f0
2+
--! Hash: sha1:eaf2866060caa0bba319236017c15a40d37a7815
33

44
--! split: 0001-reset.sql
55
/*
@@ -1064,7 +1064,9 @@ begin
10641064
-- Not too many reset attempts, let's check the token
10651065
if v_user_secret.reset_password_token = reset_token then
10661066
-- Excellent - they're legit
1067+
10671068
perform app_private.assert_valid_password(new_password);
1069+
10681070
-- Let's reset the password as requested
10691071
update app_private.user_secrets
10701072
set
@@ -1076,13 +1078,20 @@ begin
10761078
failed_reset_password_attempts = 0,
10771079
first_failed_reset_password_attempt = null
10781080
where user_secrets.user_id = v_user.id;
1081+
1082+
-- Revoke the users' sessions
1083+
delete from app_private.sessions
1084+
where sessions.user_id = v_user.id;
1085+
1086+
-- Notify user their password was reset
10791087
perform graphile_worker.add_job(
10801088
'user__audit',
10811089
json_build_object(
10821090
'type', 'reset_password',
10831091
'user_id', v_user.id,
10841092
'current_user_id', app_public.current_user_id()
10851093
));
1094+
10861095
return true;
10871096
else
10881097
-- Wrong token, bump all the attempt tracking figures
@@ -1221,18 +1230,27 @@ begin
12211230

12221231
if v_user_secret.password_hash = crypt(old_password, v_user_secret.password_hash) then
12231232
perform app_private.assert_valid_password(new_password);
1233+
12241234
-- Reset the password as requested
12251235
update app_private.user_secrets
12261236
set
12271237
password_hash = crypt(new_password, gen_salt('bf'))
12281238
where user_secrets.user_id = v_user.id;
1239+
1240+
-- Revoke all other sessions
1241+
delete from app_private.sessions
1242+
where sessions.user_id = v_user.id
1243+
and sessions.uuid <> app_public.current_session_id();
1244+
1245+
-- Notify user their password was changed
12291246
perform graphile_worker.add_job(
12301247
'user__audit',
12311248
json_build_object(
12321249
'type', 'change_password',
12331250
'user_id', v_user.id,
12341251
'current_user_id', app_public.current_user_id()
12351252
));
1253+
12361254
return true;
12371255
else
12381256
raise exception 'Incorrect password' using errcode = 'CREDS';

data/schema.sql

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 13.2 (Ubuntu 13.2-1.pgdg18.04+1)
6-
-- Dumped by pg_dump version 13.2 (Ubuntu 13.2-1.pgdg18.04+1)
5+
-- Dumped from database version 13.4 (Ubuntu 13.4-0ubuntu0.21.04.1)
6+
-- Dumped by pg_dump version 13.4 (Ubuntu 13.4-0ubuntu0.21.04.1)
77

88
SET statement_timeout = 0;
99
SET lock_timeout = 0;
@@ -522,7 +522,9 @@ begin
522522
-- Not too many reset attempts, let's check the token
523523
if v_user_secret.reset_password_token = reset_token then
524524
-- Excellent - they're legit
525+
525526
perform app_private.assert_valid_password(new_password);
527+
526528
-- Let's reset the password as requested
527529
update app_private.user_secrets
528530
set
@@ -534,13 +536,20 @@ begin
534536
failed_reset_password_attempts = 0,
535537
first_failed_reset_password_attempt = null
536538
where user_secrets.user_id = v_user.id;
539+
540+
-- Revoke the users' sessions
541+
delete from app_private.sessions
542+
where sessions.user_id = v_user.id;
543+
544+
-- Notify user their password was reset
537545
perform graphile_worker.add_job(
538546
'user__audit',
539547
json_build_object(
540548
'type', 'reset_password',
541549
'user_id', v_user.id,
542550
'current_user_id', app_public.current_user_id()
543551
));
552+
544553
return true;
545554
else
546555
-- Wrong token, bump all the attempt tracking figures
@@ -770,18 +779,27 @@ begin
770779

771780
if v_user_secret.password_hash = crypt(old_password, v_user_secret.password_hash) then
772781
perform app_private.assert_valid_password(new_password);
782+
773783
-- Reset the password as requested
774784
update app_private.user_secrets
775785
set
776786
password_hash = crypt(new_password, gen_salt('bf'))
777787
where user_secrets.user_id = v_user.id;
788+
789+
-- Revoke all other sessions
790+
delete from app_private.sessions
791+
where sessions.user_id = v_user.id
792+
and sessions.uuid <> app_public.current_session_id();
793+
794+
-- Notify user their password was changed
778795
perform graphile_worker.add_job(
779796
'user__audit',
780797
json_build_object(
781798
'type', 'change_password',
782799
'user_id', v_user.id,
783800
'current_user_id', app_public.current_user_id()
784801
));
802+
785803
return true;
786804
else
787805
raise exception 'Incorrect password' using errcode = 'CREDS';

0 commit comments

Comments
 (0)