Skip to content

Commit 0d01e42

Browse files
committed
refactor: replace python-jose with pyjwt
1 parent 337ed61 commit 0d01e42

File tree

4 files changed

+14
-65
lines changed

4 files changed

+14
-65
lines changed

api/poetry.lock

Lines changed: 2 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = ""
99
cachetools = "^5.3.0"
1010
python = "^3.10"
1111
fastapi = "^0.101.0"
12-
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
12+
pyjwt = "^2.8.0"
1313
uvicorn = {extras = ["standard"], version = "^0.21.1"}
1414
pymongo = "4.1.1"
1515
certifi = "^2023.7.22"

api/src/authentication/authentication.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import httpx
2+
import jwt
23
from cachetools import TTLCache, cached
34
from fastapi import Security
45
from fastapi.security import OAuth2AuthorizationCodeBearer
5-
from jose import JWTError, jwt
66

77
from authentication.mock_token_generator import mock_rsa_public_key
88
from authentication.models import User
@@ -18,18 +18,12 @@
1818

1919

2020
@cached(cache=TTLCache(maxsize=32, ttl=86400))
21-
def fetch_openid_configuration() -> dict[str, str]:
21+
def fetch_openid_configuration() -> jwt.PyJWKClient:
2222
try:
2323
oid_conf_response = httpx.get(config.OAUTH_WELL_KNOWN)
2424
oid_conf_response.raise_for_status()
2525
oid_conf = oid_conf_response.json()
26-
json_web_key_set_response = httpx.get(oid_conf["jwks_uri"])
27-
json_web_key_set_response.raise_for_status()
28-
return {
29-
"authorization_endpoint": oid_conf["authorization_endpoint"],
30-
"token_endpoint": oid_conf["token_endpoint"],
31-
"jwks": json_web_key_set_response.json()["keys"],
32-
}
26+
return jwt.PyJWKClient(oid_conf["jwks_uri"])
3327
except Exception as error:
3428
logger.error(f"Failed to fetch OpenId Connect configuration for '{config.OAUTH_WELL_KNOWN}': {error}")
3529
raise credentials_exception
@@ -41,7 +35,11 @@ def auth_with_jwt(jwt_token: str = Security(oauth2_scheme)) -> User:
4135
if not jwt_token:
4236
raise credentials_exception
4337
# If TEST_TOKEN is true, we are running tests. Use the self-signed keys. If not, get keys from auth server.
44-
key = mock_rsa_public_key if config.TEST_TOKEN else {"keys": fetch_openid_configuration()["jwks"]}
38+
key = (
39+
mock_rsa_public_key
40+
if config.TEST_TOKEN
41+
else fetch_openid_configuration().get_signing_key_from_jwt(jwt_token).key
42+
)
4543

4644
try:
4745
payload = jwt.decode(jwt_token, key, algorithms=["RS256"], audience=config.OAUTH_AUDIENCE)
@@ -50,7 +48,7 @@ def auth_with_jwt(jwt_token: str = Security(oauth2_scheme)) -> User:
5048
user = User(user_id=payload["oid"], **payload)
5149
else:
5250
user = User(user_id=payload["sub"], **payload)
53-
except JWTError as error:
51+
except jwt.exceptions.InvalidTokenError as error:
5452
logger.warning(f"Failed to decode JWT: {error}")
5553
raise credentials_exception
5654

api/src/authentication/mock_token_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from jose import jwt
1+
import jwt
22

33
from authentication.models import User
44
from config import default_user
@@ -63,5 +63,6 @@ def generate_mock_token(user: User = default_user) -> str:
6363
"sub": user.user_id,
6464
"roles": user.roles,
6565
"iss": "mock-auth-server",
66+
"aud": "TEST",
6667
}
6768
return jwt.encode(payload, mock_rsa_private_key, algorithm="RS256")

0 commit comments

Comments
 (0)