Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/replace jose with jwcrypto #531

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ celerybeat-schedule

# dotenv
.env
.envrc

# virtualenv
.venv
Expand Down
80 changes: 15 additions & 65 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Documentation = "https://python-keycloak.readthedocs.io/en/latest/"
[tool.poetry.dependencies]
python = ">=3.8,<4.0"
requests = ">=2.20.0"
python-jose = ">=3.3.0"
mock = {version = "^4.0.3", optional = true}
alabaster = {version = "^0.7.12", optional = true}
commonmark = {version = "^0.9.1", optional = true}
Expand All @@ -43,6 +42,7 @@ m2r2 = {version = "^0.3.2", optional = true}
sphinx-autoapi = {version = "^3.0.0", optional = true}
requests-toolbelt = ">=0.6.0"
deprecation = ">=2.1.0"
jwcrypto = "^1.5.4"

[tool.poetry.extras]
docs = [
Expand Down
13 changes: 11 additions & 2 deletions src/keycloak/keycloak_openid.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class to handle authentication and token manipulation.
import json
from typing import Optional

from jose import jwt
from jwcrypto import jwk, jwt

from .authorization import Authorization
from .connection import ConnectionManager
Expand Down Expand Up @@ -539,7 +539,16 @@ def decode_token(self, token, key, algorithms=["RS256"], **kwargs):
:returns: Decoded token
:rtype: dict
"""
return jwt.decode(token, key, algorithms=algorithms, audience=self.client_id, **kwargs)
# To keep the same API, we map the python-jose options to our claims for jwcrypto
# Per the jwcrypto dev, `exp` and `nbf` are always checked
options = kwargs.get("options", {})
check_claims = {}
if options.get("verify_aud") is True:
check_claims["aud"] = self.client_id

k = jwk.JWK.from_pem(key.encode("utf-8"))
full_jwt = jwt.JWT(jwt=token, key=k, algs=algorithms, check_claims=check_claims)
return jwt.json_decode(full_jwt.claims)

def load_authorization_config(self, path):
"""Load Keycloak settings (authorization).
Expand Down
4 changes: 1 addition & 3 deletions tests/test_keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,9 +1638,7 @@ def test_client_roles(admin: KeycloakAdmin, client: str):

# Test update client role
res = admin.update_client_role(
client_id=client,
role_name="client-role-test",
payload={"name": "client-role-test-update"},
client_id=client, role_name="client-role-test", payload={"name": "client-role-test-update"}
)
assert res == dict()
with pytest.raises(KeycloakPutError) as err:
Expand Down
Loading