Repository navigation
http-auth done - #36
Open
NEMEZIZ1234 wants to merge 1 commit into
Open
NEMEZIZ1234 wants to merge 1 commit into
NEMEZIZ1234 wants to merge 1 commit into
Conversation
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 HTTP API-key auth with per-key scope prefixes (multi-tenant): requests are rejected with 401 when auth is configured and a valid key isn't provided, and each key is restricted to its configured scope prefix. Enforced on both the MCP streamable-HTTP transport and the REST API.
Related issue
Closes #10
Changes
src/mnema/auth.py(new) βAuthContext/Authenticator,ApiKeyAuthMiddleware(ASGI, for the MCP transport),parse_api_keys, andrequest_auth/set_auth/current_authcontext-var helpers.config.pyβ newapi_keysfield (key1=user:alice:*,key2β bare key = full access; empty = auth disabled), parsed viaNoDecode+mode="before"validator (pydantic-settings β₯ 2.2).api/app.pyβget_authFastAPI dependency (401 when missing/invalid),ForbiddenErrorβ 403 handler,/memorieslist clamped + denied for out-of-prefix scopes.server.pyβ_run_httpwrapsstreamable_http_app()behindApiKeyAuthMiddleware;FastMCP(host=...)so the DNS-rebinding allowlist matches remote hosts.service.pyβ_enforce_scope/_tenant_scope/_outside_prefix: writes β 403, reads clamp to the key's prefix, by-id get/update/forget β 404 on out-of-prefix ids,list_scopes/statsfiltered, decay/reembed/summarize enforced.errors.pyβ addedForbiddenError.pyproject.tomlβpydantic-settings>=2.2; version bumped to 0.5.0.test_auth.py,test_http_auth.py(real MCP streamable client); extendedtest_api.py,test_config.py.README.md,docs/deployment.md,packages/mnema-extension/README.md(auth section); CHANGELOG[Unreleased]entry.Checklist
ruff check .is cleanpytestpasses (with the relevant[extra]installed if you touched a backend)README.md,SKILL.md,docs/) if user-facing behavior changed__init__.py::make_*), added an[extra]inpyproject.toml, and added a test classNotes for reviewer
MNEMA_API_KEYSset) β 401 otherwise β and fully disabled when unset (backwards compatible).key=scope:prefix,key2(comma-separated);pydantic-settingsJSON-decodes complex env values before validators, so the field usesAnnotated[..., NoDecode](requires β₯ 2.2).MemoryNotFoundError) so tenants can't probe each other; list/search read paths clamp to the key's prefix when no scope is given.Depends; the context var needs no reset there (per-request ASGI task). The MCP transport needs a middleware (reset per stream) per the issue spec.FastMCP(host=cfg.http_host)was required so the loopback DNS-rebinding allowlist matches the clients served; a non-loopback host disables rebinding protection, which auth now covers.