2022-01-11 18:42:26 +00:00
|
|
|
import datetime
|
2022-12-24 01:06:28 +00:00
|
|
|
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
|
2024-03-15 18:58:06 +00:00
|
|
|
from werkzeug.security import gen_salt
|
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2022-12-29 01:06:54 +00:00
|
|
|
@pytest.fixture
|
2024-09-11 07:33:42 +00:00
|
|
|
# For some reason all the params from the overridden fixture must be present here
|
2023-06-03 17:47:05 +00:00
|
|
|
# https://github.com/pytest-dev/pytest/issues/11075
|
|
|
|
def app(app, configuration, backend):
|
2022-12-29 01:06:54 +00:00
|
|
|
os.environ["AUTHLIB_INSECURE_TRANSPORT"] = "true"
|
|
|
|
yield app
|
|
|
|
|
|
|
|
|
2022-12-24 01:06:28 +00:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def keypair():
|
2023-07-01 16:46:11 +00:00
|
|
|
return generate_keypair()
|
2022-12-24 01:06:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-07-01 16:46:11 +00:00
|
|
|
def configuration(configuration, keypair):
|
2022-12-24 01:06:28 +00:00
|
|
|
private_key, public_key = keypair
|
2023-12-18 17:06:03 +00:00
|
|
|
configuration["CANAILLE_OIDC"] = {
|
|
|
|
"JWT": {
|
|
|
|
"PUBLIC_KEY": public_key,
|
|
|
|
"PRIVATE_KEY": private_key,
|
|
|
|
"ISS": "https://auth.mydomain.tld",
|
|
|
|
}
|
2022-12-24 01:06:28 +00:00
|
|
|
}
|
2023-12-18 17:06:03 +00:00
|
|
|
return configuration
|
2022-12-24 01:06:28 +00:00
|
|
|
|
|
|
|
|
2022-01-11 18:42:26 +00:00
|
|
|
@pytest.fixture
|
2023-12-23 16:23:19 +00:00
|
|
|
def client(testclient, trusted_client, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
c = models.Client(
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=gen_salt(24),
|
2022-10-17 15:49:52 +00:00
|
|
|
client_name="Some client",
|
2023-11-24 11:10:17 +00:00
|
|
|
contacts=["contact@mydomain.tld"],
|
2022-10-17 15:49:52 +00:00
|
|
|
client_uri="https://mydomain.tld",
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uris=[
|
2022-01-11 18:42:26 +00:00
|
|
|
"https://mydomain.tld/redirect1",
|
|
|
|
"https://mydomain.tld/redirect2",
|
|
|
|
],
|
2023-12-01 21:09:54 +00:00
|
|
|
logo_uri="https://mydomain.tld/logo.webp",
|
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"],
|
2022-01-11 16:57:58 +00:00
|
|
|
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",
|
2022-01-11 16:57:58 +00:00
|
|
|
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
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(c)
|
2023-12-23 16:23:19 +00:00
|
|
|
c.audience = [c, trusted_client]
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(c)
|
2022-01-11 18:42:26 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
yield c
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(c)
|
2022-01-11 18:42:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-12-23 16:23:19 +00:00
|
|
|
def trusted_client(testclient, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
c = models.Client(
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=gen_salt(24),
|
2022-10-17 15:49:52 +00:00
|
|
|
client_name="Some other client",
|
2023-11-24 11:10:17 +00:00
|
|
|
contacts=["contact@myotherdomain.tld"],
|
2022-10-17 15:49:52 +00:00
|
|
|
client_uri="https://myotherdomain.tld",
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uris=[
|
2022-01-11 18:42:26 +00:00
|
|
|
"https://myotherdomain.tld/redirect1",
|
|
|
|
"https://myotherdomain.tld/redirect2",
|
|
|
|
],
|
2023-12-01 21:09:54 +00:00
|
|
|
logo_uri="https://myotherdomain.tld/logo.webp",
|
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-01-11 16:57:58 +00:00
|
|
|
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",
|
2022-01-11 16:57:58 +00:00
|
|
|
token_endpoint_auth_method="client_secret_basic",
|
2022-05-20 12:07:56 +00:00
|
|
|
post_logout_redirect_uris=["https://myotherdomain.tld/disconnected"],
|
2023-12-23 16:23:19 +00:00
|
|
|
preconsent=True,
|
2022-01-11 18:42:26 +00:00
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(c)
|
2023-03-08 22:53:53 +00:00
|
|
|
c.audience = [c]
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(c)
|
2022-01-11 18:42:26 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
yield c
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(c)
|
2022-01-11 18:42:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-05-20 15:17:46 +00:00
|
|
|
def authorization(testclient, user, client, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
a = models.AuthorizationCode(
|
2022-02-16 17:00:30 +00:00
|
|
|
authorization_code_id=gen_salt(48),
|
2022-01-11 16:57:58 +00:00
|
|
|
code="my-code",
|
2023-03-08 22:53:53 +00:00
|
|
|
client=client,
|
|
|
|
subject=user,
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uri="https://foo.bar/callback",
|
|
|
|
response_type="code",
|
2023-11-23 21:07:42 +00:00
|
|
|
scope=["openid", "profile"],
|
2022-01-11 16:57:58 +00:00
|
|
|
nonce="nonce",
|
2023-03-17 23:38:56 +00:00
|
|
|
issue_date=datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc),
|
2023-05-17 07:29:32 +00:00
|
|
|
lifetime=3600,
|
2022-01-11 16:57:58 +00:00
|
|
|
challenge="challenge",
|
|
|
|
challenge_method="method",
|
2022-01-11 18:42:26 +00:00
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(a)
|
2022-05-20 07:24:24 +00:00
|
|
|
yield a
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(a)
|
2022-01-11 18:42:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-05-20 15:17:46 +00:00
|
|
|
def token(testclient, client, user, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
t = models.Token(
|
2022-02-16 17:00:30 +00:00
|
|
|
token_id=gen_salt(48),
|
2022-01-11 16:57:58 +00:00
|
|
|
access_token=gen_salt(48),
|
2023-03-08 22:53:53 +00:00
|
|
|
audience=[client],
|
|
|
|
client=client,
|
|
|
|
subject=user,
|
2022-01-11 16:57:58 +00:00
|
|
|
refresh_token=gen_salt(48),
|
2023-11-23 21:07:42 +00:00
|
|
|
scope=["openid", "profile"],
|
2023-03-17 23:38:56 +00:00
|
|
|
issue_date=datetime.datetime.now(datetime.timezone.utc),
|
2023-05-17 07:29:32 +00:00
|
|
|
lifetime=3600,
|
2022-01-11 18:42:26 +00:00
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(t)
|
2022-05-20 07:24:24 +00:00
|
|
|
yield t
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(t)
|
2022-01-11 18:42:26 +00:00
|
|
|
|
|
|
|
|
2022-05-20 12:07:56 +00:00
|
|
|
@pytest.fixture
|
2023-05-20 15:17:46 +00:00
|
|
|
def id_token(testclient, client, user, backend):
|
2022-05-20 12:07:56 +00:00
|
|
|
return generate_id_token(
|
|
|
|
{},
|
2023-03-08 22:53:53 +00:00
|
|
|
generate_user_info(user, client.scope),
|
2022-05-20 12:07:56 +00:00
|
|
|
aud=client.client_id,
|
2023-12-26 00:13:11 +00:00
|
|
|
**get_jwt_config(None),
|
2022-05-20 12:07:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-11 18:42:26 +00:00
|
|
|
@pytest.fixture
|
2023-05-20 15:17:46 +00:00
|
|
|
def consent(testclient, client, user, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
t = models.Consent(
|
2023-05-17 06:54:13 +00:00
|
|
|
consent_id=str(uuid.uuid4()),
|
2023-03-08 22:53:53 +00:00
|
|
|
client=client,
|
|
|
|
subject=user,
|
2022-01-11 16:57:58 +00:00
|
|
|
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
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(t)
|
2022-05-20 07:24:24 +00:00
|
|
|
yield t
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(t)
|