|
| 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