Skip to content

Commit 96338a9

Browse files
Divan009ewanharris
authored andcommitted
feat: token endpoint is now configurable
1 parent b51370d commit 96338a9

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

openfga_sdk/credentials.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
1111
"""
1212

13-
from urllib.parse import urlparse
13+
from urllib.parse import urlparse, urlunparse
1414

1515
from openfga_sdk.exceptions import ApiValueError
1616

@@ -160,6 +160,33 @@ def configuration(self, value):
160160
"""
161161
self._configuration = value
162162

163+
def _parse_issuer(self, issuer: str):
164+
default_endpoint_path = "/oauth/token"
165+
166+
parsed_url = urlparse(issuer.strip())
167+
168+
try:
169+
parsed_url.port
170+
except ValueError as e:
171+
raise ApiValueError(e)
172+
173+
if parsed_url.netloc is None and parsed_url.path is None:
174+
raise ApiValueError("Invalid issuer")
175+
176+
if parsed_url.scheme == "":
177+
parsed_url = urlparse(f"https://{issuer}")
178+
elif parsed_url.scheme not in ("http", "https"):
179+
raise ApiValueError(
180+
f"Invalid issuer scheme {parsed_url.scheme} must be HTTP or HTTPS"
181+
)
182+
183+
if parsed_url.path in ("", "/"):
184+
parsed_url = parsed_url._replace(path=default_endpoint_path)
185+
186+
valid_url = urlunparse(parsed_url)
187+
188+
return valid_url
189+
163190
def validate_credentials_config(self):
164191
"""
165192
Check whether credentials configuration is valid
@@ -190,15 +217,5 @@ def validate_credentials_config(self):
190217
"configuration `{}` requires client_id, client_secret, api_audience and api_issuer defined for client_credentials method."
191218
)
192219
# validate token issuer
193-
combined_url = "https://" + self.configuration.api_issuer
194-
parsed_url = None
195-
try:
196-
parsed_url = urlparse(combined_url)
197-
except ValueError:
198-
raise ApiValueError(
199-
f"api_issuer `{self.configuration.api_issuer}` is invalid"
200-
)
201-
if parsed_url.netloc == "":
202-
raise ApiValueError(
203-
f"api_issuer `{self.configuration.api_issuer}` is invalid"
204-
)
220+
221+
self._parse_issuer(self.configuration.api_issuer)

test/credentials_test.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
1111
"""
1212

13+
import unittest
1314
from unittest import IsolatedAsyncioTestCase
1415

1516
import openfga_sdk
1617
from openfga_sdk.credentials import CredentialConfiguration, Credentials
18+
from openfga_sdk.exceptions import ApiValueError
1719

1820

1921
class TestCredentials(IsolatedAsyncioTestCase):
@@ -172,3 +174,61 @@ def test_configuration_client_credentials_missing_api_audience(self):
172174
)
173175
with self.assertRaises(openfga_sdk.ApiValueError):
174176
credential.validate_credentials_config()
177+
178+
179+
class TestCredentialsIssuer(unittest.TestCase):
180+
def setUp(self):
181+
# Setup a basic configuration that can be modified per test case
182+
self.configuration = CredentialConfiguration(api_issuer="https://example.com")
183+
self.credentials = Credentials(
184+
method="client_credentials", configuration=self.configuration
185+
)
186+
187+
def test_valid_issuer_https(self):
188+
# Test a valid HTTPS URL
189+
self.configuration.api_issuer = "issuer.fga.example "
190+
result = self.credentials._parse_issuer(self.configuration.api_issuer)
191+
self.assertEqual(result, "https://issuer.fga.example/oauth/token")
192+
193+
def test_valid_issuer_with_oauth_endpoint_https(self):
194+
# Test a valid HTTPS URL
195+
self.configuration.api_issuer = "https://example.com/oauth/token"
196+
result = self.credentials._parse_issuer(self.configuration.api_issuer)
197+
self.assertEqual(result, "https://example.com/oauth/token")
198+
199+
def test_valid_issuer_with_some_endpoint_https(self):
200+
# Test a valid HTTPS URL
201+
self.configuration.api_issuer = "https://example.com/oauth/some/endpoint"
202+
result = self.credentials._parse_issuer(self.configuration.api_issuer)
203+
self.assertEqual(result, "https://example.com/oauth/some/endpoint")
204+
205+
def test_valid_issuer_http(self):
206+
# Test a valid HTTP URL
207+
self.configuration.api_issuer = "fga.example/some_endpoint"
208+
result = self.credentials._parse_issuer(self.configuration.api_issuer)
209+
self.assertEqual(result, "https://fga.example/some_endpoint")
210+
211+
def test_invalid_issuer_no_scheme(self):
212+
# Test an issuer URL without a scheme
213+
self.configuration.api_issuer = "https://issuer.fga.example:8080/some_endpoint "
214+
result = self.credentials._parse_issuer(self.configuration.api_issuer)
215+
self.assertEqual(result, "https://issuer.fga.example:8080/some_endpoint")
216+
217+
def test_invalid_issuer_bad_scheme(self):
218+
# Test an issuer with an unsupported scheme
219+
self.configuration.api_issuer = "ftp://example.com"
220+
with self.assertRaises(ApiValueError):
221+
self.credentials._parse_issuer(self.configuration.api_issuer)
222+
223+
def test_invalid_issuer_with_port(self):
224+
# Test an issuer with an unsupported scheme
225+
self.configuration.api_issuer = "https://issuer.fga.example:8080 "
226+
result = self.credentials._parse_issuer(self.configuration.api_issuer)
227+
self.assertEqual(result, "https://issuer.fga.example:8080/oauth/token")
228+
229+
# this should raise error
230+
def test_invalid_issuer_bad_hostname(self):
231+
# Test an issuer with an invalid hostname
232+
self.configuration.api_issuer = "https://example?.com"
233+
with self.assertRaises(ApiValueError):
234+
self.credentials._parse_issuer(self.configuration.api_issuer)

0 commit comments

Comments
 (0)