-
Notifications
You must be signed in to change notification settings - Fork 24
fix:bug in third party authentication email and name update #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
68fbf10
17019a2
55a1470
b9655b0
ac6a481
49e3b35
3be8be1
933a0dc
6435564
0ca9058
9eb3246
573ebfb
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,15 @@ Unreleased | |||||
| ---------- | ||||||
|
|
||||||
| * | ||||||
| [4.6.0] - 2025-06-09 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might update again, but this is closer.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be marked resolved. |
||||||
| -------------------- | ||||||
|
|
||||||
| Changed | ||||||
| ~~~~~~~ | ||||||
|
|
||||||
| * Improved test coverage by replacing MagicMock with real load_strategy() implementation. | ||||||
| * Fixed email update handling in authentication pipeline. | ||||||
| * Added logging for email updates. | ||||||
|
|
||||||
| Added | ||||||
| ~~~~~~~ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,12 +1,27 @@ | ||||||||||
| """Python-social-auth pipeline functions. | ||||||||||
|
|
||||||||||
| For more info visit http://python-social-auth.readthedocs.org/en/latest/pipeline.html. | ||||||||||
| For more info visit https://python-social-auth.readthedocs.io/en/latest/pipeline.html. | ||||||||||
| """ | ||||||||||
| import logging | ||||||||||
| from django.contrib.auth import get_user_model | ||||||||||
| from edx_toggles.toggles import SettingToggle | ||||||||||
| from edx_django_utils.monitoring import set_custom_attribute | ||||||||||
|
|
||||||||||
|
|
||||||||||
| logger = logging.getLogger(__name__) | ||||||||||
| User = get_user_model() | ||||||||||
|
|
||||||||||
| # .. toggle_name: SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH | ||||||||||
| # .. toggle_implementation: SettingToggle | ||||||||||
| # .. toggle_default: False | ||||||||||
| # .. toggle_description: Determines whether to block email updates when usernames don't match. | ||||||||||
| # When enabled (True), email updates will be blocked when the username in social auth details | ||||||||||
| # doesn't match the user's username. When disabled (False), email updates will proceed regardless | ||||||||||
| # of username mismatches.This will be used for a temporary rollout. | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Original suggestion had a space.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be marked resolved. |
||||||||||
| # .. toggle_use_cases: temporary | ||||||||||
| # .. toggle_creation_date: 2025-06-05 | ||||||||||
| # .. toggle_target_removal_date: 2026-06-05 | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's give a shorter window for follow-up. I also updated the date.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be marked resolved. |
||||||||||
| SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH = SettingToggle("SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH", default=False) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| # pylint: disable=unused-argument | ||||||||||
| # The function parameters must be named exactly as they are below. | ||||||||||
|
|
@@ -34,8 +49,51 @@ def get_user_if_exists(strategy, details, user=None, *args, **kwargs): # pylint | |||||||||
| def update_email(strategy, details, user=None, *args, **kwargs): # pylint: disable=keyword-arg-before-vararg | ||||||||||
| """Update the user's email address using data from provider.""" | ||||||||||
| if user: | ||||||||||
| email = details.get('email') | ||||||||||
| # Get usernames for comparison, using defensive coding | ||||||||||
| details_username = details.get('username') | ||||||||||
| user_username = getattr(user, 'username', None) | ||||||||||
| # Check if usernames match | ||||||||||
| username_match = details_username == user_username | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this were
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be marked resolved. |
||||||||||
|
|
||||||||||
| # .. custom_attribute_name: update_email.username_mismatch | ||||||||||
| # .. custom_attribute_description: Tracks whether there's a mismatch between | ||||||||||
| # the username in the social details and the user's actual username. | ||||||||||
| # True if usernames don't match, False if they match. | ||||||||||
| set_custom_attribute('update_email.username_mismatch', not username_match) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow openedx/edx-django-utils#328 for annotating/documenting each custom attribute.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be marked resolved. |
||||||||||
|
|
||||||||||
| if not username_match: | ||||||||||
| # Log warning and set additional custom attributes for mismatches | ||||||||||
| logger.warning( | ||||||||||
| "Username mismatch during email update. User username: %s, Details username: %s", | ||||||||||
| user_username, | ||||||||||
| details_username | ||||||||||
| ) | ||||||||||
| # .. custom_attribute_name: update_email.details_username | ||||||||||
| # .. custom_attribute_description: Records the username provided in the | ||||||||||
| # social details when a mismatch occurs with the user's username. | ||||||||||
| set_custom_attribute('update_email.details_username', details_username) | ||||||||||
|
|
||||||||||
| # .. custom_attribute_name: update_email.user_username | ||||||||||
| # .. custom_attribute_description: Records the actual username of the user | ||||||||||
| # when a mismatch occurs with the social details username. | ||||||||||
| set_custom_attribute('update_email.user_username', user_username) | ||||||||||
|
|
||||||||||
| # .. custom_attribute_name: update_email.details_has_email | ||||||||||
| # .. custom_attribute_description: Records whether the details contain an email | ||||||||||
| # when a username mismatch occurs, to identify potential edge cases. | ||||||||||
| set_custom_attribute('update_email.details_has_email', details.get('email') is not None) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure when and if you might get an empty email here, so let's use
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be marked resolved. |
||||||||||
|
|
||||||||||
| # Only exit if the toggle is enabled | ||||||||||
| if SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH.is_enabled(): | ||||||||||
| logger.warning( | ||||||||||
| "Skipping email update for user %s due to username mismatch and " | ||||||||||
| "SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH toggle enabled", | ||||||||||
| user_username | ||||||||||
| ) | ||||||||||
| return # Exit without updating email | ||||||||||
|
|
||||||||||
| # Proceed with email update only if usernames match or toggle is disabled | ||||||||||
| email = details.get('email') | ||||||||||
| if email and user.email != email: | ||||||||||
| user.email = email | ||||||||||
| strategy.storage.user.changed(user) | ||||||||||
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.
Switching this to a blank line:
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.
This can be marked resolved.