2022-10-24 15:18:46 +00:00
|
|
|
import warnings
|
|
|
|
from datetime import datetime
|
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
from canaille.app import models
|
2022-10-24 15:18:46 +00:00
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def test_get(testclient, backend, client, user):
|
2023-12-18 17:06:03 +00:00
|
|
|
assert not testclient.app.config["CANAILLE_OIDC"].get(
|
2023-04-10 17:28:26 +00:00
|
|
|
"DYNAMIC_CLIENT_REGISTRATION_OPEN"
|
|
|
|
)
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE_OIDC"]["DYNAMIC_CLIENT_REGISTRATION_TOKENS"] = [
|
2023-04-10 17:28:26 +00:00
|
|
|
"static-token"
|
|
|
|
]
|
2022-10-24 15:18:46 +00:00
|
|
|
|
|
|
|
headers = {"Authorization": "Bearer static-token"}
|
|
|
|
res = testclient.get(
|
|
|
|
f"/oauth/register/{client.client_id}", headers=headers, status=200
|
|
|
|
)
|
|
|
|
assert res.json == {
|
|
|
|
"client_id": client.client_id,
|
|
|
|
"client_secret": client.client_secret,
|
|
|
|
"client_id_issued_at": int(datetime.timestamp(client.client_id_issued_at)),
|
2023-11-23 08:15:40 +00:00
|
|
|
"client_secret_expires_at": 0,
|
2022-10-24 15:18:46 +00:00
|
|
|
"redirect_uris": [
|
2024-11-20 22:30:44 +00:00
|
|
|
"https://mydomain.test/redirect1",
|
|
|
|
"https://mydomain.test/redirect2",
|
2022-10-24 15:18:46 +00:00
|
|
|
],
|
|
|
|
"registration_access_token": "static-token",
|
2023-12-14 19:07:49 +00:00
|
|
|
"registration_client_uri": f"http://canaille.test/oauth/register/{client.client_id}",
|
2022-10-24 15:18:46 +00:00
|
|
|
"token_endpoint_auth_method": "client_secret_basic",
|
|
|
|
"grant_types": [
|
|
|
|
"password",
|
|
|
|
"authorization_code",
|
|
|
|
"implicit",
|
|
|
|
"hybrid",
|
|
|
|
"refresh_token",
|
2024-12-06 13:43:31 +00:00
|
|
|
"client_credentials",
|
2022-10-24 15:18:46 +00:00
|
|
|
],
|
|
|
|
"response_types": ["code", "token", "id_token"],
|
|
|
|
"client_name": "Some client",
|
2024-11-20 22:30:44 +00:00
|
|
|
"client_uri": "https://mydomain.test",
|
|
|
|
"logo_uri": "https://mydomain.test/logo.webp",
|
2023-01-28 13:04:04 +00:00
|
|
|
"scope": "openid email profile groups address phone",
|
2024-11-20 22:30:44 +00:00
|
|
|
"contacts": ["contact@mydomain.test"],
|
|
|
|
"tos_uri": "https://mydomain.test/tos",
|
|
|
|
"policy_uri": "https://mydomain.test/policy",
|
2022-10-24 15:18:46 +00:00
|
|
|
"jwk": None,
|
2024-11-20 22:30:44 +00:00
|
|
|
"jwks_uri": "https://mydomain.test/jwk",
|
2022-10-24 15:18:46 +00:00
|
|
|
"software_id": None,
|
|
|
|
"software_version": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def test_update(testclient, backend, client, user):
|
2023-12-18 17:06:03 +00:00
|
|
|
assert not testclient.app.config["CANAILLE_OIDC"].get(
|
2023-04-10 17:28:26 +00:00
|
|
|
"DYNAMIC_CLIENT_REGISTRATION_OPEN"
|
|
|
|
)
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE_OIDC"]["DYNAMIC_CLIENT_REGISTRATION_TOKENS"] = [
|
2023-04-10 17:28:26 +00:00
|
|
|
"static-token"
|
|
|
|
]
|
2022-10-24 15:18:46 +00:00
|
|
|
|
2024-11-20 22:30:44 +00:00
|
|
|
assert client.redirect_uris != ["https://newname.example.test/callback"]
|
2022-10-24 15:18:46 +00:00
|
|
|
assert client.token_endpoint_auth_method != "none"
|
|
|
|
assert client.grant_types != ["refresh_token"]
|
|
|
|
assert client.response_types != ["code", "token"]
|
|
|
|
assert client.client_name != "new name"
|
2024-11-20 22:30:44 +00:00
|
|
|
assert client.client_uri != "https://newname.example.test"
|
|
|
|
assert client.logo_uri != "https://newname.example.test/logo.webp"
|
2022-10-24 15:18:46 +00:00
|
|
|
assert client.scope != ["openid", "profile", "email"]
|
2024-11-20 22:30:44 +00:00
|
|
|
assert client.contacts != ["newcontact@example.test"]
|
|
|
|
assert client.tos_uri != "https://newname.example.test/tos"
|
|
|
|
assert client.policy_uri != "https://newname.example.test/policy"
|
|
|
|
assert client.jwks_uri != "https://newname.example.test/my_public_keys.jwks"
|
2022-10-24 15:18:46 +00:00
|
|
|
assert client.software_id != "new_software_id"
|
|
|
|
assert client.software_version != "3.14"
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"client_id": client.client_id,
|
2024-11-20 22:30:44 +00:00
|
|
|
"redirect_uris": ["https://newname.example.test/callback"],
|
2022-10-24 15:18:46 +00:00
|
|
|
"token_endpoint_auth_method": "none",
|
|
|
|
"grant_types": ["refresh_token"],
|
|
|
|
"response_types": ["code", "token"],
|
|
|
|
"client_name": "new name",
|
2024-11-20 22:30:44 +00:00
|
|
|
"client_uri": "https://newname.example.test",
|
|
|
|
"logo_uri": "https://newname.example.test/logo.webp",
|
2023-01-28 13:04:04 +00:00
|
|
|
"scope": "openid profile email",
|
2024-11-20 22:30:44 +00:00
|
|
|
"contacts": ["newcontact@example.test"],
|
|
|
|
"tos_uri": "https://newname.example.test/tos",
|
|
|
|
"policy_uri": "https://newname.example.test/policy",
|
|
|
|
"jwks_uri": "https://newname.example.test/my_public_keys.jwks",
|
2022-10-24 15:18:46 +00:00
|
|
|
"software_id": "new_software_id",
|
|
|
|
"software_version": "3.14",
|
|
|
|
}
|
|
|
|
|
|
|
|
headers = {"Authorization": "Bearer static-token"}
|
|
|
|
res = testclient.put_json(
|
|
|
|
f"/oauth/register/{client.client_id}", payload, headers=headers, status=200
|
|
|
|
)
|
2024-04-14 15:30:59 +00:00
|
|
|
client = backend.get(models.Client, client_id=res.json["client_id"])
|
2022-10-24 15:18:46 +00:00
|
|
|
|
|
|
|
assert res.json == {
|
|
|
|
"client_id": client.client_id,
|
|
|
|
"client_secret": client.client_secret,
|
|
|
|
"client_id_issued_at": int(datetime.timestamp(client.client_id_issued_at)),
|
2023-11-23 08:15:40 +00:00
|
|
|
"client_secret_expires_at": 0,
|
2024-11-20 22:30:44 +00:00
|
|
|
"redirect_uris": ["https://newname.example.test/callback"],
|
2022-10-24 15:18:46 +00:00
|
|
|
"registration_access_token": "static-token",
|
2023-12-14 19:07:49 +00:00
|
|
|
"registration_client_uri": f"http://canaille.test/oauth/register/{client.client_id}",
|
2022-10-24 15:18:46 +00:00
|
|
|
"token_endpoint_auth_method": "none",
|
|
|
|
"grant_types": ["refresh_token"],
|
|
|
|
"response_types": ["code", "token"],
|
|
|
|
"client_name": "new name",
|
2024-11-20 22:30:44 +00:00
|
|
|
"client_uri": "https://newname.example.test",
|
|
|
|
"logo_uri": "https://newname.example.test/logo.webp",
|
2023-01-28 13:04:04 +00:00
|
|
|
"scope": "openid profile email",
|
2024-11-20 22:30:44 +00:00
|
|
|
"contacts": ["newcontact@example.test"],
|
|
|
|
"tos_uri": "https://newname.example.test/tos",
|
|
|
|
"policy_uri": "https://newname.example.test/policy",
|
2022-10-24 15:18:46 +00:00
|
|
|
"jwk": None,
|
2024-11-20 22:30:44 +00:00
|
|
|
"jwks_uri": "https://newname.example.test/my_public_keys.jwks",
|
2022-10-24 15:18:46 +00:00
|
|
|
"software_id": "new_software_id",
|
|
|
|
"software_version": "3.14",
|
|
|
|
}
|
|
|
|
|
2024-11-20 22:30:44 +00:00
|
|
|
assert client.redirect_uris == ["https://newname.example.test/callback"]
|
2022-10-24 15:18:46 +00:00
|
|
|
assert client.token_endpoint_auth_method == "none"
|
|
|
|
assert client.grant_types == ["refresh_token"]
|
|
|
|
assert client.response_types == ["code", "token"]
|
|
|
|
assert client.client_name == "new name"
|
2024-11-20 22:30:44 +00:00
|
|
|
assert client.client_uri == "https://newname.example.test"
|
|
|
|
assert client.logo_uri == "https://newname.example.test/logo.webp"
|
2022-10-24 15:18:46 +00:00
|
|
|
assert client.scope == ["openid", "profile", "email"]
|
2024-11-20 22:30:44 +00:00
|
|
|
assert client.contacts == ["newcontact@example.test"]
|
|
|
|
assert client.tos_uri == "https://newname.example.test/tos"
|
|
|
|
assert client.policy_uri == "https://newname.example.test/policy"
|
|
|
|
assert client.jwks_uri == "https://newname.example.test/my_public_keys.jwks"
|
2022-10-24 15:18:46 +00:00
|
|
|
assert client.software_id == "new_software_id"
|
|
|
|
assert client.software_version == "3.14"
|
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def test_delete(testclient, backend, user):
|
2023-12-18 17:06:03 +00:00
|
|
|
assert not testclient.app.config["CANAILLE_OIDC"].get(
|
2023-04-10 17:28:26 +00:00
|
|
|
"DYNAMIC_CLIENT_REGISTRATION_OPEN"
|
|
|
|
)
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE_OIDC"]["DYNAMIC_CLIENT_REGISTRATION_TOKENS"] = [
|
2023-04-10 17:28:26 +00:00
|
|
|
"static-token"
|
|
|
|
]
|
2022-10-24 15:18:46 +00:00
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
client = models.Client(client_id="foobar", client_name="Some client")
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(client)
|
2022-10-24 15:18:46 +00:00
|
|
|
|
|
|
|
headers = {"Authorization": "Bearer static-token"}
|
|
|
|
with warnings.catch_warnings(record=True):
|
2023-05-25 11:37:58 +00:00
|
|
|
testclient.delete(
|
2022-10-24 15:18:46 +00:00
|
|
|
f"/oauth/register/{client.client_id}", headers=headers, status=204
|
|
|
|
)
|
2024-04-14 15:30:59 +00:00
|
|
|
assert not backend.get(models.Client, client_id=client.client_id)
|
2022-10-24 15:18:46 +00:00
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def test_invalid_client(testclient, backend, user):
|
2023-12-18 17:06:03 +00:00
|
|
|
assert not testclient.app.config["CANAILLE_OIDC"].get(
|
2023-04-10 17:28:26 +00:00
|
|
|
"DYNAMIC_CLIENT_REGISTRATION_OPEN"
|
|
|
|
)
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE_OIDC"]["DYNAMIC_CLIENT_REGISTRATION_TOKENS"] = [
|
2023-04-10 17:28:26 +00:00
|
|
|
"static-token"
|
|
|
|
]
|
2022-10-24 15:18:46 +00:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
"client_id": "invalid-client-id",
|
2024-11-20 22:30:44 +00:00
|
|
|
"redirect_uris": ["https://newname.example.test/callback"],
|
2022-10-24 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
headers = {"Authorization": "Bearer static-token"}
|
|
|
|
res = testclient.put_json(
|
|
|
|
"/oauth/register/invalid-client-id", payload, headers=headers, status=401
|
|
|
|
)
|
|
|
|
assert res.json == {"error": "invalid_client"}
|