canaille-globuzma/tests/test_implicit_flow.py

170 lines
4.9 KiB
Python
Raw Normal View History

2020-08-23 17:56:37 +00:00
from authlib.jose import jwt
2020-08-20 14:02:14 +00:00
from urllib.parse import urlsplit, parse_qs
2020-10-21 12:04:40 +00:00
from canaille.models import Token
2020-08-20 14:02:14 +00:00
2020-08-23 17:56:37 +00:00
def test_oauth_implicit(testclient, slapd_connection, user, client):
2020-08-20 14:02:14 +00:00
client.oauthGrantType = ["token"]
client.oauthTokenEndpointAuthMethod = "none"
client.save(slapd_connection)
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="token",
client_id=client.oauthClientID,
scope="profile",
nonce="somenonce",
),
)
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"
2020-08-20 14:02:14 +00:00
res.form["password"] = "correct horse battery staple"
2020-10-30 22:41:02 +00:00
res = res.form.submit(status=302)
2020-08-20 14:02:14 +00:00
res = res.follow()
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.oauthRedirectURIs[0])
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]
2020-08-23 17:56:37 +00:00
token = Token.get(access_token, conn=slapd_connection)
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
2021-09-28 07:30:41 +00:00
assert {
"name": "John Doe",
"sub": "user",
"family_name": "Doe",
"groups": [],
} == res.json
2020-08-23 17:56:37 +00:00
client.oauthGrantType = ["code"]
client.oauthTokenEndpointAuthMethod = "client_secret_basic"
client.save(slapd_connection)
2020-08-20 14:02:14 +00:00
2020-08-23 17:56:37 +00:00
2020-08-28 14:07:39 +00:00
def test_oidc_implicit(testclient, keypair, slapd_connection, user, client):
2020-08-23 17:56:37 +00:00
client.oauthGrantType = ["token id_token"]
client.oauthTokenEndpointAuthMethod = "none"
client.save(slapd_connection)
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="id_token token",
client_id=client.oauthClientID,
scope="openid profile",
nonce="somenonce",
),
)
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"
2020-08-23 17:56:37 +00:00
res.form["password"] = "correct horse battery staple"
2020-10-30 22:41:02 +00:00
res = res.form.submit(status=302)
2020-08-23 17:56:37 +00:00
2020-10-30 22:41:02 +00:00
res = res.follow(status=200)
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.oauthRedirectURIs[0])
params = parse_qs(urlsplit(res.location).fragment)
access_token = params["access_token"][0]
2020-08-20 14:02:14 +00:00
token = Token.get(access_token, conn=slapd_connection)
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])
2020-08-24 08:03:48 +00:00
assert user.uid[0] == claims["sub"]
assert user.cn[0] == claims["name"]
assert [client.oauthClientID] == 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
2021-09-28 07:30:41 +00:00
assert {
"name": "John Doe",
"sub": "user",
"family_name": "Doe",
"groups": [],
} == res.json
2021-06-03 15:24:36 +00:00
client.oauthGrantType = ["code"]
client.oauthTokenEndpointAuthMethod = "client_secret_basic"
client.save(slapd_connection)
2021-09-28 07:30:41 +00:00
def test_oidc_implicit_with_group(
testclient, keypair, slapd_connection, user, client, foo_group
):
2021-06-03 15:24:36 +00:00
client.oauthGrantType = ["token id_token"]
client.oauthTokenEndpointAuthMethod = "none"
client.save(slapd_connection)
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="id_token token",
client_id=client.oauthClientID,
scope="openid profile groups",
nonce="somenonce",
),
)
assert "text/html" == res.content_type
res.form["login"] = "user"
res.form["password"] = "correct horse battery staple"
res = res.form.submit(status=302)
res = res.follow(status=200)
assert "text/html" == res.content_type, res.json
res = res.form.submit(name="answer", value="accept", status=302)
assert res.location.startswith(client.oauthRedirectURIs[0])
params = parse_qs(urlsplit(res.location).fragment)
access_token = params["access_token"][0]
token = Token.get(access_token, conn=slapd_connection)
assert token is not None
id_token = params["id_token"][0]
claims = jwt.decode(id_token, keypair[1])
assert user.uid[0] == claims["sub"]
assert user.cn[0] == claims["name"]
assert [client.oauthClientID] == claims["aud"]
assert ["foo"] == claims["groups"]
res = testclient.get(
"/oauth/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
status=200,
)
assert "application/json" == res.content_type
2021-09-28 07:30:41 +00:00
assert {
"name": "John Doe",
"sub": "user",
"family_name": "Doe",
"groups": ["foo"],
} == res.json
2020-08-20 14:02:14 +00:00
client.oauthGrantType = ["code"]
client.oauthTokenEndpointAuthMethod = "client_secret_basic"
client.save(slapd_connection)