tba-runtime-relying-party-session 0.4.0.dev3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tba-runtime-relying-party-session
|
|
3
|
+
Version: 0.4.0.dev3
|
|
4
|
+
Summary: Server-side RP login transaction and one-time state runtime.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.10,<3.15
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# tba-runtime-relying-party-session
|
|
16
|
+
|
|
17
|
+
Server-owned, expiring, one-time RP login transaction state. The in-memory repository is intended for tests and single-process development; production adapters implement the same repository boundary.
|
|
18
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "tba-runtime-relying-party-session"
|
|
3
|
+
version = "0.4.0.dev3"
|
|
4
|
+
description = "Server-side RP login transaction and one-time state runtime."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10,<3.15"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
dependencies = []
|
|
9
|
+
|
|
10
|
+
[build-system]
|
|
11
|
+
requires = ["poetry-core>=1.0.0"]
|
|
12
|
+
build-backend = "poetry.core.masonry.api"
|
|
13
|
+
|
|
14
|
+
[tool.poetry]
|
|
15
|
+
packages = [{ include = "tba_runtime_relying_party_session", from = "src" }]
|
tba_runtime_relying_party_session-0.4.0.dev3/src/tba_runtime_relying_party_session/__init__.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Server-side RP transaction runtime."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import datetime, timedelta, timezone
|
|
7
|
+
from secrets import token_urlsafe
|
|
8
|
+
from typing import Callable, Protocol
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True, slots=True)
|
|
12
|
+
class RPTransaction:
|
|
13
|
+
state: str
|
|
14
|
+
nonce: str
|
|
15
|
+
code_verifier: str
|
|
16
|
+
issuer: str
|
|
17
|
+
client_id: str
|
|
18
|
+
redirect_uri: str
|
|
19
|
+
scope: tuple[str, ...]
|
|
20
|
+
created_at: datetime
|
|
21
|
+
expires_at: datetime
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class RPTransactionRepository(Protocol):
|
|
25
|
+
def save(self, transaction: RPTransaction) -> None: ...
|
|
26
|
+
def consume(self, state: str) -> RPTransaction: ...
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class InMemoryRPTransactionRepository:
|
|
30
|
+
def __init__(self) -> None:
|
|
31
|
+
self._items: dict[str, RPTransaction] = {}
|
|
32
|
+
self._consumed: set[str] = set()
|
|
33
|
+
|
|
34
|
+
def save(self, transaction: RPTransaction) -> None:
|
|
35
|
+
self._items[transaction.state] = transaction
|
|
36
|
+
|
|
37
|
+
def consume(self, state: str) -> RPTransaction:
|
|
38
|
+
if state in self._consumed:
|
|
39
|
+
raise ValueError("RP transaction state was already consumed")
|
|
40
|
+
try:
|
|
41
|
+
transaction = self._items.pop(state)
|
|
42
|
+
except KeyError as exc:
|
|
43
|
+
raise ValueError("RP transaction state is unknown") from exc
|
|
44
|
+
self._consumed.add(state)
|
|
45
|
+
return transaction
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class RPTransactionRuntime:
|
|
49
|
+
def __init__(self, repository: RPTransactionRepository, *, lifetime: timedelta = timedelta(minutes=10)) -> None:
|
|
50
|
+
self._repository = repository
|
|
51
|
+
self._lifetime = lifetime
|
|
52
|
+
|
|
53
|
+
def begin(self, *, issuer: str, client_id: str, redirect_uri: str, scope: tuple[str, ...], now: datetime | None = None) -> RPTransaction:
|
|
54
|
+
created_at = now or datetime.now(timezone.utc)
|
|
55
|
+
transaction = RPTransaction(token_urlsafe(32), token_urlsafe(32), token_urlsafe(64), issuer, client_id, redirect_uri, tuple(scope), created_at, created_at + self._lifetime)
|
|
56
|
+
self._repository.save(transaction)
|
|
57
|
+
return transaction
|
|
58
|
+
|
|
59
|
+
def consume(self, state: str, *, now: datetime | None = None) -> RPTransaction:
|
|
60
|
+
transaction = self._repository.consume(state)
|
|
61
|
+
if (now or datetime.now(timezone.utc)) >= transaction.expires_at:
|
|
62
|
+
raise ValueError("RP transaction state expired")
|
|
63
|
+
return transaction
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass(frozen=True, slots=True)
|
|
67
|
+
class RPServerSession:
|
|
68
|
+
session_id: str
|
|
69
|
+
subject: str
|
|
70
|
+
encrypted_refresh_token: str | None
|
|
71
|
+
version: int
|
|
72
|
+
expires_at: datetime
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class InMemoryRPSessionRepository:
|
|
76
|
+
def __init__(self) -> None:
|
|
77
|
+
self._items: dict[str, RPServerSession] = {}
|
|
78
|
+
|
|
79
|
+
def save(self, session: RPServerSession) -> None:
|
|
80
|
+
self._items[session.session_id] = session
|
|
81
|
+
|
|
82
|
+
def get(self, session_id: str) -> RPServerSession:
|
|
83
|
+
return self._items[session_id]
|
|
84
|
+
|
|
85
|
+
def compare_and_swap(self, session_id: str, expected_version: int, replacement: RPServerSession) -> bool:
|
|
86
|
+
current = self._items.get(session_id)
|
|
87
|
+
if current is None or current.version != expected_version:
|
|
88
|
+
return False
|
|
89
|
+
self._items[session_id] = replacement
|
|
90
|
+
return True
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class RPSessionRuntime:
|
|
94
|
+
def __init__(self, repository: InMemoryRPSessionRepository, *, encrypt: Callable[[str], str]) -> None:
|
|
95
|
+
self._repository = repository
|
|
96
|
+
self._encrypt = encrypt
|
|
97
|
+
|
|
98
|
+
def create(self, *, subject: str, refresh_token: str | None, expires_at: datetime) -> RPServerSession:
|
|
99
|
+
session = RPServerSession(token_urlsafe(32), subject, self._encrypt(refresh_token) if refresh_token else None, 1, expires_at)
|
|
100
|
+
self._repository.save(session)
|
|
101
|
+
return session
|
|
102
|
+
|
|
103
|
+
def rotate_refresh_token(self, session_id: str, *, expected_version: int, refresh_token: str, expires_at: datetime) -> RPServerSession:
|
|
104
|
+
current = self._repository.get(session_id)
|
|
105
|
+
replacement = RPServerSession(current.session_id, current.subject, self._encrypt(refresh_token), expected_version + 1, expires_at)
|
|
106
|
+
if not self._repository.compare_and_swap(session_id, expected_version, replacement):
|
|
107
|
+
raise ValueError("concurrent refresh rotation rejected")
|
|
108
|
+
return replacement
|
|
109
|
+
|
|
110
|
+
__all__ = ["InMemoryRPSessionRepository", "InMemoryRPTransactionRepository", "RPServerSession", "RPSessionRuntime", "RPTransaction", "RPTransactionRepository", "RPTransactionRuntime"]
|