Skip to content

Commit 9d54133

Browse files
committed
Support SECRET_KEY_FALLBACKS when loading tokens
This allows for key rotation by placing the old key in the fallbacks and switching to a new key. The new key would be used for dumping with both used for loading.
1 parent 9abd946 commit 9d54133

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/quart_auth/extension.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from contextlib import asynccontextmanager
33
from enum import auto, Enum
44
from hashlib import sha512
5-
from typing import Any, AsyncGenerator, cast, Dict, Literal, Optional, Type, Union
5+
from typing import Any, AsyncGenerator, cast, Dict, Iterable, Literal, Optional, Type, Union
66

77
from itsdangerous import BadSignature, SignatureExpired, URLSafeTimedSerializer
88
from quart import (
@@ -46,7 +46,9 @@ class Action(Enum):
4646

4747

4848
class _AuthSerializer(URLSafeTimedSerializer):
49-
def __init__(self, secret: Union[str, bytes], salt: Union[str, bytes]) -> None:
49+
def __init__(
50+
self, secret: Union[str, bytes, Iterable[str], Iterable[bytes]], salt: Union[str, bytes]
51+
) -> None:
5052
super().__init__(secret, salt, signer_kwargs={"digest_method": sha512})
5153

5254

@@ -203,7 +205,12 @@ def load_token(self, token: str, app: Optional[Quart] = None) -> Optional[str]:
203205
if app is None:
204206
app = current_app
205207

206-
serializer = self.serializer_class(app.secret_key, self.salt)
208+
keys = [app.secret_key]
209+
210+
if fallbacks := app.config.get("SECRET_KEY_FALLBACKS"):
211+
keys.extend(fallbacks)
212+
213+
serializer = self.serializer_class(keys, self.salt) # type: ignore[arg-type]
207214
try:
208215
return serializer.loads(token, max_age=self.duration)
209216
except (BadSignature, SignatureExpired):

0 commit comments

Comments
 (0)