Skip to content

added user email upn data in request session #364 #365

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions django_auth_adfs/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you're not overriding validate_access_token in your code to achieve the logic you want?

signals.adfs_claims_processed.send(sender=self, request=request, claims=claims)

user = self.process_access_token(access_token, adfs_response)
return user

Expand Down
5 changes: 5 additions & 0 deletions django_auth_adfs/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
# * claims
# * adfs_response
post_authenticate = Signal()

# Arguments sent with the signal:
# * request
# * claims
adfs_claims_processed = Signal()
21 changes: 21 additions & 0 deletions docs/signals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Loading