Repository navigation
Fix: CORS issues in third-party auth disconnect by adding JSON endpoint #37100
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
8ac1425
feat: adds custom endpoint for TPA disconnect
wgu-jesse-stewart a5cbb37
Merge branch 'openedx:master' into fix-disconnect-cors
wgu-jesse-stewart 991c774
feat: address feedback
wgu-jesse-stewart a0f0e05
Merge branch 'fix-disconnect-cors' of https://github.com/WGU-Open-edX…
wgu-jesse-stewart 445468c
feat: remove GET from endpoint
wgu-jesse-stewart 65c4d22
fix: lint files
wgu-jesse-stewart 093c562
fix: lint errors
wgu-jesse-stewart 01a7363
fix: update tests
wgu-jesse-stewart 43478e1
fix: address feedback
wgu-jesse-stewart fd4d760
fix: remove comment
wgu-jesse-stewart 835cc24
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart ba17be8
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart b0f41bc
feat: removes comment
wgu-jesse-stewart 56bbcbe
feat: adds backend_instance
wgu-jesse-stewart eb8dde0
fix: lint
wgu-jesse-stewart eecf1d4
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart 7ed0f07
feat: pr feedback
wgu-jesse-stewart bb5c61e
fix: linting
wgu-jesse-stewart f276260
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart d3af088
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart da307cf
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart 338ebed
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart 63dfe98
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart ac89165
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart 6f65452
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart 0e115db
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart cc14612
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart 39b254f
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart df037f5
feat: add logging to exception and return generic error
wgu-jesse-stewart 6c9c748
Merge branch 'master' into fix-disconnect-cors
wgu-jesse-stewart f847c19
Update common/djangoapps/third_party_auth/views.py
wgu-jesse-stewart 498b5d7
Update common/djangoapps/third_party_auth/views.py
wgu-jesse-stewart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
common/djangoapps/third_party_auth/custom_disconnect_view.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """ | ||
| Custom disconnect view that returns JSON instead of redirecting to avoid CORS issues. | ||
| """ | ||
| from django.http import JsonResponse | ||
| from django.contrib.auth.decorators import login_required | ||
| from django.views.decorators.http import require_http_methods | ||
| from django.core.exceptions import ValidationError, PermissionDenied | ||
| from django.db import DatabaseError | ||
| from social_django.utils import psa | ||
| from social_django.models import UserSocialAuth | ||
|
|
||
|
|
||
| @login_required | ||
| @require_http_methods(["POST"]) | ||
| @psa() | ||
| def disconnect_json_view(request, backend, association_id=None): | ||
| """ | ||
| Custom disconnect view that returns JSON response instead of redirecting. | ||
| This prevents CORS issues when called from MFE frontends. | ||
| """ | ||
| user = request.user | ||
|
|
||
| # Check URL parameter first, then POST parameter, and fallback to GET parameter for backward compatibility | ||
| if not association_id: | ||
| association_id = request.POST.get('association_id') or request.GET.get('association_id') | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
|
|
||
| try: | ||
| if association_id: | ||
| # Disconnect specific association by ID | ||
| association = UserSocialAuth.objects.get( | ||
| id=association_id, | ||
| user=user, | ||
| provider=backend | ||
| ) | ||
| association.delete() | ||
| else: | ||
| # Disconnect all associations for this backend | ||
| UserSocialAuth.objects.filter( | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
| user=user, | ||
| provider=backend | ||
| ).delete() | ||
|
|
||
| return JsonResponse({ | ||
| 'success': True, | ||
| 'message': 'Account successfully disconnected', | ||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }) | ||
|
|
||
| except UserSocialAuth.DoesNotExist: | ||
| return JsonResponse({ | ||
| 'success': False, | ||
| 'error': 'Social auth association not found', | ||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }, status=404) | ||
|
|
||
| except ValueError as e: | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
| return JsonResponse({ | ||
| 'success': False, | ||
| 'error': 'Invalid parameter value provided', | ||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }, status=400) | ||
|
|
||
| except TypeError as e: | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
| return JsonResponse({ | ||
| 'success': False, | ||
| 'error': 'Invalid parameter type provided', | ||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }, status=400) | ||
|
|
||
| except DatabaseError as e: | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
| return JsonResponse({ | ||
| 'success': False, | ||
| 'error': 'Database operation failed', | ||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }, status=500) | ||
|
|
||
| except ValidationError as e: | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
| return JsonResponse({ | ||
| 'success': False, | ||
| 'error': 'Validation failed', | ||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }, status=400) | ||
|
|
||
| except PermissionDenied as e: | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
| return JsonResponse({ | ||
| 'success': False, | ||
| 'error': 'Permission denied', | ||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }, status=403) | ||
|
|
||
| except Exception as e: | ||
| return JsonResponse({ | ||
| 'success': False, | ||
| 'error': 'An unexpected error occurred', | ||
|
wgu-jesse-stewart marked this conversation as resolved.
Resolved
|
||
| 'backend': backend, | ||
| 'association_id': association_id | ||
| }, status=500) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.