Skip to content

Commit e285f92

Browse files
Nathan Furnalmarcospereirampj
Nathan Furnal
authored andcommitted
fix: use jwcrypto and remove python-jose
1 parent 6ba6c90 commit e285f92

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/keycloak/keycloak_openid.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class to handle authentication and token manipulation.
3030
import json
3131
from typing import Optional
3232

33-
from jose import jwt
33+
from jwcrypto import jwk, jwt
3434

3535
from .authorization import Authorization
3636
from .connection import ConnectionManager
@@ -539,7 +539,16 @@ def decode_token(self, token, key, algorithms=["RS256"], **kwargs):
539539
:returns: Decoded token
540540
:rtype: dict
541541
"""
542-
return jwt.decode(token, key, algorithms=algorithms, audience=self.client_id, **kwargs)
542+
# To keep the same API, we map the python-jose options to our claims for jwcrypto
543+
# Per the jwcrypto dev, `exp` and `nbf` are always checked
544+
options = kwargs.get("options", {})
545+
check_claims = {}
546+
if options.get("verify_aud") is True:
547+
check_claims["aud"] = self.client_id
548+
549+
k = jwk.JWK.from_pem(key.encode("utf-8"))
550+
full_jwt = jwt.JWT(jwt=token, key=k, algs=algorithms, check_claims=check_claims)
551+
return jwt.json_decode(full_jwt.claims)
543552

544553
def load_authorization_config(self, path):
545554
"""Load Keycloak settings (authorization).

tests/test_keycloak_admin.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1638,9 +1638,7 @@ def test_client_roles(admin: KeycloakAdmin, client: str):
16381638

16391639
# Test update client role
16401640
res = admin.update_client_role(
1641-
client_id=client,
1642-
role_name="client-role-test",
1643-
payload={"name": "client-role-test-update"},
1641+
client_id=client, role_name="client-role-test", payload={"name": "client-role-test-update"}
16441642
)
16451643
assert res == dict()
16461644
with pytest.raises(KeycloakPutError) as err:

0 commit comments

Comments
 (0)