-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement RFC7523 client JWT authentication and grant
- Loading branch information
Showing
16 changed files
with
450 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ maintainers = [{name="Éloi Rivard", email="[email protected]"}] | |
requires-python = ">=3.10" | ||
dependencies = [ | ||
"flask >= 3.0.0", | ||
"flask-caching>=2.3.0", | ||
"flask-wtf >= 1.2.1", | ||
"pydantic-settings >= 2.0.3", | ||
"requests>=2.32.3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import time | ||
import uuid | ||
|
||
from joserfc import jwt | ||
|
||
from canaille.app import models | ||
|
||
|
||
def test_nominal_case(testclient, logged_user, client, backend, client_jwks): | ||
"""Test JWT grant for a client with consent.""" | ||
now = time.time() | ||
client.preconsent = True | ||
backend.save(client) | ||
|
||
_, private_key = client_jwks | ||
header = {"alg": "RS256"} | ||
payload = { | ||
"iss": client.client_id, | ||
"sub": logged_user.user_name, | ||
"aud": "http://canaille.test/oauth/token", | ||
"nbf": now - 3600, | ||
"exp": now + 3600, | ||
"iat": now - 1, | ||
"jti": str(uuid.uuid4()), | ||
} | ||
client_jwt = jwt.encode(header, payload, private_key) | ||
|
||
res = testclient.post( | ||
"/oauth/token", | ||
params=dict( | ||
grant_type="urn:ietf:params:oauth:grant-type:jwt-bearer", | ||
scope="openid profile email groups address phone", | ||
assertion=client_jwt, | ||
redirect_uri=client.redirect_uris[0], | ||
), | ||
status=200, | ||
) | ||
|
||
access_token = res.json["access_token"] | ||
token = backend.get(models.Token, access_token=access_token) | ||
assert token.client == client | ||
assert token.subject == logged_user | ||
assert set(token.scope) == { | ||
"openid", | ||
"profile", | ||
"email", | ||
"groups", | ||
"address", | ||
"phone", | ||
} | ||
|
||
|
||
def test_no_jwk(testclient, logged_user, client, backend, client_jwks): | ||
"""Test JWT grant for a client without JWKs.""" | ||
now = time.time() | ||
client.preconsent = True | ||
client.jwks = None | ||
backend.save(client) | ||
|
||
_, private_key = client_jwks | ||
header = {"alg": "RS256"} | ||
payload = { | ||
"iss": client.client_id, | ||
"sub": logged_user.user_name, | ||
"aud": "http://canaille.test/oauth/token", | ||
"nbf": now - 3600, | ||
"exp": now + 3600, | ||
"iat": now - 1, | ||
"jti": str(uuid.uuid4()), | ||
} | ||
client_jwt = jwt.encode(header, payload, private_key) | ||
|
||
res = testclient.post( | ||
"/oauth/token", | ||
params=dict( | ||
grant_type="urn:ietf:params:oauth:grant-type:jwt-bearer", | ||
scope="openid profile email groups address phone", | ||
assertion=client_jwt, | ||
redirect_uri=client.redirect_uris[0], | ||
), | ||
status=400, | ||
) | ||
|
||
assert res.json == { | ||
"error": "invalid_client", | ||
"error_description": "No matching JWK", | ||
} |
Oops, something went wrong.