-
Notifications
You must be signed in to change notification settings - Fork 0
fix: added logout user before sso pipeline starts #2
Changes from all commits
b9c2682
0ce0e34
af81e6d
a3a8b48
740773a
2968468
596f168
abf3829
17dc283
67da51d
dcf9f58
ae626fa
e877492
4225aa2
00841f3
c5a8382
e099d04
6768c58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,9 +2,27 @@ | |||||
|
|
||||||
| For more information visit https://docs.djangoproject.com/en/dev/topics/auth/customizing/. | ||||||
| """ | ||||||
| import logging | ||||||
| import jwt | ||||||
| from django.contrib.auth import logout | ||||||
| from django.dispatch import Signal | ||||||
| from social_core.backends.oauth import BaseOAuth2 | ||||||
| from edx_toggles.toggles import SettingToggle | ||||||
| from edx_django_utils.monitoring import set_custom_attribute | ||||||
|
|
||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
| # .. toggle_name: ENABLE_OAUTH_SESSION_CLEANUP | ||||||
| # .. toggle_implementation: SettingToggle | ||||||
| # .. toggle_default: True | ||||||
| # .. toggle_description: Controls whether to perform session cleanup during OAuth start. | ||||||
| # When enabled (True), existing user sessions are cleared before OAuth authentication | ||||||
| # to prevent user association conflicts. When disabled (False), session cleanup is skipped. | ||||||
| # This toggle allows for gradual rollout and quick rollback if issues arise. | ||||||
| # .. toggle_use_cases: temporary | ||||||
| # .. toggle_creation_date: 2025-09-15 | ||||||
| # .. toggle_target_removal_date: 2025-11-15 | ||||||
| ENABLE_OAUTH_SESSION_CLEANUP = SettingToggle("ENABLE_OAUTH_SESSION_CLEANUP", default=True) | ||||||
|
|
||||||
| PROFILE_CLAIMS_TO_DETAILS_KEY_MAP = { | ||||||
| 'preferred_username': 'username', | ||||||
|
|
@@ -69,6 +87,56 @@ def logout_url(self): | |||||
| else: | ||||||
| return self.end_session_url() | ||||||
|
|
||||||
| def start(self): | ||||||
| """Initialize OAuth authentication with optional session cleanup.""" | ||||||
|
|
||||||
| # .. custom_attribute_name: session_cleanup.toggle_enabled | ||||||
| # .. custom_attribute_description: Tracks whether the ENABLE_OAUTH_SESSION_CLEANUP | ||||||
| # toggle is enabled during OAuth start. | ||||||
| set_custom_attribute('session_cleanup.toggle_enabled', ENABLE_OAUTH_SESSION_CLEANUP.is_enabled()) | ||||||
Akanshu-2u marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| request = self.strategy.request if hasattr(self.strategy, 'request') else None | ||||||
|
|
||||||
| # .. custom_attribute_name: session_cleanup.has_request | ||||||
| # .. custom_attribute_description: Tracks whether a request object is available | ||||||
| # during OAuth start. True if request exists, False if missing. | ||||||
| set_custom_attribute('session_cleanup.has_request', request is not None) | ||||||
|
|
||||||
| user_authenticated = ( | ||||||
| request is not None and | ||||||
| hasattr(request, 'user') and | ||||||
|
||||||
| hasattr(request, 'user') and |
Akanshu-2u marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Sep 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using getattr with a default value of 'unknown' is inappropriate here. Since we've already verified request.user.is_authenticated is True, the user object is guaranteed to have a username attribute. This should be simplified to request.user.username to avoid logging misleading 'unknown' values.
| existing_username = getattr(request.user, 'username', 'unknown') | |
| existing_username = request.user.username |
robrap marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.