From d7a1958f263224d686282f9ec28d671d90e09c2e Mon Sep 17 00:00:00 2001 From: Imam Khan Date: Mon, 31 Mar 2025 15:59:56 +0530 Subject: [PATCH 1/6] added user email upn data in request session #364 --- django_auth_adfs/backend.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/django_auth_adfs/backend.py b/django_auth_adfs/backend.py index c3165cf..6710c7b 100644 --- a/django_auth_adfs/backend.py +++ b/django_auth_adfs/backend.py @@ -420,6 +420,15 @@ def authenticate(self, request=None, authorization_code=None, **kwargs): adfs_response = self.exchange_auth_code(authorization_code, request) access_token = adfs_response["access_token"] + + # Extract claims before user lookup + claims = self.validate_access_token(access_token) + + # Store claims in session so it's available in login_failed() + if request and hasattr(request, "session"): + username_claim = settings.USERNAME_CLAIM + request.session["username_claim"] = claims[username_claim] + user = self.process_access_token(access_token, adfs_response) return user From 3e0d8861896d411e51782d2b83bb11e0e8603ba6 Mon Sep 17 00:00:00 2001 From: Imam Khan Date: Mon, 31 Mar 2025 16:33:18 +0530 Subject: [PATCH 2/6] removed whitespace from line 431 as per Lint Test #364 --- django_auth_adfs/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_auth_adfs/backend.py b/django_auth_adfs/backend.py index 6710c7b..7028300 100644 --- a/django_auth_adfs/backend.py +++ b/django_auth_adfs/backend.py @@ -428,7 +428,7 @@ def authenticate(self, request=None, authorization_code=None, **kwargs): if request and hasattr(request, "session"): username_claim = settings.USERNAME_CLAIM request.session["username_claim"] = claims[username_claim] - + user = self.process_access_token(access_token, adfs_response) return user From fc473c89e033df6cf8bafdee19df27d7050b8cc2 Mon Sep 17 00:00:00 2001 From: Imam Khan Date: Mon, 31 Mar 2025 16:35:02 +0530 Subject: [PATCH 3/6] removed whitespace from line 431 as per Lint Test #364 --- django_auth_adfs/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_auth_adfs/backend.py b/django_auth_adfs/backend.py index 7028300..f39217e 100644 --- a/django_auth_adfs/backend.py +++ b/django_auth_adfs/backend.py @@ -428,7 +428,7 @@ def authenticate(self, request=None, authorization_code=None, **kwargs): if request and hasattr(request, "session"): username_claim = settings.USERNAME_CLAIM request.session["username_claim"] = claims[username_claim] - + user = self.process_access_token(access_token, adfs_response) return user From b11e3f69619fbeb17cee8e3889d6ae377198c227 Mon Sep 17 00:00:00 2001 From: Imam Khan Date: Mon, 31 Mar 2025 16:36:25 +0530 Subject: [PATCH 4/6] removed space from comment from line 424 as per Lint Test #364 --- django_auth_adfs/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_auth_adfs/backend.py b/django_auth_adfs/backend.py index f39217e..146e519 100644 --- a/django_auth_adfs/backend.py +++ b/django_auth_adfs/backend.py @@ -421,7 +421,7 @@ def authenticate(self, request=None, authorization_code=None, **kwargs): adfs_response = self.exchange_auth_code(authorization_code, request) access_token = adfs_response["access_token"] - # Extract claims before user lookup + # Extract claims before user lookup claims = self.validate_access_token(access_token) # Store claims in session so it's available in login_failed() From 690c801784de7a45c0934994fc15f5754d8e2a03 Mon Sep 17 00:00:00 2001 From: Imam Khan Date: Thu, 3 Apr 2025 20:33:34 +0530 Subject: [PATCH 5/6] added hook for saving email via username_claim in request session #364 --- django_auth_adfs/backend.py | 6 +----- django_auth_adfs/signals.py | 5 +++++ docs/signals.rst | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/django_auth_adfs/backend.py b/django_auth_adfs/backend.py index 146e519..b601104 100644 --- a/django_auth_adfs/backend.py +++ b/django_auth_adfs/backend.py @@ -423,11 +423,7 @@ def authenticate(self, request=None, authorization_code=None, **kwargs): # Extract claims before user lookup claims = self.validate_access_token(access_token) - - # Store claims in session so it's available in login_failed() - if request and hasattr(request, "session"): - username_claim = settings.USERNAME_CLAIM - request.session["username_claim"] = claims[username_claim] + signals.adfs_claims_processed.send(sender=self, request=request, claims=claims) user = self.process_access_token(access_token, adfs_response) return user diff --git a/django_auth_adfs/signals.py b/django_auth_adfs/signals.py index 0f15a37..2c8a52e 100644 --- a/django_auth_adfs/signals.py +++ b/django_auth_adfs/signals.py @@ -5,3 +5,8 @@ # * claims # * adfs_response post_authenticate = Signal() + +# Arguments sent with the signal: +# * request +# * claims +adfs_claims_processed = Signal() \ No newline at end of file diff --git a/docs/signals.rst b/docs/signals.rst index cdb630a..b3a119d 100644 --- a/docs/signals.rst +++ b/docs/signals.rst @@ -16,6 +16,14 @@ following signals are supported: * ``adfs_response`` (``dict|None``): used in the ``AdfsAuthCodeBackend`` to provide the full response received from the server when exchanging an authorization code for an access token. +* ``adfs_claims_processed``: sent after a user has been authenticated through ``AdfsAuthCodeBackend``. The + signal is sent after after access_token is received, e.g. extacting user email when it is not registered in Django App + In addition to the sender, the signal includes the request object, and the claims dictionary as arguments for the signal handler: + + * ``sender`` (``AdfsAuthCodeBackend``): the backend instance from which the signal was triggered. + * ``request`` (``WSGIRequest``): the request object. + * ``claims`` (``dict``): the decoded access token JWT, which contains all claims sent from the identity provider. + To use a signal in your application: .. code-block:: python @@ -28,4 +36,17 @@ To use a signal in your application: def handle_post_authenticate(sender, user, claims, adfs_response=None, **kwargs): user.do_post_auth_steps(claims, adfs_response) +To get store Email id Request session: + +.. code-block:: python + + from django.dispatch import receiver + from django_auth_adfs.signals import adfs_claims_processed + + @receiver(adfs_claims_processed) + def handle_adfs_claims(sender, request, claims, **kwargs): + print("Signal is received") + if request and hasattr(request, "session"): + username_claim = settings.AUTH_ADFS['USERNAME_CLAIM'] + request.session["username_claim"] = claims[username_claim] \ No newline at end of file From e586618ed89dcf801fcab627cca8c2a9b5722ff7 Mon Sep 17 00:00:00 2001 From: Imam Khan Date: Thu, 3 Apr 2025 20:37:53 +0530 Subject: [PATCH 6/6] added new line at end in signals.py as per linting test #364 --- django_auth_adfs/signals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_auth_adfs/signals.py b/django_auth_adfs/signals.py index 2c8a52e..b88dac6 100644 --- a/django_auth_adfs/signals.py +++ b/django_auth_adfs/signals.py @@ -9,4 +9,4 @@ # Arguments sent with the signal: # * request # * claims -adfs_claims_processed = Signal() \ No newline at end of file +adfs_claims_processed = Signal()