-
Notifications
You must be signed in to change notification settings - Fork 809
Backchannel Logout #1573
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
Open
lullis
wants to merge
2
commits into
django-oauth:master
Choose a base branch
from
mushroomlabs:1545_backchannel_logout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Backchannel Logout #1573
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
import logging | ||
from datetime import timedelta | ||
|
||
import requests | ||
from django.contrib.auth.signals import user_logged_out | ||
from django.dispatch import receiver | ||
from django.utils import timezone | ||
from jwcrypto import jwt | ||
|
||
from .exceptions import BackchannelLogoutRequestError | ||
from .models import AbstractApplication, get_id_token_model | ||
from .settings import oauth2_settings | ||
|
||
|
||
IDToken = get_id_token_model() | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def send_backchannel_logout_request(id_token, *args, **kwargs): | ||
""" | ||
Send a logout token to the applications backchannel logout uri | ||
""" | ||
|
||
ttl = kwargs.get("ttl") or timedelta(minutes=10) | ||
|
||
try: | ||
assert oauth2_settings.OIDC_BACKCHANNEL_LOGOUT_ENABLED, "Backchannel logout not enabled" | ||
assert id_token.application.algorithm != AbstractApplication.NO_ALGORITHM, ( | ||
"Application must provide signing algorithm" | ||
) | ||
assert id_token.application.backchannel_logout_uri is not None, ( | ||
"URL for backchannel logout not provided by client" | ||
) | ||
|
||
issued_at = timezone.now() | ||
expiration_date = issued_at + ttl | ||
|
||
claims = { | ||
"iss": oauth2_settings.OIDC_ISS_ENDPOINT, | ||
"sub": str(id_token.user.id), | ||
"aud": str(id_token.application.client_id), | ||
"iat": int(issued_at.timestamp()), | ||
"exp": int(expiration_date.timestamp()), | ||
"jti": id_token.jti, | ||
"events": {"http://schemas.openid.net/event/backchannel-logout": {}}, | ||
} | ||
|
||
# Standard JWT header | ||
header = {"typ": "logout+jwt", "alg": id_token.application.algorithm} | ||
|
||
# RS256 consumers expect a kid in the header for verifying the token | ||
if id_token.application.algorithm == AbstractApplication.RS256_ALGORITHM: | ||
header["kid"] = id_token.application.jwk_key.thumbprint() | ||
|
||
token = jwt.JWT( | ||
header=json.dumps(header, default=str), | ||
claims=json.dumps(claims, default=str), | ||
) | ||
|
||
token.make_signed_token(id_token.application.jwk_key) | ||
|
||
headers = {"Content-Type": "application/x-www-form-urlencoded"} | ||
data = {"logout_token": token.serialize()} | ||
response = requests.post(id_token.application.backchannel_logout_uri, headers=headers, data=data) | ||
response.raise_for_status() | ||
except (AssertionError, requests.RequestException) as exc: | ||
raise BackchannelLogoutRequestError(str(exc)) | ||
|
||
|
||
@receiver(user_logged_out) | ||
def on_user_logged_out_maybe_send_backchannel_logout(sender, **kwargs): | ||
handler = oauth2_settings.OIDC_BACKCHANNEL_LOGOUT_HANDLER | ||
if not oauth2_settings.OIDC_BACKCHANNEL_LOGOUT_ENABLED or not callable(handler): | ||
return | ||
|
||
user = kwargs["user"] | ||
id_tokens = IDToken.objects.filter(application__backchannel_logout_uri__isnull=False, user=user) | ||
for id_token in id_tokens: | ||
try: | ||
handler(id_token=id_token) | ||
except BackchannelLogoutRequestError as exc: | ||
logger.warn(str(exc)) |
18 changes: 18 additions & 0 deletions
18
oauth2_provider/migrations/0013_application_backchannel_logout_uri.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,18 @@ | ||
# Generated by Django 5.2 on 2025-06-06 12:42 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('oauth2_provider', '0012_add_token_checksum'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='application', | ||
name='backchannel_logout_uri', | ||
field=models.URLField(blank=True, help_text='Backchannel Logout URI where logout tokens will be sent', null=True), | ||
), | ||
] |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
OIDC_BACKCHANNEL_LOGOUT_HANDLER is not documented. What are the use cases for making a setting to override the logout handler? Is the specification flexible enough that it makes sense to give people the option to override this?
Uh oh!
There was an error while loading. Please reload this page.
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.
The main use-case for a configurable handler is simple: I would like to give consumers of this library a hook to let them execute this function outside of the request cycle. For example, if someone wants to run this as a celery task or implement a function that can keep track of errors. I will add it to the documentation.
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.
Delegating to celery sounds reasonable, especially if people want to implement retries etc. Do we not have logging for error tracking or are you thinking of some particular 3rd party instrumentation? Can you expand on your thoughts in regard to error tracking?
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.
I pushed some changes, you will see that now the handler is made to work on id_tokens, not on the user directly.
My idea was to leave it completely open to the developer using DOT. The way I see it, someone that wants to have the requests on celery could do something like this: