2024-04-17 11:05:14 +00:00
|
|
|
import datetime
|
2024-10-14 12:04:39 +00:00
|
|
|
import logging
|
2021-12-20 22:57:27 +00:00
|
|
|
from urllib.parse import parse_qs
|
|
|
|
from urllib.parse import urlsplit
|
|
|
|
|
2024-05-02 08:26:32 +00:00
|
|
|
import time_machine
|
2021-10-13 09:52:02 +00:00
|
|
|
from authlib.jose import jwt
|
2020-08-25 13:28:13 +00:00
|
|
|
from authlib.oauth2.rfc7636 import create_s256_code_challenge
|
2023-08-13 20:08:28 +00:00
|
|
|
from flask import g
|
2020-08-25 13:28:13 +00:00
|
|
|
from werkzeug.security import gen_salt
|
2020-08-19 08:28:28 +00:00
|
|
|
|
2024-03-15 18:58:06 +00:00
|
|
|
from canaille.app import models
|
|
|
|
|
2021-12-20 22:57:27 +00:00
|
|
|
from . import client_credentials
|
|
|
|
|
2020-08-19 08:28:28 +00:00
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_nominal_case(
|
2024-10-14 12:04:39 +00:00
|
|
|
testclient, logged_user, client, keypair, trusted_client, backend, caplog
|
2024-04-10 13:44:11 +00:00
|
|
|
):
|
|
|
|
assert not backend.query(models.Consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2020-08-19 08:28:28 +00:00
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-07-07 14:05:34 +00:00
|
|
|
scope="openid profile email groups address phone",
|
2020-08-19 08:28:28 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-08-19 08:28:28 +00:00
|
|
|
)
|
2020-10-30 22:41:02 +00:00
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
2024-10-14 12:04:39 +00:00
|
|
|
assert (
|
|
|
|
"canaille",
|
2024-10-21 09:17:55 +00:00
|
|
|
logging.SECURITY,
|
|
|
|
"New consent for user in client Some client from unknown IP",
|
2024-10-14 12:04:39 +00:00
|
|
|
) in caplog.record_tuples
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2020-08-24 08:52:21 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2020-08-24 08:52:21 +00:00
|
|
|
assert authcode is not None
|
2023-11-22 15:30:38 +00:00
|
|
|
assert set(authcode.scope) == {
|
2022-07-07 14:05:34 +00:00
|
|
|
"openid",
|
|
|
|
"profile",
|
|
|
|
"email",
|
|
|
|
"groups",
|
|
|
|
"address",
|
|
|
|
"phone",
|
|
|
|
}
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-07-07 14:05:34 +00:00
|
|
|
assert set(consents[0].scope) == {
|
|
|
|
"openid",
|
|
|
|
"profile",
|
|
|
|
"email",
|
|
|
|
"groups",
|
|
|
|
"address",
|
|
|
|
"phone",
|
|
|
|
}
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2020-08-24 08:52:21 +00:00
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
2022-07-07 14:05:34 +00:00
|
|
|
scope="openid profile email groups address phone",
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uri=client.redirect_uris[0],
|
2020-08-24 08:52:21 +00:00
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-08-24 08:52:21 +00:00
|
|
|
)
|
|
|
|
|
2021-10-13 09:52:02 +00:00
|
|
|
access_token = res.json["access_token"]
|
2024-04-14 15:30:59 +00:00
|
|
|
token = backend.get(models.Token, access_token=access_token)
|
2023-03-08 22:53:53 +00:00
|
|
|
assert token.client == client
|
|
|
|
assert token.subject == logged_user
|
2023-11-22 15:36:42 +00:00
|
|
|
assert set(token.scope) == {
|
2022-07-07 14:05:34 +00:00
|
|
|
"openid",
|
|
|
|
"profile",
|
|
|
|
"email",
|
|
|
|
"groups",
|
|
|
|
"address",
|
|
|
|
"phone",
|
|
|
|
}
|
2023-01-13 19:26:35 +00:00
|
|
|
claims = jwt.decode(access_token, keypair[1])
|
2023-11-15 17:20:13 +00:00
|
|
|
assert claims["sub"] == logged_user.user_name
|
|
|
|
assert claims["name"] == logged_user.formatted_name
|
2023-12-23 16:23:19 +00:00
|
|
|
assert claims["aud"] == [client.client_id, trusted_client.client_id]
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2021-10-13 09:52:02 +00:00
|
|
|
id_token = res.json["id_token"]
|
|
|
|
claims = jwt.decode(id_token, keypair[1])
|
2023-11-15 17:20:13 +00:00
|
|
|
assert claims["sub"] == logged_user.user_name
|
|
|
|
assert claims["name"] == logged_user.formatted_name
|
2023-12-23 16:23:19 +00:00
|
|
|
assert claims["aud"] == [client.client_id, trusted_client.client_id]
|
2024-10-14 12:04:39 +00:00
|
|
|
assert (
|
|
|
|
"canaille",
|
2024-10-21 09:17:55 +00:00
|
|
|
logging.SECURITY,
|
|
|
|
"Issued authorization_code token for user in client Some client from unknown IP",
|
2024-10-14 12:04:39 +00:00
|
|
|
) in caplog.record_tuples
|
2020-09-25 09:26:41 +00:00
|
|
|
res = testclient.get(
|
2020-10-30 18:19:34 +00:00
|
|
|
"/oauth/userinfo",
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
status=200,
|
2020-09-25 09:26:41 +00:00
|
|
|
)
|
2022-12-24 00:44:16 +00:00
|
|
|
assert res.json["name"] == "John (johnny) Doe"
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
for consent in consents:
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2022-12-11 12:16:24 +00:00
|
|
|
def test_invalid_client(testclient, logged_user, keypair):
|
2023-05-25 11:37:58 +00:00
|
|
|
testclient.get(
|
2022-12-11 12:16:24 +00:00
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id="invalid",
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_redirect_uri(
|
|
|
|
testclient, logged_user, client, keypair, trusted_client, backend
|
|
|
|
):
|
|
|
|
assert not backend.query(models.Consent)
|
2022-12-10 09:58:22 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id=client.client_id,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
nonce="somenonce",
|
|
|
|
redirect_uri=client.redirect_uris[1],
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
|
|
|
|
assert res.location.startswith(client.redirect_uris[1])
|
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2022-12-10 09:58:22 +00:00
|
|
|
assert authcode is not None
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-12-10 09:58:22 +00:00
|
|
|
|
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
redirect_uri=client.redirect_uris[1],
|
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
access_token = res.json["access_token"]
|
2024-04-14 15:30:59 +00:00
|
|
|
token = backend.get(models.Token, access_token=access_token)
|
2023-03-08 22:53:53 +00:00
|
|
|
assert token.client == client
|
|
|
|
assert token.subject == logged_user
|
2022-12-10 09:58:22 +00:00
|
|
|
|
|
|
|
for consent in consents:
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-12-10 09:58:22 +00:00
|
|
|
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_preconsented_client(
|
|
|
|
testclient, logged_user, client, keypair, trusted_client, backend
|
|
|
|
):
|
|
|
|
assert not backend.query(models.Consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
client.preconsent = True
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(client)
|
2021-10-20 10:05:08 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2021-10-20 10:05:08 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=302,
|
|
|
|
)
|
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2021-10-20 10:05:08 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2021-10-20 10:05:08 +00:00
|
|
|
assert authcode is not None
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-05-20 07:24:24 +00:00
|
|
|
assert not consents
|
|
|
|
|
2021-10-20 10:05:08 +00:00
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uri=client.redirect_uris[0],
|
2021-10-20 10:05:08 +00:00
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
access_token = res.json["access_token"]
|
2024-04-14 15:30:59 +00:00
|
|
|
token = backend.get(models.Token, access_token=access_token)
|
2023-03-08 22:53:53 +00:00
|
|
|
assert token.client == client
|
|
|
|
assert token.subject == logged_user
|
2021-10-20 10:05:08 +00:00
|
|
|
|
|
|
|
id_token = res.json["id_token"]
|
|
|
|
claims = jwt.decode(id_token, keypair[1])
|
2023-11-15 17:20:13 +00:00
|
|
|
assert logged_user.user_name == claims["sub"]
|
|
|
|
assert logged_user.formatted_name == claims["name"]
|
2023-12-23 16:23:19 +00:00
|
|
|
assert [client.client_id, trusted_client.client_id] == claims["aud"]
|
2021-10-20 10:05:08 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/userinfo",
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
status=200,
|
|
|
|
)
|
2022-12-24 00:44:16 +00:00
|
|
|
assert res.json["name"] == "John (johnny) Doe"
|
2021-10-20 10:05:08 +00:00
|
|
|
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_logout_login(testclient, logged_user, client, backend):
|
|
|
|
assert not backend.query(models.Consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2020-08-24 08:52:21 +00:00
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2020-08-24 08:52:21 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-08-24 08:52:21 +00:00
|
|
|
)
|
|
|
|
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.form.submit(name="answer", value="logout")
|
|
|
|
res = res.follow()
|
2023-08-13 20:08:28 +00:00
|
|
|
g.user = None
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.follow()
|
|
|
|
res = res.follow()
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2023-11-15 17:20:13 +00:00
|
|
|
res.form["login"] = logged_user.user_name
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.form.submit()
|
|
|
|
res = res.follow()
|
|
|
|
|
2020-08-24 08:54:50 +00:00
|
|
|
res.form["password"] = "wrong password"
|
2020-10-30 22:41:02 +00:00
|
|
|
res = res.form.submit(status=200)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert ("error", "Login failed, please check your information") in res.flashes
|
2020-08-24 08:54:50 +00:00
|
|
|
|
2020-08-19 11:49:38 +00:00
|
|
|
res.form["password"] = "correct horse battery staple"
|
2020-10-30 22:41:02 +00:00
|
|
|
res = res.form.submit(status=302)
|
|
|
|
res = res.follow(status=200)
|
2020-08-19 08:28:28 +00:00
|
|
|
|
2020-10-30 22:41:02 +00:00
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
2020-10-28 16:57:27 +00:00
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2020-08-19 08:28:28 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2020-08-19 08:28:28 +00:00
|
|
|
assert authcode is not None
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-05-20 07:24:24 +00:00
|
|
|
assert "profile" in consents[0].scope
|
|
|
|
|
2020-08-19 08:28:28 +00:00
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uri=client.redirect_uris[0],
|
2020-08-19 08:28:28 +00:00
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-08-19 08:28:28 +00:00
|
|
|
)
|
|
|
|
|
2021-10-13 09:52:02 +00:00
|
|
|
access_token = res.json["access_token"]
|
2024-04-14 15:30:59 +00:00
|
|
|
token = backend.get(models.Token, access_token=access_token)
|
2023-03-08 22:53:53 +00:00
|
|
|
assert token.client == client
|
|
|
|
assert token.subject == logged_user
|
2020-08-19 08:28:28 +00:00
|
|
|
|
2020-09-25 09:26:41 +00:00
|
|
|
res = testclient.get(
|
2020-10-30 18:19:34 +00:00
|
|
|
"/oauth/userinfo",
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
status=200,
|
2020-09-25 09:26:41 +00:00
|
|
|
)
|
2022-12-24 00:44:16 +00:00
|
|
|
assert res.json["name"] == "John (johnny) Doe"
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
for consent in consents:
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2020-08-24 08:52:21 +00:00
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_deny(testclient, logged_user, client, backend):
|
|
|
|
assert not backend.query(models.Consent)
|
2022-12-11 13:43:21 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id=client.client_id,
|
|
|
|
scope="openid profile",
|
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
res = res.form.submit(name="answer", value="deny", status=302)
|
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
error = params["error"][0]
|
|
|
|
assert error == "access_denied"
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
assert not backend.query(models.Consent)
|
2022-12-11 13:43:21 +00:00
|
|
|
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_code_challenge(testclient, logged_user, client, backend):
|
|
|
|
assert not backend.query(models.Consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
client.token_endpoint_auth_method = "none"
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(client)
|
2020-08-25 13:51:49 +00:00
|
|
|
|
2020-08-25 13:28:13 +00:00
|
|
|
code_verifier = gen_salt(48)
|
|
|
|
code_challenge = create_s256_code_challenge(code_verifier)
|
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
code_challenge=code_challenge,
|
|
|
|
code_challenge_method="S256",
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2020-08-25 13:28:13 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-08-25 13:28:13 +00:00
|
|
|
)
|
|
|
|
|
2020-10-30 22:41:02 +00:00
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
2020-08-25 13:28:13 +00:00
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2020-08-25 13:28:13 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2020-08-25 13:28:13 +00:00
|
|
|
assert authcode is not None
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-05-20 07:24:24 +00:00
|
|
|
assert "profile" in consents[0].scope
|
|
|
|
|
2020-08-25 13:28:13 +00:00
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2020-08-25 13:28:13 +00:00
|
|
|
code_verifier=code_verifier,
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uri=client.redirect_uris[0],
|
|
|
|
client_id=client.client_id,
|
2020-08-25 13:28:13 +00:00
|
|
|
),
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-08-25 13:28:13 +00:00
|
|
|
)
|
|
|
|
access_token = res.json["access_token"]
|
|
|
|
|
2024-04-14 15:30:59 +00:00
|
|
|
token = backend.get(models.Token, access_token=access_token)
|
2023-03-08 22:53:53 +00:00
|
|
|
assert token.client == client
|
|
|
|
assert token.subject == logged_user
|
2020-08-25 13:28:13 +00:00
|
|
|
|
2020-09-25 09:26:41 +00:00
|
|
|
res = testclient.get(
|
2020-10-30 18:19:34 +00:00
|
|
|
"/oauth/userinfo",
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
status=200,
|
2020-09-25 09:26:41 +00:00
|
|
|
)
|
2022-12-24 00:44:16 +00:00
|
|
|
assert res.json["name"] == "John (johnny) Doe"
|
2020-08-25 13:51:49 +00:00
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
client.token_endpoint_auth_method = "client_secret_basic"
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(client)
|
2020-09-17 08:00:39 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
for consent in consents:
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2020-09-17 08:00:39 +00:00
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_consent_already_given(testclient, logged_user, client, backend):
|
|
|
|
assert not backend.query(models.Consent)
|
2020-09-17 08:00:39 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2020-09-17 08:00:39 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-09-17 08:00:39 +00:00
|
|
|
)
|
|
|
|
|
2020-10-30 22:41:02 +00:00
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
2020-09-17 08:00:39 +00:00
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2020-09-17 08:00:39 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2020-09-17 08:00:39 +00:00
|
|
|
assert authcode is not None
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-01-11 16:57:58 +00:00
|
|
|
assert "profile" in consents[0].scope
|
2020-09-17 08:00:39 +00:00
|
|
|
|
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uri=client.redirect_uris[0],
|
2020-09-17 08:00:39 +00:00
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
2020-10-30 18:19:34 +00:00
|
|
|
status=200,
|
2020-09-17 08:00:39 +00:00
|
|
|
)
|
|
|
|
assert "access_token" in res.json
|
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2020-09-17 08:00:39 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
2020-10-30 22:41:02 +00:00
|
|
|
status=302,
|
2020-09-17 08:00:39 +00:00
|
|
|
)
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2020-09-17 08:00:39 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
assert "code" in params
|
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
for consent in consents:
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2020-09-17 08:00:39 +00:00
|
|
|
|
2024-04-17 10:58:58 +00:00
|
|
|
def test_when_consent_already_given_but_for_a_smaller_scope(
|
2024-04-10 13:44:11 +00:00
|
|
|
testclient, logged_user, client, backend
|
2021-10-27 07:31:24 +00:00
|
|
|
):
|
2024-04-10 13:44:11 +00:00
|
|
|
assert not backend.query(models.Consent)
|
2021-10-27 07:31:24 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2021-10-27 07:31:24 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2021-10-27 07:31:24 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2021-10-27 07:31:24 +00:00
|
|
|
assert authcode is not None
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-01-11 16:57:58 +00:00
|
|
|
assert "profile" in consents[0].scope
|
|
|
|
assert "groups" not in consents[0].scope
|
2021-10-27 07:31:24 +00:00
|
|
|
|
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uri=client.redirect_uris[0],
|
2021-10-27 07:31:24 +00:00
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
assert "access_token" in res.json
|
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile groups",
|
2021-10-27 07:31:24 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
|
2022-01-11 16:57:58 +00:00
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2021-10-27 07:31:24 +00:00
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2021-10-27 07:31:24 +00:00
|
|
|
assert authcode is not None
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-01-11 16:57:58 +00:00
|
|
|
assert "profile" in consents[0].scope
|
|
|
|
assert "groups" in consents[0].scope
|
2021-10-27 07:31:24 +00:00
|
|
|
|
2022-05-20 07:24:24 +00:00
|
|
|
for consent in consents:
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-05-20 07:24:24 +00:00
|
|
|
|
2021-10-27 07:31:24 +00:00
|
|
|
|
2024-04-14 20:51:58 +00:00
|
|
|
def test_user_cannot_use_oidc(
|
|
|
|
testclient, user, client, keypair, trusted_client, backend
|
|
|
|
):
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE"]["ACL"]["DEFAULT"]["PERMISSIONS"] = []
|
2024-04-14 20:51:58 +00:00
|
|
|
backend.reload(user)
|
2021-12-06 23:07:32 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client.client_id,
|
2022-05-13 13:56:31 +00:00
|
|
|
scope="openid profile",
|
2021-12-06 23:07:32 +00:00
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
)
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.follow()
|
2021-12-06 23:07:32 +00:00
|
|
|
|
2023-05-11 13:33:34 +00:00
|
|
|
res.form["login"] = "user"
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.form.submit()
|
|
|
|
res = res.follow()
|
2021-12-06 23:07:32 +00:00
|
|
|
|
|
|
|
res.form["password"] = "correct horse battery staple"
|
|
|
|
res = res.form.submit(status=302)
|
2023-12-22 15:39:20 +00:00
|
|
|
res = res.follow(status=403)
|
2021-12-06 23:07:32 +00:00
|
|
|
|
|
|
|
|
2022-05-19 10:36:39 +00:00
|
|
|
def test_nonce_required_in_oidc_requests(testclient, logged_user, client):
|
2022-05-13 13:56:31 +00:00
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id=client.client_id,
|
|
|
|
scope="openid profile",
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res.json.get("error") == "invalid_request"
|
|
|
|
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_nonce_not_required_in_oauth_requests(testclient, logged_user, client, backend):
|
|
|
|
assert not backend.query(models.Consent)
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE_OIDC"]["REQUIRE_NONCE"] = False
|
2022-05-13 13:56:31 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id=client.client_id,
|
|
|
|
scope="profile",
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
|
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
2024-04-10 13:44:11 +00:00
|
|
|
for consent in backend.query(models.Consent):
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-07-07 14:05:34 +00:00
|
|
|
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
def test_request_scope_too_large(testclient, logged_user, keypair, client, backend):
|
|
|
|
assert not backend.query(models.Consent)
|
2023-12-23 16:23:19 +00:00
|
|
|
client.scope = ["openid", "profile", "groups"]
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(client)
|
2022-07-07 14:05:34 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
2023-12-23 16:23:19 +00:00
|
|
|
client_id=client.client_id,
|
2022-07-07 14:05:34 +00:00
|
|
|
scope="openid profile email",
|
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
|
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2023-11-22 15:30:38 +00:00
|
|
|
assert set(authcode.scope) == {
|
2022-07-07 14:05:34 +00:00
|
|
|
"openid",
|
|
|
|
"profile",
|
|
|
|
}
|
|
|
|
|
2024-04-10 13:44:11 +00:00
|
|
|
consents = backend.query(models.Consent, client=client, subject=logged_user)
|
2022-07-07 14:05:34 +00:00
|
|
|
assert set(consents[0].scope) == {
|
|
|
|
"openid",
|
|
|
|
"profile",
|
|
|
|
}
|
|
|
|
|
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
|
|
|
scope="openid profile email groups address phone",
|
2023-12-23 16:23:19 +00:00
|
|
|
redirect_uri=client.redirect_uris[0],
|
2022-07-07 14:05:34 +00:00
|
|
|
),
|
2023-12-23 16:23:19 +00:00
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
2022-07-07 14:05:34 +00:00
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
access_token = res.json["access_token"]
|
2024-04-14 15:30:59 +00:00
|
|
|
token = backend.get(models.Token, access_token=access_token)
|
2023-12-23 16:23:19 +00:00
|
|
|
assert token.client == client
|
2023-03-08 22:53:53 +00:00
|
|
|
assert token.subject == logged_user
|
2023-11-22 15:36:42 +00:00
|
|
|
assert set(token.scope) == {
|
2022-07-07 14:05:34 +00:00
|
|
|
"openid",
|
|
|
|
"profile",
|
|
|
|
}
|
|
|
|
|
|
|
|
id_token = res.json["id_token"]
|
|
|
|
claims = jwt.decode(id_token, keypair[1])
|
2023-11-15 17:20:13 +00:00
|
|
|
assert logged_user.user_name == claims["sub"]
|
|
|
|
assert logged_user.formatted_name == claims["name"]
|
2022-07-07 14:05:34 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/userinfo",
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
status=200,
|
|
|
|
)
|
2022-12-24 00:44:16 +00:00
|
|
|
assert res.json["name"] == "John (johnny) Doe"
|
2022-07-07 14:05:34 +00:00
|
|
|
|
|
|
|
for consent in consents:
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(consent)
|
2022-12-27 17:07:24 +00:00
|
|
|
|
|
|
|
|
2024-04-17 10:58:58 +00:00
|
|
|
def test_code_expired(testclient, user, client):
|
2024-05-02 08:26:32 +00:00
|
|
|
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
|
2022-12-27 17:07:24 +00:00
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id=client.client_id,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
)
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.follow()
|
|
|
|
|
2023-05-11 13:33:34 +00:00
|
|
|
res.form["login"] = "user"
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.form.submit(name="answer", value="accept").follow()
|
|
|
|
|
2022-12-27 17:07:24 +00:00
|
|
|
res.form["password"] = "correct horse battery staple"
|
|
|
|
res = res.form.submit(name="answer", value="accept").follow()
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
|
|
|
|
2024-05-02 08:26:32 +00:00
|
|
|
with time_machine.travel("2021-01-01 01:00:00+00:00", tick=False):
|
2022-12-27 17:07:24 +00:00
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
redirect_uri=client.redirect_uris[0],
|
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
assert res.json == {
|
|
|
|
"error": "invalid_grant",
|
|
|
|
"error_description": 'Invalid "code" in request.',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-14 15:30:59 +00:00
|
|
|
def test_code_with_invalid_user(testclient, admin, client, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
user = models.User(
|
2023-02-05 17:57:18 +00:00
|
|
|
formatted_name="John Doe",
|
|
|
|
family_name="Doe",
|
|
|
|
user_name="temp",
|
2024-11-20 22:30:44 +00:00
|
|
|
emails=["temp@temp.test"],
|
2023-04-10 19:42:14 +00:00
|
|
|
password="correct horse battery staple",
|
2022-12-27 17:07:24 +00:00
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(user)
|
2022-12-27 17:07:24 +00:00
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id=client.client_id,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
nonce="somenonce",
|
|
|
|
),
|
2023-08-31 16:49:31 +00:00
|
|
|
).follow()
|
|
|
|
|
2023-05-11 13:33:34 +00:00
|
|
|
res.form["login"] = "temp"
|
2023-08-31 16:49:31 +00:00
|
|
|
res = res.form.submit(name="answer", value="accept", status=302).follow()
|
|
|
|
|
2022-12-27 17:07:24 +00:00
|
|
|
res.form["password"] = "correct horse battery staple"
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302).follow()
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2022-12-27 17:07:24 +00:00
|
|
|
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(user)
|
2022-12-27 17:07:24 +00:00
|
|
|
|
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
redirect_uri=client.redirect_uris[0],
|
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
assert res.json == {
|
|
|
|
"error": "invalid_grant",
|
|
|
|
"error_description": 'There is no "user" for this code.',
|
|
|
|
}
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(authcode)
|
2024-04-17 11:05:14 +00:00
|
|
|
|
|
|
|
|
2024-04-14 15:30:59 +00:00
|
|
|
def test_locked_account(
|
|
|
|
testclient, logged_user, client, keypair, trusted_client, backend
|
|
|
|
):
|
2024-04-17 11:05:14 +00:00
|
|
|
"""Users with a locked account should not be able to exchange code against
|
|
|
|
tokens."""
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
client_id=client.client_id,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
logged_user.lock_date = datetime.datetime.now(datetime.timezone.utc)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(logged_user)
|
2024-04-17 11:05:14 +00:00
|
|
|
|
|
|
|
res = res.form.submit(name="answer", value="accept", status=302)
|
|
|
|
|
|
|
|
assert res.location.startswith(client.redirect_uris[0])
|
|
|
|
params = parse_qs(urlsplit(res.location).query)
|
|
|
|
code = params["code"][0]
|
2024-04-14 15:30:59 +00:00
|
|
|
authcode = backend.get(models.AuthorizationCode, code=code)
|
2024-04-17 11:05:14 +00:00
|
|
|
assert authcode is not None
|
|
|
|
|
|
|
|
res = testclient.post(
|
|
|
|
"/oauth/token",
|
|
|
|
params=dict(
|
|
|
|
grant_type="authorization_code",
|
|
|
|
code=code,
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
redirect_uri=client.redirect_uris[0],
|
|
|
|
),
|
|
|
|
headers={"Authorization": f"Basic {client_credentials(client)}"},
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert "access_token" not in res.json
|
2024-09-13 13:07:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_missing_client_id(
|
|
|
|
testclient, logged_user, client, keypair, trusted_client, backend
|
|
|
|
):
|
|
|
|
"""Missing client_id should raise a 400 error."""
|
|
|
|
|
|
|
|
res = testclient.get(
|
|
|
|
"/oauth/authorize",
|
|
|
|
params=dict(
|
|
|
|
response_type="code",
|
|
|
|
scope="openid profile email groups address phone",
|
|
|
|
nonce="somenonce",
|
|
|
|
),
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
res.mustcontain("client_id parameter is missing.")
|