Skip to content
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

Obtain logout endpoint from OIDC Discovery #17

Merged
merged 1 commit into from
Mar 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion identity/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.0" # Note: Perhaps update ReadTheDocs and README.md too?
__version__ = "0.5.1" # Note: Perhaps update ReadTheDocs and README.md too?
17 changes: 15 additions & 2 deletions identity/web.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import functools
import logging
import time

import requests
import msal


Expand Down Expand Up @@ -233,6 +235,10 @@ def _get_token_for_user(self, scopes, force_refresh=None):
return result
return {"error": "interaction_required", "error_description": "Cache missed"}

@functools.lru_cache(maxsize=1)
def _get_oidc_config(self):
return requests.get(f"{self._authority}/.well-known/openid-configuration").json()

def log_out(self, homepage):
# The vocabulary is "log out" (rather than "sign out") in the specs
# https://openid.net/specs/openid-connect-frontchannel-1_0.html
Expand All @@ -248,8 +254,15 @@ def log_out(self, homepage):
"""
self._session.pop(self._USER, None) # Must
self._session.pop(self._TOKEN_CACHE, None) # Optional
return "{authority}/oauth2/v2.0/logout?post_logout_redirect_uri={hp}".format(
authority=self._authority, hp=homepage)
try:
# Empirically, Microsoft Entra ID's /v2.0 endpoint shows an account picker
# but its default (i.e. v1.0) endpoint will sign out the (only?) account
e = self._get_oidc_config().get("end_session_endpoint")
except requests.exceptions.RequestException as e:
logger.exception("Failed to get OIDC config")
return homepage
else:
return f"{e}?post_logout_redirect_uri={homepage}" if e else homepage

def get_token_for_client(self, scopes):
"""Get access token for the current app, with specified scopes.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ long_description_content_type = text/markdown
python_requires = >=3.7
install_requires =
msal>=1.16,<2
# requests>=2.0.0,<3
requests>=2.0.0,<3
# importlib; python_version == "2.6"
# See also https://setuptools.readthedocs.io/en/latest/userguide/quickstart.html#dependency-management

Expand Down
Loading