Skip to content

Commit e1425cd

Browse files
authored
Merge pull request #642 from crstrn13/token_introspect
Add test for token introspection.
2 parents 7d040d4 + d7fa555 commit e1425cd

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

testsuite/kuadrant/policy/authorization/sections.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
Cache,
1616
ResourceAttributes,
1717
)
18+
from testsuite.oidc.keycloak import Keycloak
1819
from testsuite.utils import asdict
19-
from testsuite.kubernetes import modify, Selector
20+
from testsuite.kubernetes import modify, Selector, KubernetesObject
2021

2122
if TYPE_CHECKING:
2223
from .auth_config import AuthConfig
@@ -172,6 +173,21 @@ def add_plain(self, name, auth_json, **common_features):
172173
"""Adds plain identity"""
173174
self.add_item(name, {"plain": asdict(ValueFrom(auth_json))}, **common_features)
174175

176+
@modify
177+
def add_oauth2_introspection(self, name: str, keycloak: Keycloak, client_secret: KubernetesObject):
178+
"""Add introspection for tokens with keycloak and client credentials stored in secret"""
179+
self.add_item(
180+
name,
181+
{
182+
"oauth2Introspection": {
183+
"endpoint": f"{keycloak.server_url}/realms/{keycloak.realm_name}/protocol/openid-connect/token/"
184+
f"introspect",
185+
"tokenTypeHint": "requesting_party_token",
186+
"credentialsRef": {"name": client_secret.name()},
187+
}
188+
},
189+
)
190+
175191

176192
class MetadataSection(Section):
177193
"""Section which contains metadata configuration"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Test for checking I OAuth 2.0 access tokens (e.g. opaque tokens) for online user data and token validation in
3+
request-time.
4+
https://github.com/Kuadrant/authorino/blob/main/docs/user-guides/oauth2-token-introspection.md
5+
"""
6+
7+
import pytest
8+
9+
from testsuite.httpx.auth import HttpxOidcClientAuth
10+
11+
pytestmark = [pytest.mark.authorino]
12+
13+
14+
@pytest.fixture(scope="module")
15+
def client_secret(create_client_secret, keycloak, blame):
16+
"""create the required secrets that will be used by Authorino to authenticate with Keycloak"""
17+
return create_client_secret(blame("secret"), keycloak.client.auth_id, keycloak.client.secret)
18+
19+
20+
@pytest.fixture(scope="function")
21+
def _auth(oidc_provider):
22+
return HttpxOidcClientAuth(oidc_provider.get_token, "authorization")
23+
24+
25+
@pytest.fixture(scope="module")
26+
def authorization(client_secret, authorization, keycloak):
27+
"""
28+
On every request, Authorino will try to verify the token remotely with the Keycloak server with the introspect
29+
endpoint. It's credentials are referenced from the secret created before.
30+
"""
31+
authorization.identity.add_oauth2_introspection("default", keycloak, client_secret)
32+
return authorization
33+
34+
35+
def test_no_token(client):
36+
"""Test access with no auth"""
37+
response = client.get("get")
38+
assert response.status_code == 401
39+
40+
41+
def test_access_token(client, _auth):
42+
"""Tests auth with token granted from fixture"""
43+
response = client.get("get", auth=_auth)
44+
assert response.status_code == 200
45+
46+
47+
def test_revoked_token(client, _auth, keycloak):
48+
"""Revoke token by logging out and test if is unauthorized"""
49+
keycloak.oidc_client.logout(_auth.token.refresh_token)
50+
response = client.get("get", auth=_auth)
51+
assert response.status_code == 401

0 commit comments

Comments
 (0)