canaille-globuzma/tests/oidc/test_implicit_flow.py

161 lines
4.8 KiB
Python
Raw Normal View History

2021-12-20 22:57:27 +00:00
from urllib.parse import parse_qs
from urllib.parse import urlsplit
2020-08-23 17:56:37 +00:00
from authlib.jose import jwt
from canaille.app import models
2020-08-20 14:02:14 +00:00
def test_oauth_implicit(testclient, user, client, backend):
2022-10-17 15:49:52 +00:00
client.grant_types = ["token"]
client.token_endpoint_auth_method = "none"
2020-08-20 14:02:14 +00:00
backend.save(client)
2020-08-20 14:02:14 +00:00
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="token",
client_id=client.client_id,
2020-08-20 14:02:14 +00:00
scope="profile",
nonce="somenonce",
),
).follow()
2020-10-30 22:41:02 +00:00
assert "text/html" == res.content_type
2020-08-20 14:02:14 +00:00
2020-10-28 18:12:08 +00:00
res.form["login"] = "user"
res = res.form.submit(status=302).follow()
2020-08-20 14:02:14 +00:00
res.form["password"] = "correct horse battery staple"
res = res.form.submit(status=302).follow()
2020-08-20 14:02:14 +00:00
2020-10-30 22:41:02 +00:00
assert "text/html" == res.content_type, res.json
2020-08-20 14:02:14 +00:00
2020-10-30 22:41:02 +00:00
res = res.form.submit(name="answer", value="accept", status=302)
2020-08-20 14:02:14 +00:00
assert res.location.startswith(client.redirect_uris[0])
2020-08-20 14:02:14 +00:00
params = parse_qs(urlsplit(res.location).fragment)
2020-08-23 17:56:37 +00:00
2020-08-20 14:02:14 +00:00
access_token = params["access_token"][0]
token = backend.get(models.Token, access_token=access_token)
2020-08-23 17:56:37 +00:00
assert token is not None
2020-09-25 09:26:41 +00:00
res = testclient.get(
"/oauth/userinfo", headers={"Authorization": f"Bearer {access_token}"}
)
2020-10-30 22:41:02 +00:00
assert "application/json" == res.content_type
2022-12-24 00:44:16 +00:00
assert res.json["name"] == "John (johnny) Doe"
2020-08-23 17:56:37 +00:00
2022-10-17 15:49:52 +00:00
client.grant_types = ["code"]
client.token_endpoint_auth_method = "client_secret_basic"
backend.save(client)
2020-08-20 14:02:14 +00:00
2020-08-23 17:56:37 +00:00
def test_oidc_implicit(testclient, keypair, user, client, trusted_client, backend):
2022-10-17 15:49:52 +00:00
client.grant_types = ["token id_token"]
client.token_endpoint_auth_method = "none"
2020-08-23 17:56:37 +00:00
backend.save(client)
2020-08-23 17:56:37 +00:00
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="id_token token",
client_id=client.client_id,
2020-08-23 17:56:37 +00:00
scope="openid profile",
nonce="somenonce",
),
).follow()
2020-10-30 22:41:02 +00:00
assert "text/html" == res.content_type
2020-08-23 17:56:37 +00:00
2020-10-28 18:12:08 +00:00
res.form["login"] = "user"
res = res.form.submit(status=302).follow()
2020-08-23 17:56:37 +00:00
res.form["password"] = "correct horse battery staple"
res = res.form.submit(status=302).follow()
2020-08-23 17:56:37 +00:00
2020-10-30 22:41:02 +00:00
assert "text/html" == res.content_type, res.json
2020-08-23 17:56:37 +00:00
2020-10-30 22:41:02 +00:00
res = res.form.submit(name="answer", value="accept", status=302)
2020-08-23 17:56:37 +00:00
assert res.location.startswith(client.redirect_uris[0])
2020-08-23 17:56:37 +00:00
params = parse_qs(urlsplit(res.location).fragment)
access_token = params["access_token"][0]
token = backend.get(models.Token, access_token=access_token)
2020-08-20 14:02:14 +00:00
assert token is not None
2020-08-23 17:56:37 +00:00
id_token = params["id_token"][0]
2020-08-28 14:07:39 +00:00
claims = jwt.decode(id_token, keypair[1])
assert user.user_name == claims["sub"]
assert user.formatted_name == claims["name"]
assert [client.client_id, trusted_client.client_id] == claims["aud"]
2020-08-23 17:56:37 +00:00
2020-09-25 09:26:41 +00:00
res = testclient.get(
2020-10-30 22:41:02 +00:00
"/oauth/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
status=200,
2020-09-25 09:26:41 +00:00
)
2020-10-30 22:41:02 +00:00
assert "application/json" == res.content_type
2022-12-24 00:44:16 +00:00
assert res.json["name"] == "John (johnny) Doe"
2021-06-03 15:24:36 +00:00
2022-10-17 15:49:52 +00:00
client.grant_types = ["code"]
client.token_endpoint_auth_method = "client_secret_basic"
backend.save(client)
2021-06-03 15:24:36 +00:00
2021-09-28 07:30:41 +00:00
def test_oidc_implicit_with_group(
testclient, keypair, user, client, foo_group, trusted_client, backend
2021-09-28 07:30:41 +00:00
):
2022-10-17 15:49:52 +00:00
client.grant_types = ["token id_token"]
client.token_endpoint_auth_method = "none"
2021-06-03 15:24:36 +00:00
backend.save(client)
2021-06-03 15:24:36 +00:00
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="id_token token",
client_id=client.client_id,
2021-06-03 15:24:36 +00:00
scope="openid profile groups",
nonce="somenonce",
),
).follow()
2021-06-03 15:24:36 +00:00
assert "text/html" == res.content_type
res.form["login"] = "user"
res = res.form.submit(status=302).follow()
2021-06-03 15:24:36 +00:00
res.form["password"] = "correct horse battery staple"
res = res.form.submit(status=302).follow()
2021-06-03 15:24:36 +00:00
assert "text/html" == res.content_type, res.json
res = res.form.submit(name="answer", value="accept", status=302)
assert res.location.startswith(client.redirect_uris[0])
2021-06-03 15:24:36 +00:00
params = parse_qs(urlsplit(res.location).fragment)
access_token = params["access_token"][0]
token = backend.get(models.Token, access_token=access_token)
2021-06-03 15:24:36 +00:00
assert token is not None
id_token = params["id_token"][0]
claims = jwt.decode(id_token, keypair[1])
assert user.user_name == claims["sub"]
assert user.formatted_name == claims["name"]
assert [client.client_id, trusted_client.client_id] == claims["aud"]
2021-06-03 15:24:36 +00:00
assert ["foo"] == claims["groups"]
res = testclient.get(
"/oauth/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
status=200,
)
assert "application/json" == res.content_type
2022-12-24 00:44:16 +00:00
assert res.json["name"] == "John (johnny) Doe"
2020-08-20 14:02:14 +00:00
2022-10-17 15:49:52 +00:00
client.grant_types = ["code"]
client.token_endpoint_auth_method = "client_secret_basic"
backend.save(client)