Repository navigation
feat: scoped API tokens, token rotation UI, per-org rate limits, PII redaction - #756
Merged
Ayinkx merged 6 commits intoNov 23, 2026
Conversation
Origin/main failed 'ruff check', 'black --check' and 'flask db heads': two Phase 11 branches (idempotency keys and IP allowlists) landed without a merge revision, and four recently merged files drifted from the configured formatters. Apply ruff/black fixes only; no behaviour change.
…yinkxLab#446) The route tests built rows around a frozen 2026-10-20 anchor while the dashboard queries the real clock, so the owner-scoped totals, empty state and filter assertions expired after the window moved on. Anchor the fixture to 'now' and derive the expected daily labels instead of hard-coding them.
|
@awwaumuhammed-star is attempting to deploy a commit to the Ayinkx Team on Vercel. A member of the Team first needs to authorize it. |
|
@awwaumuhammed-star Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
|
||
| def hash_secret(secret: str) -> str: | ||
| """Return the SHA-256 hex digest stored for ``secret``.""" | ||
| return hashlib.sha256(secret.encode("utf-8")).hexdigest() |
|
|
||
| def redact_text(value: str) -> str: | ||
| """Replace email addresses embedded in ``value`` with a bounded marker.""" | ||
| return EMAIL_PATTERN.sub(EMAIL_MARKER, value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Scoped personal API tokens with rotation and revocation, per-organization rate limits, and central PII redaction for logs and exports.
This branch also carries two preparatory commits that were needed to make the repo gate and migrations usable at all; they are listed separately at the end so they can be split out.
Closes #613
Closes #616
Closes #617
Closes #621
#616 — scoped API tokens
ApiTokenmodel (api_tokenstable, migrationc1d2e3f4a5b6): label, display prefix, SHA-256 of the secret, scopes, created/last-used/expiry/revoked timestamps. The secret itself is never stored, andto_dictis metadata-only.app/services/api_tokens.pyprovides create/rotate/revoke/authenticate plusnormalize_scopes,required_scopeandtoken_allows. Scopes are rankedread<write<admin: safe methods need read, POST/PUT/PATCH need write, DELETE needs admin.before_requesthook that sets the Flask-Login identity for the request without touching the session. Tokens are accepted on JSON API paths only; a token on an HTML route returns the structuredunauthorizederror.ApiTokenCSRFProtect; session-authenticated requests keep the normal check.docs/api-tokens.md,docs/api-v1.md, README.#617 — rotation and revocation UI
app/tokens/blueprint with a management page and JSON endpoints: list (metadata only), create, rotate, revoke.api_token_created,api_token_rotated,api_token_revoked.docs/api-token-management.md.#621 — per-organization rate limits
app/services/ratelimit.pygainsapi_quota(family): per-organization and per-token budgets per endpoint family, standardX-RateLimit-*headers andRetry-After, and the catalograte_limitederror body.RATE_LIMIT_MAX_KEYS(default 10,000) with oldest-key eviction under the existing lock./api/v1resource; legacyper_user_limit/ai_limitdenials now return the same catalog body instead of ad-hoc JSON.docs/api-rate-limits.md.#613 — PII redaction for logs and exports
app/services/classification.pygainsREDACT_FIELDS(derived from everypiientry inFIELD_LEVELS), recursive redaction across dicts, lists, tuples and sets, and email scrubbing inside otherwise allowed strings viaredact_text/EMAIL_MARKER.redactis the generic entry point;redact_for_logkeeps its name for existing callers.redact(..., max_level=INTERNAL); SIEM forwarding and structured chat audit logs already used the same helper.docs/data-classification.md.Verification
Test run covers the four new test modules plus classification, audit export, API v1, API quota, rate-limit endpoints, API keys, the error catalog and docs links.
Limitations
Authorizationas a subject key); without a session it is401.create_all; no PostgreSQL upgrade/downgrade was run.Preparatory commits riding along
chore: restore green lint, format and single alembic head— adds an empty merge revisiona0b1c2d3e4f6becausemaincurrently has two Alembic heads (idempotency keys and IP allowlists), which breaksflask db upgrade; plus ruff/black-only fixes inapp/services/idempotency.pyand three test files.test(billing): keep per-user usage dashboard checks date-independent (#446)—tests/test_usage_dashboards.pyanchored rows to a frozen date while the route queries the real clock, so the tests expired; now derived fromdatetime.now(UTC).Both are independently reviewable and can be split into their own PR if you prefer.