Test a resource server#
This guide shows how to test a resource server that checks access tokens with the IAM server.
It is meant for developers of an API that accepts OAuth2 bearer tokens.
It covers token introspection, as defined by RFC 7662.
The examples use the user and client fixtures from Create users, groups and clients.
It does not cover JWT access tokens (RFC 9068):
random_token() creates opaque tokens only.
Configure the resource server#
The tested resource server sends each token to the introspection endpoint of the IAM server,
at oauth/introspect under iam_server.url.
It authenticates with the credentials of the client fixture.
The examples of this page use a Flask application protected by Authlib.
Its /resource endpoint needs the profile scope.
import pytest
import requests
from authlib.integrations.flask_oauth2 import ResourceProtector
from authlib.oauth2.rfc7662 import IntrospectTokenValidator
from flask import Flask
@pytest.fixture
def app(iam_server, client):
class IAMTokenValidator(IntrospectTokenValidator):
def introspect_token(self, token_string):
response = requests.post(
f"{iam_server.url}oauth/introspect",
data={"token": token_string, "token_type_hint": "access_token"},
auth=(client.client_id, client.client_secret),
)
return response.json()
require_oauth = ResourceProtector()
require_oauth.register_token_validator(IAMTokenValidator())
app = Flask(__name__)
@app.route("/resource")
@require_oauth("profile")
def resource():
return {"success": True}
return app
@pytest.fixture
def test_client(app):
return app.test_client()
In a real project, the app fixture creates the tested resource server with the same settings.
Create access tokens#
random_token() creates a token for a client.
With a user as subject, the token acts for this user.
With subject=None, the token belongs to the client only, as with the client credentials grant.
The token has the scopes of the client, and lasts one hour.
@pytest.fixture
def user_token(iam_server, client, user):
token = iam_server.random_token(subject=user, client=client)
yield token
iam_server.backend.delete(token)
@pytest.fixture
def client_token(iam_server, client):
token = iam_server.random_token(subject=None, client=client)
yield token
iam_server.backend.delete(token)
Check the access#
A valid token gives access to the resource:
def test_user_token(test_client, user_token):
res = test_client.get(
"/resource", headers={"Authorization": f"Bearer {user_token.access_token}"}
)
assert res.status_code == 200
An unknown token gets a 401 status:
def test_invalid_token(test_client):
res = test_client.get("/resource", headers={"Authorization": "Bearer invalid"})
assert res.status_code == 401
Keyword arguments of random_token() change the token values.
A token without the profile scope gets a 403 status:
def test_missing_scope(iam_server, client, user, test_client):
token = iam_server.random_token(subject=user, client=client, scope=["openid"])
res = test_client.get(
"/resource", headers={"Authorization": f"Bearer {token.access_token}"}
)
assert res.status_code == 403
iam_server.backend.delete(token)