-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: create a new pipeline step that validates the national id base … #233
Changes from all commits
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 |
---|---|---|
|
@@ -4,15 +4,21 @@ | |
social_details: Allows to map response fields to user standard fields. | ||
invalidate_current_user: Sets to None the current user. | ||
""" | ||
import logging | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import logout | ||
from django.http import HttpResponseForbidden | ||
from django.shortcuts import redirect | ||
from django.utils.translation import gettext_lazy as _ | ||
from social_core.pipeline.social_auth import associate_user | ||
from social_core.pipeline.social_auth import social_details as social_core_details | ||
|
||
from eox_nelp.edxapp_wrapper.edxmako import edxmako | ||
from eox_nelp.third_party_auth.utils import match_user_using_uid_query | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def social_details(backend, details, response, *args, **kwargs): | ||
"""This is an extension of `social_core.pipeline.social_auth.social_details` that allows | ||
|
@@ -143,3 +149,35 @@ def disallow_staff_superuser_users( # pylint: disable=unused-argument | |
) | ||
) | ||
return {} | ||
|
||
|
||
def validate_national_id_and_associate_user(request, backend, uid, *args, user=None, social=None, **kwargs): | ||
""" | ||
Validates the user's national ID against the provided SAML UID before associating | ||
a SAML identity with a Django user. If validation fails, the session is ended, and | ||
the user is redirected to registration. | ||
|
||
Args: | ||
request (HttpRequest): The HTTP request object. | ||
backend: The authentication backend used, such as SAML. | ||
uid (str): Unique identifier from SAML (e.g., user ID). | ||
user (User, optional): Django user instance, if found. | ||
social (optional): Existing social authentication data, if found. | ||
|
||
Returns: | ||
If the UID validation succeeds, proceeds to associate the user with social auth. | ||
Otherwise, logs out the current session and redirects to the registration page. | ||
""" | ||
national_id = user.extrainfo.national_id if user and hasattr(user, "extrainfo") else "" | ||
|
||
if national_id and uid.endswith(national_id): | ||
return associate_user(backend, uid, user, social, *args, **kwargs) | ||
|
||
logger.warning( | ||
"User association failed: UID does not end with the user's national ID. UID: %s, National ID: %s", | ||
uid, | ||
national_id, | ||
) | ||
logout(request) | ||
|
||
return redirect("/register") | ||
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. Here is a redirect. Do you think this pipe would have 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. That decorator doesn't fit in this context at all, when I was developing this pipe I included this |
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.
Could you add a log here to know that this pipeline is working blocking incorrect associate users...
eg like this log