canaille-globuzma/tests/oidc/conftest.py

218 lines
6.6 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
2022-01-11 18:49:06 +00:00
from canaille.oidc.models import AuthorizationCode
from canaille.oidc.models import Client
from canaille.oidc.models import Consent
from canaille.oidc.models import Token
2022-10-06 11:32:41 +00:00
from canaille.oidc.oauth import generate_user_info
from canaille.oidc.oauth import get_jwt_config
from cryptography.hazmat.backends import default_backend as crypto_default_backend
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa
2022-01-11 18:42:26 +00:00
from werkzeug.security import gen_salt
@pytest.fixture
def app(app):
os.environ["AUTHLIB_INSECURE_TRANSPORT"] = "true"
yield app
@pytest.fixture(scope="session")
def keypair():
key = rsa.generate_private_key(
backend=crypto_default_backend(), public_exponent=65537, key_size=2048
)
private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH, crypto_serialization.PublicFormat.OpenSSH
)
return private_key, public_key
@pytest.fixture
def keypair_path(keypair, tmp_path):
private_key, public_key = keypair
private_key_path = os.path.join(tmp_path, "private.pem")
with open(private_key_path, "wb") as fd:
fd.write(private_key)
public_key_path = os.path.join(tmp_path, "public.pem")
with open(public_key_path, "wb") as fd:
fd.write(public_key)
return private_key_path, public_key_path
@pytest.fixture
def configuration(configuration, keypair_path):
private_key_path, public_key_path = keypair_path
conf = {
**configuration,
"JWT": {
"PUBLIC_KEY": public_key_path,
"PRIVATE_KEY": private_key_path,
"ISS": "https://auth.mydomain.tld",
"MAPPING": {
"SUB": "{{ user.user_name[0] }}",
2023-04-07 20:38:01 +00:00
"NAME": "{{ user.formatted_name[0] }}",
"PHONE_NUMBER": "{{ user.phone_number[0] }}",
"EMAIL": "{{ user.email[0] }}",
"GIVEN_NAME": "{{ user.given_name[0] }}",
"FAMILY_NAME": "{{ user.family_name[0] }}",
"PREFERRED_USERNAME": "{{ user.display_name }}",
"LOCALE": "{{ user.preferred_language }}",
"PICTURE": "{% if user.photo %}{{ url_for('account.photo', user_name=user.user_name[0], field='photo', _external=True) }}{% endif %}",
"ADDRESS": "{{ user.formatted_address[0] }}",
"WEBSITE": "{{ user.profile_url[0] }}",
},
},
}
return conf
2022-01-11 18:42:26 +00:00
@pytest.fixture
2022-05-20 07:24:24 +00:00
def client(testclient, other_client, slapd_connection):
2022-01-11 18:42:26 +00:00
c = 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
2022-05-20 07:24:24 +00:00
def other_client(testclient, slapd_connection):
2022-01-11 18:42:26 +00:00
c = 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
2022-05-20 07:24:24 +00:00
def authorization(testclient, user, client, slapd_connection):
2022-01-11 18:42:26 +00:00
a = 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",
revokation="",
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
2022-05-20 07:24:24 +00:00
def token(testclient, client, user, slapd_connection):
2022-01-11 18:42:26 +00:00
t = 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=str(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, slapd_connection):
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
2022-05-20 07:24:24 +00:00
def consent(testclient, client, user, slapd_connection):
2022-01-11 18:42:26 +00:00
t = Consent(
2023-01-23 17:55:27 +00:00
cn=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()