Commit 8dc8285
authored
feat(types): enhance crypto column types and add TOTP/OTP storage (#758)
## Summary
Enhance the at-rest cryptographic column types and add two new types —
an encrypted TOTP shared-secret type and a hashed, self-expiring
one-time-code type.
## What changed
### `EncryptedString` / PGCrypto
- **Corrected `PGCryptoBackend`** to run encryption/decryption
server-side via SQLAlchemy `bind_expression`/`column_expression`: writes
wrap as `armor(pgp_sym_encrypt(:v, key))`, reads as
`pgp_sym_decrypt(dearmor(col), key)`. ASCII-armored ciphertext stays in
a `String` column, so the column type is unchanged. **Verified
interchangeable with `FernetBackend`** on PostgreSQL — same API, same
round-trip for value/None/empty/unicode, ciphertext at rest.
- Wired `cryptography` through the optionals facade
(`CRYPTOGRAPHY_INSTALLED`); `FernetBackend` raises
`MissingDependencyError` when it's absent. Importing
`advanced_alchemy.types` never eagerly imports the optional crypto
libraries — guards fire at construction time.
- **Deprecated the random default key**: constructing without an
explicit `key` now emits a `DeprecationWarning` (the random default
changes per process restart → undecryptable rows). Non-breaking.
- Cache the derived cipher for static keys; callable keys still
re-resolve.
- `__repr__` renders `type(self).__name__` so `EncryptedText`
reconstructs as `EncryptedText` (not `EncryptedString`) in autogenerated
migrations.
### `PasswordHash`
- Added `needs_rehash` to all three backends and
`HashedPassword.verify_and_update` for transparent rehash-on-login.
- Facade guards for argon2/passlib/pwdlib; default `length` 128 → 255;
narrowed `Argon2Hasher.verify` (no bare `except`).
### New types
- **`TOTPSecret`** — base32 TOTP secret encrypted at rest, returns a
pyotp-backed `TOTPProvider` (`now`/`verify`/`provisioning_uri`);
`generate_totp_secret` helper. `key` is required (no deprecated
default).
- **`OneTimeCode`** — a transient code hashed in a **JSON column that
also tracks expiry, single-use redemption, and wrong-guess lockout**, so
the whole lifecycle lives in one column:
- `OneTimeCode(backend, ttl_seconds=None, max_attempts=3)` stores
`{hash, expires_at, used_at, attempts}`.
- `HashedOneTimeCode` exposes `is_expired`/`is_used`/`is_locked`;
`verify` succeeds only while the code is redeemable; `redeem(code)`
verifies and returns the updated value to persist (marks used on
success, records a failed attempt otherwise).
- Single-use is always enforced; `max_attempts` (default 3) locks after
that many wrong guesses; `ttl_seconds` expires it. State persists by
assigning the redeemed value back and committing.
- `generate_one_time_code(length=6, digits_only=True)` helper.
- Both new types are registered in the Alembic mako templates and
reconstruct faithfully.
### Docs & packaging
- New `cryptography` and `pyotp` optional-dependency extras.
- Usage docs for key management, PGCrypto requirements, password
hashing, TOTP, and the one-time-code redeem lifecycle; README feature
entries; a single changelog entry in the `1.11.0` block. All new docs
examples execute under Sybil.
## Backward compatibility
- No change to Fernet ciphertext format or key derivation.
- The default-key change is non-breaking (warning only).
- `compare_value`/`compare_expression` retained (documented as an
advanced extension point), not removed.1 parent 5e69914 commit 8dc8285
28 files changed
Lines changed: 2007 additions & 389 deletions
File tree
- advanced_alchemy
- alembic/templates
- asyncio
- sync
- types
- password_hash
- docs
- usage
- frameworks
- modeling
- tests
- integration
- unit/test_types
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| 36 | + | |
| 37 | + | |
34 | 38 | | |
| 39 | + | |
35 | 40 | | |
36 | 41 | | |
37 | 42 | | |
| |||
375 | 380 | | |
376 | 381 | | |
377 | 382 | | |
| 383 | + | |
378 | 384 | | |
379 | 385 | | |
380 | 386 | | |
| 387 | + | |
381 | 388 | | |
382 | 389 | | |
383 | 390 | | |
384 | 391 | | |
| 392 | + | |
| 393 | + | |
385 | 394 | | |
| 395 | + | |
386 | 396 | | |
387 | 397 | | |
388 | 398 | | |
| |||
Lines changed: 8 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | 21 | | |
31 | 22 | | |
32 | 23 | | |
33 | 24 | | |
34 | | - | |
| 25 | + | |
35 | 26 | | |
36 | 27 | | |
37 | 28 | | |
| |||
47 | 38 | | |
48 | 39 | | |
49 | 40 | | |
| 41 | + | |
| 42 | + | |
50 | 43 | | |
51 | 44 | | |
52 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | 21 | | |
31 | 22 | | |
32 | 23 | | |
33 | 24 | | |
34 | | - | |
| 25 | + | |
35 | 26 | | |
36 | 27 | | |
37 | 28 | | |
| |||
47 | 38 | | |
48 | 39 | | |
49 | 40 | | |
| 41 | + | |
| 42 | + | |
50 | 43 | | |
51 | 44 | | |
52 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
26 | 32 | | |
27 | 33 | | |
28 | 34 | | |
| |||
39 | 45 | | |
40 | 46 | | |
41 | 47 | | |
| 48 | + | |
42 | 49 | | |
43 | 50 | | |
44 | 51 | | |
| 52 | + | |
45 | 53 | | |
46 | 54 | | |
47 | 55 | | |
48 | 56 | | |
49 | 57 | | |
| 58 | + | |
| 59 | + | |
50 | 60 | | |
51 | 61 | | |
52 | 62 | | |
| 63 | + | |
| 64 | + | |
53 | 65 | | |
54 | 66 | | |
| 67 | + | |
55 | 68 | | |
0 commit comments