From 969ede0a436a7fc9f74a5175b7f4f4a4d58e7e0c Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 2 Nov 2023 19:03:15 +0800 Subject: [PATCH 1/8] Add function to clear cookies --- openlibrary/accounts/model.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/openlibrary/accounts/model.py b/openlibrary/accounts/model.py index ce526b86a8b..8c85aa869fa 100644 --- a/openlibrary/accounts/model.py +++ b/openlibrary/accounts/model.py @@ -127,6 +127,10 @@ def create_link_doc(key, username, email): "expires_on": expires.isoformat(), } +def clear_cookies(): + web.setcookies('pd', "", expires=-1) + web.setcookies('sfw', "", expires=-1) + class Link(web.storage): def get_expiration_time(self): From 2593c3b467fac8057f2e098e5bb0a0b315eefbca Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 2 Nov 2023 19:17:32 +0800 Subject: [PATCH 2/8] Create account_logout function --- openlibrary/plugins/upstream/account.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index 08497d6a892..e0c45179bf1 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -18,6 +18,7 @@ ) from infogami.infobase.client import ClientException import infogami.core.code as core +from infogami.core.code import logout as infogami_logout from openlibrary import accounts from openlibrary.i18n import gettext as _ @@ -38,6 +39,7 @@ OpenLibraryAccount, InternetArchiveAccount, valid_email, + clear_cookies ) from openlibrary.plugins.upstream import borrow, forms, utils from openlibrary.utils.dateutil import elapsed_time @@ -478,6 +480,18 @@ def has_borrowed_at_least(self, amount: int, s3_keys) -> bool: return len(resp) == amount +class account_logout(delegate.page): + """Account logout. + + This registers a handler to the /account/logout endpoint in infogami so that additional logic, such as clearing admin cookies, + can be handled prior to the calling of infogami's standard logout procedure + + """ + path = "/account/login" + def POST(self): + clear_cookies() + infogami_logout().POST() + class account_verify(delegate.page): """Verify user account.""" From 665cf777ad31d207bdaac5efad3dfb04e197907c Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 2 Nov 2023 19:56:07 +0800 Subject: [PATCH 3/8] Fix typo of setcookies to setcookie --- openlibrary/accounts/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openlibrary/accounts/model.py b/openlibrary/accounts/model.py index 8c85aa869fa..cb81d28c016 100644 --- a/openlibrary/accounts/model.py +++ b/openlibrary/accounts/model.py @@ -128,8 +128,8 @@ def create_link_doc(key, username, email): } def clear_cookies(): - web.setcookies('pd', "", expires=-1) - web.setcookies('sfw', "", expires=-1) + web.setcookie('pd', "", expires=-1) + web.setcookie('sfw', "", expires=-1) class Link(web.storage): From 110661a22314ea539d715edfdb986c1223417853 Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 2 Nov 2023 19:56:25 +0800 Subject: [PATCH 4/8] Create the account_logout handler --- openlibrary/plugins/upstream/account.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index e0c45179bf1..a2c961b4f21 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -487,11 +487,14 @@ class account_logout(delegate.page): can be handled prior to the calling of infogami's standard logout procedure """ - path = "/account/login" + path = "/account/logout" + def POST(self): clear_cookies() + from infogami.core.code import logout as infogami_logout infogami_logout().POST() + class account_verify(delegate.page): """Verify user account.""" From 8501f68dc0f6051c69a6127866ba8afdf23d2fb9 Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 2 Nov 2023 20:11:26 +0800 Subject: [PATCH 5/8] Clear cookies before logging in through admin --- openlibrary/plugins/admin/code.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/openlibrary/plugins/admin/code.py b/openlibrary/plugins/admin/code.py index de4d83bd4a3..99cf0ea3d6c 100644 --- a/openlibrary/plugins/admin/code.py +++ b/openlibrary/plugins/admin/code.py @@ -25,7 +25,7 @@ import openlibrary from openlibrary import accounts - +from openlibrary.accounts.model import clear_cookies from openlibrary.core import admin as admin_stats, helpers as h, imports, cache from openlibrary.core.waitinglist import Stats as WLStats from openlibrary.core.sponsorships import summary, sync_completed_sponsored_books @@ -462,7 +462,10 @@ def POST_set_bot_flag(self, account, bot): def POST_su(self, account): code = account.generate_login_code() + #Clear all existing admin cookies before logging in as another user + clear_cookies() web.setcookie(config.login_cookie_name, code, expires="") + return web.seeother("/") def POST_anonymize_account(self, account, test): From fbb7351e056db5716d01ffc657c48f636f7fd797 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:41:09 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- openlibrary/accounts/model.py | 1 + openlibrary/plugins/admin/code.py | 4 ++-- openlibrary/plugins/upstream/account.py | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/openlibrary/accounts/model.py b/openlibrary/accounts/model.py index cb81d28c016..7f0126942e7 100644 --- a/openlibrary/accounts/model.py +++ b/openlibrary/accounts/model.py @@ -127,6 +127,7 @@ def create_link_doc(key, username, email): "expires_on": expires.isoformat(), } + def clear_cookies(): web.setcookie('pd', "", expires=-1) web.setcookie('sfw', "", expires=-1) diff --git a/openlibrary/plugins/admin/code.py b/openlibrary/plugins/admin/code.py index 99cf0ea3d6c..304e9b35f72 100644 --- a/openlibrary/plugins/admin/code.py +++ b/openlibrary/plugins/admin/code.py @@ -462,10 +462,10 @@ def POST_set_bot_flag(self, account, bot): def POST_su(self, account): code = account.generate_login_code() - #Clear all existing admin cookies before logging in as another user + # Clear all existing admin cookies before logging in as another user clear_cookies() web.setcookie(config.login_cookie_name, code, expires="") - + return web.seeother("/") def POST_anonymize_account(self, account, test): diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index a2c961b4f21..a7c4423a685 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -39,7 +39,7 @@ OpenLibraryAccount, InternetArchiveAccount, valid_email, - clear_cookies + clear_cookies, ) from openlibrary.plugins.upstream import borrow, forms, utils from openlibrary.utils.dateutil import elapsed_time @@ -487,11 +487,13 @@ class account_logout(delegate.page): can be handled prior to the calling of infogami's standard logout procedure """ + path = "/account/logout" def POST(self): clear_cookies() from infogami.core.code import logout as infogami_logout + infogami_logout().POST() From cfe4e3d130eb759c1b22eb51018a7d839300b0cb Mon Sep 17 00:00:00 2001 From: ClementineAccount Date: Fri, 3 Nov 2023 20:45:20 +0800 Subject: [PATCH 7/8] Remove 'redefinition of infogami_logout from line 21' --- openlibrary/plugins/upstream/account.py | 1 - 1 file changed, 1 deletion(-) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index a7c4423a685..08eca06f560 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -18,7 +18,6 @@ ) from infogami.infobase.client import ClientException import infogami.core.code as core -from infogami.core.code import logout as infogami_logout from openlibrary import accounts from openlibrary.i18n import gettext as _ From 81908c43476ef9ae825d1532523c59b5dc17b6e4 Mon Sep 17 00:00:00 2001 From: Mek Date: Mon, 6 Nov 2023 13:09:20 -0800 Subject: [PATCH 8/8] update return value --- openlibrary/plugins/upstream/account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index 08eca06f560..b5ef54bc34b 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -493,7 +493,7 @@ def POST(self): clear_cookies() from infogami.core.code import logout as infogami_logout - infogami_logout().POST() + return infogami_logout().POST() class account_verify(delegate.page):