2022-01-11 18:42:26 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
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
|
2022-01-11 18:42:26 +00:00
|
|
|
from werkzeug.security import gen_salt
|
|
|
|
|
|
|
|
|
|
|
|
@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(
|
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",
|
|
|
|
contacts="contact@mydomain.tld",
|
|
|
|
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",
|
|
|
|
],
|
2022-01-11 16:57:58 +00:00
|
|
|
logo_uri="https://mydomain.tld/logo.png",
|
2022-10-17 15:49:52 +00:00
|
|
|
client_id_issued_at=datetime.datetime.now(),
|
|
|
|
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
|
|
|
)
|
2022-01-11 16:57:58 +00:00
|
|
|
c.audience = [c.dn, other_client.dn]
|
2022-05-08 14:31:17 +00:00
|
|
|
c.save()
|
2022-01-11 18:42:26 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
yield c
|
|
|
|
try:
|
|
|
|
c.delete()
|
|
|
|
except:
|
|
|
|
pass
|
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(
|
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",
|
|
|
|
contacts="contact@myotherdomain.tld",
|
|
|
|
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",
|
|
|
|
],
|
2022-01-11 16:57:58 +00:00
|
|
|
logo_uri="https://myotherdomain.tld/logo.png",
|
2022-10-17 15:49:52 +00:00
|
|
|
client_id_issued_at=datetime.datetime.now(),
|
|
|
|
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"],
|
2022-01-11 18:42:26 +00:00
|
|
|
)
|
2022-01-11 16:57:58 +00:00
|
|
|
c.audience = [c.dn]
|
2022-05-08 14:31:17 +00:00
|
|
|
c.save()
|
2022-01-11 18:42:26 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
yield c
|
|
|
|
try:
|
|
|
|
c.delete()
|
|
|
|
except:
|
|
|
|
pass
|
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(
|
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",
|
|
|
|
client=client.dn,
|
|
|
|
subject=user.dn,
|
|
|
|
redirect_uri="https://foo.bar/callback",
|
|
|
|
response_type="code",
|
|
|
|
scope="openid profile",
|
|
|
|
nonce="nonce",
|
|
|
|
issue_date=datetime.datetime(2020, 1, 1),
|
|
|
|
lifetime="3600",
|
|
|
|
challenge="challenge",
|
|
|
|
challenge_method="method",
|
|
|
|
revokation="",
|
2022-01-11 18:42:26 +00:00
|
|
|
)
|
2022-05-08 14:31:17 +00:00
|
|
|
a.save()
|
2022-05-20 07:24:24 +00:00
|
|
|
yield a
|
|
|
|
try:
|
|
|
|
a.delete()
|
|
|
|
except:
|
|
|
|
pass
|
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(
|
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),
|
|
|
|
audience=[client.dn],
|
|
|
|
client=client.dn,
|
|
|
|
subject=user.dn,
|
|
|
|
token_type=None,
|
|
|
|
refresh_token=gen_salt(48),
|
|
|
|
scope="openid profile",
|
|
|
|
issue_date=datetime.datetime.now(),
|
|
|
|
lifetime=str(3600),
|
2022-01-11 18:42:26 +00:00
|
|
|
)
|
2022-05-08 14:31:17 +00:00
|
|
|
t.save()
|
2022-05-20 07:24:24 +00:00
|
|
|
yield t
|
|
|
|
try:
|
|
|
|
t.delete()
|
|
|
|
except:
|
|
|
|
pass
|
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.dn, client.scope),
|
|
|
|
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(
|
2022-01-11 16:57:58 +00:00
|
|
|
client=client.dn,
|
|
|
|
subject=user.dn,
|
|
|
|
scope=["openid", "profile"],
|
|
|
|
issue_date=datetime.datetime.now(),
|
2022-01-11 18:42:26 +00:00
|
|
|
)
|
2022-05-08 14:31:17 +00:00
|
|
|
t.save()
|
2022-05-20 07:24:24 +00:00
|
|
|
yield t
|
|
|
|
try:
|
|
|
|
t.delete()
|
|
|
|
except:
|
|
|
|
pass
|