Skip to content

Commit 847f6f5

Browse files
committed
feat: make AuthSettings compatible with single key
1 parent a061ff6 commit 847f6f5

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

diracx-core/src/diracx/core/settings.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
from botocore.config import Config
2323
from botocore.errorfactory import ClientError
2424
from cryptography.fernet import Fernet
25-
from joserfc.jws import KeySet
25+
from joserfc.jwk import JWKRegistry, KeySet
2626
from pydantic import (
27+
AliasChoices,
2728
AnyUrl,
2829
BeforeValidator,
2930
Field,
@@ -70,7 +71,7 @@ def __init__(self, data: str):
7071

7172

7273
def _maybe_load_keys_from_file(value: Any) -> Any:
73-
"""Load private keys from files if needed."""
74+
"""Load jwks from files if needed."""
7475
if isinstance(value, str):
7576
# If the value is a string, we need to check if it is a JSON string or a file URL
7677
if not (value.strip().startswith("{") or value.startswith("[")):
@@ -85,8 +86,16 @@ def _maybe_load_keys_from_file(value: Any) -> Any:
8586
return value
8687

8788

89+
def _maybe_load_and_wrap_single_key(value: Any) -> Any:
90+
if isinstance(value, str) and value.strip().startswith("-----BEGIN"):
91+
return json.dumps(KeySet(keys=[JWKRegistry.import_key(value)]).as_dict(private=True)) # type: ignore
92+
return value
93+
94+
8895
TokenSigningKeyStore = Annotated[
89-
_TokenSigningKeyStore, BeforeValidator(_maybe_load_keys_from_file)
96+
_TokenSigningKeyStore,
97+
BeforeValidator(_maybe_load_keys_from_file),
98+
BeforeValidator(_maybe_load_and_wrap_single_key),
9099
]
91100

92101

@@ -151,7 +160,9 @@ class AuthSettings(ServiceSettingsBase):
151160
state_key: FernetKey
152161

153162
token_issuer: str
154-
token_keystore: TokenSigningKeyStore
163+
token_keystore: TokenSigningKeyStore = Field(
164+
validation_alias=AliasChoices("token_keystore", "token_key")
165+
)
155166
token_allowed_algorithms: list[str] = ["RS256", "EdDSA"] # noqa: S105
156167
access_token_expire_minutes: int = 20
157168
refresh_token_expire_minutes: int = 60

diracx-logic/src/diracx/logic/auth/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from cachetools import TTLCache
1010
from cryptography.fernet import Fernet
1111
from joserfc import jwt
12-
from joserfc.jws import KeySet
12+
from joserfc.jwk import KeySet
1313
from joserfc.jwt import Claims, JWTClaimsRegistry
1414
from typing_extensions import TypedDict
1515
from uuid_utils import UUID

diracx-routers/tests/auth/test_standard.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
import jwt
1313
import pytest
1414
from cryptography.fernet import Fernet
15-
from joserfc.jwk import RSAKey, OKPKey
16-
from joserfc.jws import KeySet
15+
from joserfc.jwk import RSAKey, OKPKey, KeySet
1716
from joserfc.errors import UnsupportedKeyOperationError
1817
from pytest_httpx import HTTPXMock
1918
from uuid_utils import uuid7

0 commit comments

Comments
 (0)