|
11 | 11 |
|
12 | 12 |
|
13 | 13 | class StatelessOAuthStateStore(OAuthStateStore, AsyncOAuthStateStore): |
| 14 | + """A stateless OAuth ``state`` store backed by a signed, self-contained token. |
| 15 | +
|
| 16 | + ``issue()`` creates an HMAC-SHA256 signed token in the JWT style. The token |
| 17 | + carries only an expiration claim. ``consume()`` re-verifies the signature and |
| 18 | + the expiry. Nothing is stored server-side. No storage backend is required. |
| 19 | +
|
| 20 | + .. warning:: |
| 21 | + This store provides **no one-time-use / anti-replay guarantee**. It returns |
| 22 | + ``True`` for the same token every time until that token expires. This is |
| 23 | + inherent to a stateless token. The token can only be invalidated by expiry. |
| 24 | + It cannot be revoked early unless you add server-side state, and that would |
| 25 | + defeat the point of being stateless. |
| 26 | +
|
| 27 | + Consequences for integrators: |
| 28 | +
|
| 29 | + - **Keep the token lifetime short.** A token stays valid until it expires. |
| 30 | + Use a short ``expiration_seconds``. A value of ``300`` (5 minutes) is a |
| 31 | + good choice and matches the OAuth sample. This limits how long a captured |
| 32 | + ``state`` can be replayed. |
| 33 | + - **Make the callback idempotent.** A captured ``state`` can be replayed |
| 34 | + within its lifetime. Your callback may run more than once for a single |
| 35 | + authorization. The ``oauth.v2.access`` exchange and the installation |
| 36 | + persistence must tolerate this. Slack authorization codes are single-use. |
| 37 | + A sequential replay fails the token exchange. The real exposure is a |
| 38 | + concurrent duplicate callback. |
| 39 | + - **CSRF protection is unchanged.** CSRF defense comes from the |
| 40 | + ``Secure; HttpOnly`` state cookie. ``OAuthStateUtils`` validates that cookie |
| 41 | + before ``consume()`` runs in the callback. Compared to the stateful stores, |
| 42 | + this store only gives up anti-replay and defense-in-depth. It does not give |
| 43 | + up CSRF protection. |
| 44 | +
|
| 45 | + Args: |
| 46 | + expiration_seconds: Lifetime of an issued ``state`` token in seconds. After |
| 47 | + this window ``consume()`` returns ``False``. Keep this value short. A |
| 48 | + value of ``300`` (5 minutes) is a good default. Avoid long lifetimes. |
| 49 | + signing_secret: Secret used to sign and verify tokens with HMAC. Keep it |
| 50 | + secret. Keep it the same across every instance that must validate a |
| 51 | + shared token. |
| 52 | + logger: Logger for validation warnings. |
| 53 | + """ |
| 54 | + |
14 | 55 | def __init__( |
15 | 56 | self, |
16 | 57 | *, |
@@ -55,6 +96,12 @@ def issue(self, *args, **kwargs) -> str: |
55 | 96 | return f"{header}.{payload}.{signature}" |
56 | 97 |
|
57 | 98 | def consume(self, state: str) -> bool: |
| 99 | + """Validate a ``state`` token's signature and ``expiration`` claim. |
| 100 | +
|
| 101 | + Returns ``True`` for any correctly signed, unexpired token, **every time**. |
| 102 | + This store keeps no record of consumed tokens. It does not enforce |
| 103 | + one-time-use. See the class docstring for the anti-replay caveat. |
| 104 | + """ |
58 | 105 | try: |
59 | 106 | parts = state.split(".") |
60 | 107 | if len(parts) != 3: |
|
0 commit comments