Repository navigation
Conversation
…protection
- Add flow_agent/auth.py supporting Bearer and X-API-Key token authentication
- Protect image/video generation and balance endpoints with constant-time verification
- Maintain backward-compatible development mode when no keys are configured
- Add 'key {create,list,revoke,delete}' CLI subcommand in main.py
- Support FLOW_API_KEY environment variable as master key
- Add unit tests in tests/test_api_auth.py (100% pass)
- Update roadmap in engine-architecture-plan.md
…redentials Behaviour fixes across the API key surface: - Credentials are read from headers only. The ?api_key= query-string fallback is removed: it copied keys into access logs, browser history and Referer headers, and bought nothing, because media routes are not guarded. - Credentials are compared as UTF-8 bytes. secrets.compare_digest refuses str outside ASCII, so a non-ASCII key raised TypeError and answered 500 -- for a wrong key and for the correct one alike. Both now answer 401. - load_api_keys() no longer swallows every exception. A store that exists but cannot be read or parsed raises KeyStoreError, is_auth_enforced() reports True for it, and verify_api_key() answers 503. Such a store previously returned [], which switched enforcement off and let unauthenticated requests through -- the opposite of what the module is for. - create_api_key() refuses a duplicate alias, and revoke/delete refuse an alias shared by several entries instead of silently acting on all of them. - The module, the `python main.py key` subcommand and the tests are English; every other .md and .py in this repository already is. Tests: tests/test_api_auth.py grows from 8 to 17 cases, 17 passed on Python 3.11.9.
iceblue03
force-pushed
the
feat/api-key-authentication
branch
from
October 18, 2026 06:27
1f80bd4 to
c83b33e
Compare
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
Adds optional API key authentication to the FastAPI surface, a
keysubcommand tomanage keys from the terminal, and a test suite covering both. Enforcement is
off until a key exists, so an existing single-user deployment keeps working
with no configuration and no flag.
Motivation
The server exposes image and video generation over HTTP, and its default bind is
127.0.0.1. The moment it is started with--host 0.0.0.0— a container, a LANbox, a NAS — every route that spends Veo/Imagen credits is reachable by anyone
who can reach the port, with no credential and no audit trail. This adds that
credential, without breaking the local single-user workflow the README documents.
Detailed changes
flow_agent/auth.py(new). Key generation (secrets.token_urlsafe(32)),constant-time verification, an on-disk store written atomically via a temp file
and
os.replacewith a best-effort0600, andFLOW_API_KEYas an optionalenvironment master key.
flow_agent/api.py. Every credit-spending and account-reading route takesdependencies=[Depends(verify_api_key)]— 14 path operations.GET /,/healthand
/openapi.jsonstay public, so probes and the docs page keep working.main.py. Akeysubcommand group:create(name/alias, prints the keyonce),
list(state, alias, masked key, creation time),revoke(deactivate),delete(remove permanently). Both matchers accept the key string or the alias,and the output never echoes a key string back to the terminal — only its masked
form.
tests/test_api_auth.py. 17 cases: pass-through with no keys, 401 for amissing/invalid credential, both accepted headers, the master key, revocation,
public routes, and the failure modes listed below.
Verification
The two failures are in
tests/test_engine.py(
test_command_drops_arguments_that_were_not_given,test_command_keeps_falsy_values_that_are_not_absent) and are pre-existing:a pristine worktree at the branch's parent commit fails the same two. They are
untouched by this change.
Behaviour matrix, verified end-to-end through
TestClientagainst the fixedrevision:
Authorization: Bearer <key>X-API-Key: <key>?api_key=<key>FLOW_API_KEY, ASCII requestkey create --name <existing>key delete <alias shared by two>Design decisions
is_auth_enforced()islen(get_active_keys()) > 0;enforcement follows from key presence. Turning it off means revoking or
deleting every key (and unsetting
FLOW_API_KEY), which is the state a freshclone is already in.
KeyStoreError; requests answer503rather than falling back to noenforcement. Availability loss here is visible and fixable; an unauthorised
generation against someone's billing account is neither.
leak: it lands in access logs, shell history and
Refererheaders, and it isunnecessary here because media routes are not guarded.
Known gaps
No user-facing documentation yet.
README.md,docs/API_REFERENCE.mdanddocs/CLI_REFERENCE.mddo not mention thekeygroup or the auth contract —in particular that zero registered keys means no authentication, which an
operator binding
0.0.0.0needs to know. Worth a follow-up before this isadvertised as a security feature.
Media routes (
GET /v1/media/{id}) remain public. If they are ever guarded,a header-only rule needs rethinking for plain browser downloads, which cannot
set one.
--host 0.0.0.0with no keys registered is still an unauthenticated publicserver; the code cannot refuse to start, because that is also the documented
local default path.
Interacts with feat: add direct image stream preview and web dashboard for OpenAI image API #23 (
feat/openai-image-stream-preview). The dashboard thatPR adds generates through this same route:
flow_agent/api.py:877callsfetch('/v1/images/generations', ...)and that file carries noAuthorizationor
X-API-Keyheader anywhere, so registering a key makes the dashboard answer401 while the page itself stays public. That is enforcement working as
designed, but the dashboard then needs a way to authenticate, and a secret
inside page script is not one. The two PRs also conflict: both sit on
mainat7d7abdd, andgit merge-tree --write-tree origin/feat/openai-image-stream-preview origin/feat/api-key-authenticationreports
CONFLICT (content)inflow-agent/flow_agent/api.py, because feat: add direct image stream preview and web dashboard for OpenAI image API #23rewrites the same route decorators this PR adds
dependencies=[Depends(verify_api_key)]to. Whichever of the two merges first,the other needs that file rebased by hand.
AI disclosure
The code, the English rewrite and the test suite were produced with AI
assistance; the author reviewed and owns the result.