Skip to content

Commit 5996dbd

Browse files
committed
Always do OIDC discovery
1 parent 0ffffc9 commit 5996dbd

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

tests/test_client.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
from tests import unittest
1515

1616

17-
THIS_FOLDER = os.path.dirname(__file__)
1817
CONFIG_FILENAME = "config.json"
1918

2019
def load_conf(filename):
2120
"""
2221
Example of a configuration file:
2322
2423
{
25-
"Note": "the following server_configuration is optional",
26-
"server_configuration": {
24+
"Note": "the OpenID Discovery will be updated by following optional content",
25+
"openid_configuration": {
2726
"authorization_endpoint": "https://example.com/tenant/oauth2/authorize",
2827
"token_endpoint": "https://example.com/tenant/oauth2/token",
2928
"device_authorization_endpoint": "device_authorization"
@@ -35,7 +34,7 @@ def load_conf(filename):
3534
"scope": ["your_scope"],
3635
"resource": "Some IdP needs this",
3736
38-
"authority": "https://example.com/tenant/",
37+
"oidp": "https://example.com/tenant/",
3938
"username": "[email protected]",
4039
"password": "I could tell you but then I would have to kill you",
4140
@@ -48,20 +47,28 @@ def load_conf(filename):
4847
except:
4948
logging.warn("Unable to open/read JSON configuration %s" % filename)
5049
raise
51-
if not conf.get("server_configuration"): # Then we do a discovery
50+
openid_configuration = {}
51+
try:
5252
# The following line may duplicate a '/' at the joining point,
5353
# but requests.get(...) would still work.
54-
# Besides, standard urljoin(...) is picky on insisting authority ends with '/'
55-
discovery_uri = conf["authority"] + '/.well-known/openid-configuration'
56-
conf["server_configuration"] = requests.get(discovery_uri).json()
57-
if conf["server_configuration"].get("device_authorization_endpoint"):
54+
# Besides, standard urljoin(...) is picky on insisting oidp ends with '/'
55+
discovery_uri = conf["oidp"] + '/.well-known/openid-configuration'
56+
openid_configuration.update(requests.get(discovery_uri).json())
57+
except:
58+
logging.warn("openid-configuration uri not accesible: %s", discovery_uri)
59+
openid_configuration.update(conf.get("openid_configuration", {}))
60+
if openid_configuration.get("device_authorization_endpoint"):
5861
# The following urljoin(..., ...) trick allows a "path_name" shorthand
59-
conf["server_configuration"]["device_authorization_endpoint"] = urljoin(
60-
conf["server_configuration"].get("authorization_endpoint", ""),
61-
conf["server_configuration"].get("device_authorization_endpoint", ""))
62+
openid_configuration["device_authorization_endpoint"] = urljoin(
63+
openid_configuration.get("token_endpoint", ""),
64+
openid_configuration.get("device_authorization_endpoint", ""))
65+
conf["openid_configuration"] = openid_configuration
6266
return conf
6367

64-
CONFIG = load_conf(os.path.join(THIS_FOLDER, 'config.json')) or {}
68+
THIS_FOLDER = os.path.dirname(__file__)
69+
CONFIG = load_conf(os.path.join(THIS_FOLDER, CONFIG_FILENAME)) or {}
70+
71+
logging.basicConfig(level=logging.DEBUG)
6572

6673

6774
class Oauth2TestCase(unittest.TestCase):
@@ -86,7 +93,7 @@ def setUpClass(cls):
8693
cls.client = Client(
8794
CONFIG['client_id'],
8895
client_secret=CONFIG.get('client_secret'),
89-
configuration=CONFIG["server_configuration"])
96+
configuration=CONFIG["openid_configuration"])
9097

9198
@unittest.skipUnless("client_secret" in CONFIG, "client_secret missing")
9299
def test_client_credentials(self):
@@ -104,7 +111,7 @@ def test_username_password(self):
104111
self.assertLoosely(result)
105112

106113
@unittest.skipUnless(
107-
"authorization_endpoint" in CONFIG.get("server_configuration", {}),
114+
"authorization_endpoint" in CONFIG.get("openid_configuration", {}),
108115
"authorization_endpoint missing")
109116
def test_auth_code(self):
110117
port = CONFIG.get("listen_port", 44331)
@@ -123,7 +130,7 @@ def test_auth_code(self):
123130
self.assertLoosely(result, lambda: self.assertIn('access_token', result))
124131

125132
@unittest.skipUnless(
126-
CONFIG.get("server_configuration", {}).get("device_authorization_endpoint"),
133+
CONFIG.get("openid_configuration", {}).get("device_authorization_endpoint"),
127134
"device_authorization_endpoint is missing")
128135
def test_device_flow(self):
129136
flow = self.client.initiate_device_flow(scope=CONFIG.get("scope"))

0 commit comments

Comments
 (0)