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

Bug/okta OIDC configuration url fix #663

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 19 additions & 8 deletions social_core/backends/okta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Okta OAuth2 and OpenIdConnect:
https://python-social-auth.readthedocs.io/en/latest/backends/okta.html
"""
from urllib.parse import urljoin
from urllib.parse import urljoin, urlparse, urlunparse

from ..utils import append_slash
from .oauth import BaseOAuth2
Expand All @@ -21,15 +21,26 @@ def access_token_url(self):
def _url(self, path):
return urljoin(append_slash(self.setting("API_URL")), path)

def oidc_config(self):
return self.get_json(
self._url(
"/.well-known/openid-configuration?client_id={}".format(
self.setting("KEY")
)
)
def oidc_config_url(self):
# https://developer.okta.com/docs/reference/api/oidc/#well-known-openid-configuration
url = urlparse(self.api_url())

# If the URL path does not contain an authorizedServerId, we need
# to truncate the path in order to generate a proper openid-configuration
# URL.
if url.path == "/oauth2/":
url = url._replace(path="")

return urljoin(
urlunparse(url),
"./.well-known/openid-configuration?client_id={}".format(
self.setting("KEY")
),
)

def oidc_config(self):
return self.get_json(self.oidc_config_url())


class OktaOAuth2(OktaMixin, BaseOAuth2):
"""Okta OAuth authentication backend"""
Expand Down
21 changes: 21 additions & 0 deletions social_core/tests/backends/test_okta.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,41 @@
status=200,
body=self.openid_config_body,
)
oidc_config = json.loads(self.openid_config_body)

Check failure on line 145 in social_core/tests/backends/test_okta.py

View workflow job for this annotation

GitHub Actions / flake8

local variable 'oidc_config' is assigned to but never used

def jwks(_request, _uri, headers):
return 200, headers, json.dumps({"keys": [self.key]})

def test_okta_oidc_config(self):
Copy link
Member

Choose a reason for hiding this comment

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

This decoupled usage of oidc_config variable from its definition what causes test to fail.

# With no custom authorization server
self.strategy.set_settings(
{
"SOCIAL_AUTH_OKTA_OPENIDCONNECT_API_URL": "https://dev-000000.oktapreview.com/oauth2",
}
)
self.assertEqual(
self.backend.oidc_config_url(),
"https://dev-000000.oktapreview.com/.well-known/openid-configuration?client_id=a-key",
)
self.strategy.set_settings(
{
"SOCIAL_AUTH_OKTA_OPENIDCONNECT_API_URL": "https://dev-000000.oktapreview.com/oauth2/id-123456",
}
)
self.assertEqual(
self.backend.oidc_config_url(),
"https://dev-000000.oktapreview.com/oauth2/id-123456/.well-known/openid-configuration?client_id=a-key",
)

HTTPretty.register_uri(
HTTPretty.GET,
oidc_config.get("jwks_uri"),

Check failure on line 173 in social_core/tests/backends/test_okta.py

View workflow job for this annotation

GitHub Actions / flake8

undefined name 'oidc_config'
status=200,
body=json.dumps({"keys": [self.public_key]}),
)

self.backend.JWKS_URI = oidc_config.get("jwks_uri")

Check failure on line 178 in social_core/tests/backends/test_okta.py

View workflow job for this annotation

GitHub Actions / flake8

undefined name 'oidc_config'
self.backend.ID_TOKEN_ISSUER = oidc_config.get("issuer")

Check failure on line 179 in social_core/tests/backends/test_okta.py

View workflow job for this annotation

GitHub Actions / flake8

undefined name 'oidc_config'

def pre_complete_callback(self, start_url):
super().pre_complete_callback(start_url)
Expand Down
Loading