canaille-globuzma/tests/oidc/conftest.py

177 lines
4.8 KiB
Python
Raw Normal View History

2022-01-11 18:42:26 +00:00
import datetime
import os
2023-01-23 17:55:27 +00:00
import uuid
2022-01-11 18:42:26 +00:00
import pytest
2022-05-20 12:07:56 +00:00
from authlib.oidc.core.grants.util import generate_id_token
from canaille.app import models
2023-07-01 16:46:11 +00:00
from canaille.oidc.installation import generate_keypair
2022-10-06 11:32:41 +00:00
from canaille.oidc.oauth import generate_user_info
from canaille.oidc.oauth import get_jwt_config
2022-01-11 18:42:26 +00:00
from werkzeug.security import gen_salt
@pytest.fixture
2023-06-03 17:47:05 +00:00
# For some reason all the params from the overriden fixture must be present here
# https://github.com/pytest-dev/pytest/issues/11075
def app(app, configuration, backend):
os.environ["AUTHLIB_INSECURE_TRANSPORT"] = "true"
yield app
@pytest.fixture(scope="session")
def keypair():
2023-07-01 16:46:11 +00:00
return generate_keypair()
@pytest.fixture
2023-07-01 16:46:11 +00:00
def configuration(configuration, keypair):
private_key, public_key = keypair
conf = {
**configuration,
"OIDC": {
"JWT": {
2023-07-01 16:46:11 +00:00
"PUBLIC_KEY": public_key,
"PRIVATE_KEY": private_key,
"ISS": "https://auth.mydomain.tld",
}
},
}
return conf
2022-01-11 18:42:26 +00:00
@pytest.fixture
def client(testclient, other_client, backend):
c = models.Client(
client_id=gen_salt(24),
2022-10-17 15:49:52 +00:00
client_name="Some client",
contacts="contact@mydomain.tld",
client_uri="https://mydomain.tld",
redirect_uris=[
2022-01-11 18:42:26 +00:00
"https://mydomain.tld/redirect1",
"https://mydomain.tld/redirect2",
],
logo_uri="https://mydomain.tld/logo.png",
2023-03-17 23:38:56 +00:00
client_id_issued_at=datetime.datetime.now(datetime.timezone.utc),
2022-10-17 15:49:52 +00:00
client_secret=gen_salt(48),
grant_types=[
2022-01-11 18:42:26 +00:00
"password",
"authorization_code",
"implicit",
"hybrid",
"refresh_token",
],
2022-10-17 15:49:52 +00:00
response_types=["code", "token", "id_token"],
2022-07-07 14:05:34 +00:00
scope=["openid", "email", "profile", "groups", "address", "phone"],
tos_uri="https://mydomain.tld/tos",
policy_uri="https://mydomain.tld/policy",
2022-10-17 15:49:52 +00:00
jwks_uri="https://mydomain.tld/jwk",
token_endpoint_auth_method="client_secret_basic",
2022-05-20 12:07:56 +00:00
post_logout_redirect_uris=["https://mydomain.tld/disconnected"],
2022-01-11 18:42:26 +00:00
)
c.audience = [c, other_client]
c.save()
2022-01-11 18:42:26 +00:00
2022-05-20 07:24:24 +00:00
yield c
c.delete()
2022-01-11 18:42:26 +00:00
@pytest.fixture
def other_client(testclient, backend):
c = models.Client(
client_id=gen_salt(24),
2022-10-17 15:49:52 +00:00
client_name="Some other client",
contacts="contact@myotherdomain.tld",
client_uri="https://myotherdomain.tld",
redirect_uris=[
2022-01-11 18:42:26 +00:00
"https://myotherdomain.tld/redirect1",
"https://myotherdomain.tld/redirect2",
],
logo_uri="https://myotherdomain.tld/logo.png",
2023-03-17 23:38:56 +00:00
client_id_issued_at=datetime.datetime.now(datetime.timezone.utc),
2022-10-17 15:49:52 +00:00
client_secret=gen_salt(48),
grant_types=[
2022-01-11 18:42:26 +00:00
"password",
"authorization_code",
"implicit",
"hybrid",
"refresh_token",
],
2022-10-17 15:49:52 +00:00
response_types=["code", "token", "id_token"],
scope=["openid", "profile", "groups"],
tos_uri="https://myotherdomain.tld/tos",
policy_uri="https://myotherdomain.tld/policy",
2022-10-17 15:49:52 +00:00
jwks_uri="https://myotherdomain.tld/jwk",
token_endpoint_auth_method="client_secret_basic",
2022-05-20 12:07:56 +00:00
post_logout_redirect_uris=["https://myotherdomain.tld/disconnected"],
2022-01-11 18:42:26 +00:00
)
c.audience = [c]
c.save()
2022-01-11 18:42:26 +00:00
2022-05-20 07:24:24 +00:00
yield c
c.delete()
2022-01-11 18:42:26 +00:00
@pytest.fixture
def authorization(testclient, user, client, backend):
a = models.AuthorizationCode(
authorization_code_id=gen_salt(48),
code="my-code",
client=client,
subject=user,
redirect_uri="https://foo.bar/callback",
response_type="code",
scope=["openid", "profile"],
nonce="nonce",
2023-03-17 23:38:56 +00:00
issue_date=datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc),
lifetime=3600,
challenge="challenge",
challenge_method="method",
2022-01-11 18:42:26 +00:00
)
a.save()
2022-05-20 07:24:24 +00:00
yield a
a.delete()
2022-01-11 18:42:26 +00:00
@pytest.fixture
def token(testclient, client, user, backend):
t = models.Token(
token_id=gen_salt(48),
access_token=gen_salt(48),
audience=[client],
client=client,
subject=user,
token_type=None,
refresh_token=gen_salt(48),
scope=["openid", "profile"],
2023-03-17 23:38:56 +00:00
issue_date=datetime.datetime.now(datetime.timezone.utc),
lifetime=3600,
2022-01-11 18:42:26 +00:00
)
t.save()
2022-05-20 07:24:24 +00:00
yield t
t.delete()
2022-01-11 18:42:26 +00:00
2022-05-20 12:07:56 +00:00
@pytest.fixture
def id_token(testclient, client, user, backend):
2022-05-20 12:07:56 +00:00
return generate_id_token(
{},
generate_user_info(user, client.scope),
2022-05-20 12:07:56 +00:00
aud=client.client_id,
**get_jwt_config(None)
)
2022-01-11 18:42:26 +00:00
@pytest.fixture
def consent(testclient, client, user, backend):
t = models.Consent(
consent_id=str(uuid.uuid4()),
client=client,
subject=user,
scope=["openid", "profile"],
2023-03-17 23:38:56 +00:00
issue_date=datetime.datetime.now(datetime.timezone.utc),
2022-01-11 18:42:26 +00:00
)
t.save()
2022-05-20 07:24:24 +00:00
yield t
t.delete()