-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
providers/oauth2: cache ProviderInfoView get_claims #24890
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
Merged
+112
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Provider info (OpenID discovery) tests""" | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| from django.urls import reverse | ||
|
|
||
| from authentik.blueprints.tests import apply_blueprint | ||
| from authentik.core.models import Application | ||
| from authentik.core.tests.utils import create_test_flow | ||
| from authentik.lib.generators import generate_id | ||
| from authentik.providers.oauth2.models import ( | ||
| OAuth2Provider, | ||
| RedirectURI, | ||
| RedirectURIMatchingMode, | ||
| ScopeMapping, | ||
| ) | ||
| from authentik.providers.oauth2.tests.utils import OAuthTestCase | ||
| from authentik.providers.oauth2.views.provider import ProviderInfoView | ||
|
|
||
|
|
||
| class TestProviderInfo(OAuthTestCase): | ||
| """Test provider info view""" | ||
|
|
||
| @apply_blueprint("system/providers-oauth2.yaml") | ||
| def setUp(self) -> None: | ||
| super().setUp() | ||
| self.provider = OAuth2Provider.objects.create( | ||
| name=generate_id(), | ||
| client_id=generate_id(), | ||
| authorization_flow=create_test_flow(), | ||
| redirect_uris=[RedirectURI(RedirectURIMatchingMode.STRICT, "http://local.invalid")], | ||
| signing_key=self.keypair, | ||
| ) | ||
| self.provider.property_mappings.set(ScopeMapping.objects.all()) | ||
| self.app = Application.objects.create( | ||
| name=generate_id(), slug=generate_id(), provider=self.provider | ||
| ) | ||
|
|
||
| def get_info(self, app: Application | None = None) -> dict: | ||
| """Fetch the discovery document""" | ||
| response = self.client.get( | ||
| reverse( | ||
| "authentik_providers_oauth2:provider-info", | ||
| kwargs={"application_slug": (app or self.app).slug}, | ||
| ) | ||
| ) | ||
| self.assertEqual(response.status_code, 200) | ||
| return response.json() | ||
|
|
||
| def test_info(self): | ||
| """Test discovery document""" | ||
| body = self.get_info() | ||
| self.assertTrue(body["issuer"].endswith(f"/application/o/{self.app.slug}/")) | ||
| self.assertTrue(body["token_endpoint"].endswith("/token/")) | ||
| self.assertIn("openid", body["scopes_supported"]) | ||
| self.assertIn("email", body["scopes_supported"]) | ||
| for claim in ["sub", "iss", "aud", "exp", "iat", "acr", "amr", "nonce"]: | ||
| self.assertIn(claim, body["claims_supported"]) | ||
| # Claims from the default scope mappings | ||
| self.assertIn("email", body["claims_supported"]) | ||
| self.assertIn("preferred_username", body["claims_supported"]) | ||
|
|
||
| def test_claims_cached(self): | ||
| """Test claims are only evaluated once""" | ||
| claims = self.get_info()["claims_supported"] | ||
| # Anything hitting get_claims again means the cache didn't take | ||
| with patch.object(ProviderInfoView, "get_claims", return_value=claims) as get_claims: | ||
| self.assertEqual(self.get_info()["claims_supported"], claims) | ||
| get_claims.assert_not_called() | ||
|
|
||
| def test_claims_cached_per_provider(self): | ||
| """Test cached claims aren't shared between providers""" | ||
| self.get_info() | ||
| other = OAuth2Provider.objects.create( | ||
| name=generate_id(), | ||
| client_id=generate_id(), | ||
| authorization_flow=create_test_flow(), | ||
| redirect_uris=[RedirectURI(RedirectURIMatchingMode.STRICT, "http://local.invalid")], | ||
| signing_key=self.keypair, | ||
| ) | ||
| other.property_mappings.set( | ||
| ScopeMapping.objects.filter(managed="goauthentik.io/providers/oauth2/scope-openid") | ||
| ) | ||
| other_app = Application.objects.create( | ||
| name=generate_id(), slug=generate_id(), provider=other | ||
| ) | ||
| self.assertNotIn("email", self.get_info(other_app)["claims_supported"]) | ||
| self.assertIn("email", self.get_info()["claims_supported"]) | ||
|
|
||
| def test_claims_mapping_error(self): | ||
| """Test failing scope mapping is skipped""" | ||
| self.provider.property_mappings.add( | ||
| ScopeMapping.objects.create( | ||
| name=generate_id(), | ||
| scope_name=generate_id(), | ||
| expression="raise Exception('test')", | ||
| ) | ||
| ) | ||
| body = self.get_info() | ||
| self.assertIn("email", body["claims_supported"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth caching on the hash of something like
ScopeMapping.objects.filter(provider=provider).order_by("scope_name").values_list("pk", flat=True)instead? So we get cache invalidation "for free"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point, I was gonna add a signal on ScopeMapping post_save that prunes the cache for each of the providers it uses