spyddb 0.1.0__py3-none-any.whl
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.
- spyddb/__init__.py +98 -0
- spyddb/auth.py +109 -0
- spyddb/binary.py +717 -0
- spyddb/client.py +888 -0
- spyddb/errors.py +162 -0
- spyddb/orm/__init__.py +138 -0
- spyddb/orm/database.py +207 -0
- spyddb/orm/expression.py +433 -0
- spyddb/orm/fields.py +517 -0
- spyddb/orm/migrations.py +137 -0
- spyddb/orm/model.py +464 -0
- spyddb/orm/query.py +382 -0
- spyddb/orm/schema.py +404 -0
- spyddb/protocol.py +231 -0
- spyddb/values.py +530 -0
- spyddb-0.1.0.dist-info/METADATA +230 -0
- spyddb-0.1.0.dist-info/RECORD +19 -0
- spyddb-0.1.0.dist-info/WHEEL +5 -0
- spyddb-0.1.0.dist-info/top_level.txt +1 -0
spyddb/__init__.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Official Python driver and ORM for SpydDB.
|
|
2
|
+
|
|
3
|
+
The driver speaks SpydWire directly: framed JSON over TCP, token or
|
|
4
|
+
SCRAM-SHA-256 authentication, TLS, and cancellation. The ORM adds declarative
|
|
5
|
+
models, a SpydQL query builder, additive schema sync, and migrations.
|
|
6
|
+
|
|
7
|
+
from spyddb import Client
|
|
8
|
+
|
|
9
|
+
with Client.connect("spydwire://127.0.0.1:5433?token_env=SPYD_TOKEN") as client:
|
|
10
|
+
result = client.execute("users | where active == true | pick email")
|
|
11
|
+
print(result.dicts)
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from .auth import ScramClient
|
|
15
|
+
from .client import Client, Column, ConnectionOptions, Pool, PreparedStatement, Result
|
|
16
|
+
from .errors import (
|
|
17
|
+
SpydAuthenticationError,
|
|
18
|
+
SpydCancelledError,
|
|
19
|
+
SpydConflictError,
|
|
20
|
+
SpydConnectionError,
|
|
21
|
+
SpydConstraintError,
|
|
22
|
+
SpydError,
|
|
23
|
+
SpydMultipleResultsError,
|
|
24
|
+
SpydNoResultError,
|
|
25
|
+
SpydPermissionError,
|
|
26
|
+
SpydProtocolError,
|
|
27
|
+
SpydQueryError,
|
|
28
|
+
SpydResourceError,
|
|
29
|
+
SpydSchemaError,
|
|
30
|
+
SpydTimeoutError,
|
|
31
|
+
SpydTypeError,
|
|
32
|
+
SpydUnsupportedError,
|
|
33
|
+
SpydValueError,
|
|
34
|
+
)
|
|
35
|
+
from .protocol import DEFAULT_MAX_FRAME_BYTES, crc32c
|
|
36
|
+
from .values import (
|
|
37
|
+
ColumnType,
|
|
38
|
+
DecimalParam,
|
|
39
|
+
EnumName,
|
|
40
|
+
Float32,
|
|
41
|
+
GeoPoint,
|
|
42
|
+
Int,
|
|
43
|
+
Text,
|
|
44
|
+
UInt,
|
|
45
|
+
Vector,
|
|
46
|
+
decode_value,
|
|
47
|
+
encode_parameter,
|
|
48
|
+
parse_type,
|
|
49
|
+
)
|
|
50
|
+
from . import orm
|
|
51
|
+
from .orm import Database, Model
|
|
52
|
+
|
|
53
|
+
connect = Database.connect
|
|
54
|
+
|
|
55
|
+
__all__ = [
|
|
56
|
+
"Client",
|
|
57
|
+
"Column",
|
|
58
|
+
"ColumnType",
|
|
59
|
+
"ConnectionOptions",
|
|
60
|
+
"Database",
|
|
61
|
+
"DecimalParam",
|
|
62
|
+
"DEFAULT_MAX_FRAME_BYTES",
|
|
63
|
+
"EnumName",
|
|
64
|
+
"Float32",
|
|
65
|
+
"GeoPoint",
|
|
66
|
+
"Int",
|
|
67
|
+
"Model",
|
|
68
|
+
"Pool",
|
|
69
|
+
"PreparedStatement",
|
|
70
|
+
"Result",
|
|
71
|
+
"ScramClient",
|
|
72
|
+
"SpydAuthenticationError",
|
|
73
|
+
"SpydCancelledError",
|
|
74
|
+
"SpydConflictError",
|
|
75
|
+
"SpydConnectionError",
|
|
76
|
+
"SpydConstraintError",
|
|
77
|
+
"SpydError",
|
|
78
|
+
"SpydMultipleResultsError",
|
|
79
|
+
"SpydNoResultError",
|
|
80
|
+
"SpydPermissionError",
|
|
81
|
+
"SpydProtocolError",
|
|
82
|
+
"SpydQueryError",
|
|
83
|
+
"SpydResourceError",
|
|
84
|
+
"SpydSchemaError",
|
|
85
|
+
"SpydTimeoutError",
|
|
86
|
+
"SpydTypeError",
|
|
87
|
+
"SpydUnsupportedError",
|
|
88
|
+
"SpydValueError",
|
|
89
|
+
"Text",
|
|
90
|
+
"UInt",
|
|
91
|
+
"Vector",
|
|
92
|
+
"connect",
|
|
93
|
+
"crc32c",
|
|
94
|
+
"decode_value",
|
|
95
|
+
"encode_parameter",
|
|
96
|
+
"orm",
|
|
97
|
+
"parse_type",
|
|
98
|
+
]
|
spyddb/auth.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""SCRAM-SHA-256 client authentication.
|
|
2
|
+
|
|
3
|
+
This follows the server implementation in ``crates/spyd-server/src/scram.rs``
|
|
4
|
+
exactly: SHA-256, ``biws`` channel binding, the literal strings the client
|
|
5
|
+
transmits are the ones hashed, and the server signature must be verified.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import base64
|
|
11
|
+
import hashlib
|
|
12
|
+
import hmac
|
|
13
|
+
import os
|
|
14
|
+
from typing import Optional
|
|
15
|
+
|
|
16
|
+
from .errors import SpydAuthenticationError, SpydProtocolError
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _b64(data: bytes) -> str:
|
|
20
|
+
return base64.b64encode(data).decode("ascii")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _unb64(text: str) -> bytes:
|
|
24
|
+
try:
|
|
25
|
+
return base64.b64decode(text, validate=True)
|
|
26
|
+
except Exception as error: # noqa: BLE001 - surfaced as a protocol error
|
|
27
|
+
raise SpydProtocolError(f"the server sent invalid base64: {error}") from error
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _attribute(message: str, key: str) -> Optional[str]:
|
|
31
|
+
"""Find ``key=value`` in a comma-separated SCRAM message."""
|
|
32
|
+
for part in message.split(","):
|
|
33
|
+
if part.startswith(key + "="):
|
|
34
|
+
return part[len(key) + 1:]
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ScramClient:
|
|
39
|
+
"""One SCRAM exchange."""
|
|
40
|
+
|
|
41
|
+
def __init__(self, user: str, password: str) -> None:
|
|
42
|
+
if not user:
|
|
43
|
+
raise SpydAuthenticationError("SCRAM needs a user name")
|
|
44
|
+
self.user = user
|
|
45
|
+
self.password = password
|
|
46
|
+
self.client_first_bare = ""
|
|
47
|
+
self.client_final_without_proof = ""
|
|
48
|
+
self._server_key = b""
|
|
49
|
+
self._auth_message = b""
|
|
50
|
+
|
|
51
|
+
def start(self) -> str:
|
|
52
|
+
"""Return the ``client_first`` message."""
|
|
53
|
+
nonce = _b64(os.urandom(18))
|
|
54
|
+
self.client_first_bare = f"n={self.user},r={nonce}"
|
|
55
|
+
return self.client_first_bare
|
|
56
|
+
|
|
57
|
+
def finish(self, server_first: str) -> tuple[str, str]:
|
|
58
|
+
"""Compute ``(client_final, proof)`` from the server-first message."""
|
|
59
|
+
combined_nonce = _attribute(server_first, "r")
|
|
60
|
+
salt_text = _attribute(server_first, "s")
|
|
61
|
+
iterations_text = _attribute(server_first, "i")
|
|
62
|
+
if not combined_nonce or salt_text is None or iterations_text is None:
|
|
63
|
+
raise SpydProtocolError("the SCRAM challenge is incomplete")
|
|
64
|
+
if not combined_nonce.startswith(self._nonce()):
|
|
65
|
+
raise SpydProtocolError("the server changed the SCRAM nonce")
|
|
66
|
+
try:
|
|
67
|
+
iterations = int(iterations_text)
|
|
68
|
+
except ValueError as error:
|
|
69
|
+
raise SpydProtocolError("the SCRAM iteration count is not a number") from error
|
|
70
|
+
if iterations < 4096:
|
|
71
|
+
raise SpydProtocolError("the server offered a weak SCRAM iteration count")
|
|
72
|
+
|
|
73
|
+
salt = _unb64(salt_text)
|
|
74
|
+
salted = hashlib.pbkdf2_hmac(
|
|
75
|
+
"sha256", self.password.encode("utf-8"), salt, iterations, dklen=32
|
|
76
|
+
)
|
|
77
|
+
client_key = hmac.new(salted, b"Client Key", hashlib.sha256).digest()
|
|
78
|
+
stored_key = hashlib.sha256(client_key).digest()
|
|
79
|
+
server_key = hmac.new(salted, b"Server Key", hashlib.sha256).digest()
|
|
80
|
+
client_final = f"c=biws,r={combined_nonce}"
|
|
81
|
+
auth_message = (
|
|
82
|
+
f"{self.client_first_bare},{server_first},{client_final}".encode("utf-8")
|
|
83
|
+
)
|
|
84
|
+
signature = hmac.new(stored_key, auth_message, hashlib.sha256).digest()
|
|
85
|
+
proof = bytes(left ^ right for left, right in zip(client_key, signature))
|
|
86
|
+
|
|
87
|
+
self._server_key = server_key
|
|
88
|
+
self._auth_message = auth_message
|
|
89
|
+
self.client_final_without_proof = client_final
|
|
90
|
+
return client_final, _b64(proof)
|
|
91
|
+
|
|
92
|
+
def verify(self, server_final: str) -> None:
|
|
93
|
+
"""Verify the server signature and raise on a mismatch."""
|
|
94
|
+
if not server_final.startswith("v="):
|
|
95
|
+
raise SpydProtocolError("the server did not send a SCRAM signature")
|
|
96
|
+
expected = hmac.new(self._server_key, self._auth_message, hashlib.sha256).digest()
|
|
97
|
+
received = _unb64(server_final[2:])
|
|
98
|
+
if len(received) != len(expected) or not hmac.compare_digest(received, expected):
|
|
99
|
+
raise SpydAuthenticationError("the server signature does not match")
|
|
100
|
+
|
|
101
|
+
def _nonce(self) -> str:
|
|
102
|
+
marker = "r="
|
|
103
|
+
index = self.client_first_bare.find(marker)
|
|
104
|
+
if index < 0:
|
|
105
|
+
return ""
|
|
106
|
+
return self.client_first_bare[index + len(marker):]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
__all__ = ["ScramClient"]
|