Skip to content

Commit 875a6b2

Browse files
committed
server/auth: prioritize customer session over web session
We found out that users were troubled by the fact that their own customer portal was showing up instead of the one tied to the customer session token that was present in the URL. With this change, if the backend finds a valid customer session token, it uses it instead of the web cookie session, correctly showing the customer we're trying to see instead of the authenticated user.
1 parent 464d11c commit 875a6b2

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

server/polar/auth/dependencies.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@ async def get_user_session(
4040

4141

4242
async def _get_auth_subject(
43+
customer_session_credentials: tuple[CustomerSession | None, bool] = (None, False),
4344
user_session: UserSession | None = None,
4445
oauth2_credentials: tuple[OAuth2Token | None, bool] = (None, False),
4546
personal_access_token_credentials: tuple[PersonalAccessToken | None, bool] = (
4647
None,
4748
False,
4849
),
49-
customer_session_credentials: tuple[CustomerSession | None, bool] = (None, False),
5050
) -> AuthSubject[Subject]:
51+
# Customer session is prioritized over web session
52+
customer_session, customer_session_authorization_set = customer_session_credentials
53+
if customer_session:
54+
return AuthSubject(
55+
customer_session.customer,
56+
{Scope.customer_portal_write},
57+
AuthMethod.CUSTOMER_SESSION_TOKEN,
58+
)
59+
5160
# Web session
5261
if user_session is not None:
5362
user = user_session.user
@@ -64,7 +73,6 @@ async def _get_auth_subject(
6473
personal_access_token, personal_access_token_authorization_set = (
6574
personal_access_token_credentials
6675
)
67-
customer_session, customer_session_authorization_set = customer_session_credentials
6876

6977
if oauth2_token:
7078
return AuthSubject(
@@ -78,13 +86,6 @@ async def _get_auth_subject(
7886
AuthMethod.PERSONAL_ACCESS_TOKEN,
7987
)
8088

81-
if customer_session:
82-
return AuthSubject(
83-
customer_session.customer,
84-
{Scope.customer_portal_write},
85-
AuthMethod.CUSTOMER_SESSION_TOKEN,
86-
)
87-
8889
if any(
8990
(
9091
oauth2_authorization_set,

0 commit comments

Comments
 (0)