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 6 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 @@ -3,7 +3,7 @@
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 @@ -22,15 +22,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
42 changes: 18 additions & 24 deletions social_core/tests/backends/test_okta.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,30 +144,24 @@ def setUp(self):
status=200,
body=self.openid_config_body,
)
oidc_config = json.loads(self.openid_config_body)

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

HTTPretty.register_uri(
HTTPretty.GET,
oidc_config.get("jwks_uri"),
status=200,
body=json.dumps({"keys": [self.public_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.backend.JWKS_URI = oidc_config.get("jwks_uri")
self.backend.ID_TOKEN_ISSUER = oidc_config.get("issuer")

def pre_complete_callback(self, start_url):
super().pre_complete_callback(start_url)
HTTPretty.register_uri(
"GET",
uri=self.backend.userinfo_url(),
status=200,
body=json.dumps({"preferred_username": self.expected_username}),
content_type="text/json",
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",
)

def test_everything_works(self):
self.do_login()
Loading