Skip to content

feat(access-control): add GET /me with app hook - #687

Merged
uniqueg merged 7 commits into
devfrom
feat/610-get-me
Nov 4, 2026
Merged

uniqueg merged 7 commits into
devfrom
feat/610-get-me

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #610.

With security.access_control.me_enabled, FOCA serves GET /me (path: me_path) for any caller with a valid token. It returns subject, client, groups, admin and admin_available, in the snake_case of the permissions API. me_hook names a function adding fields of the app, which can't replace these. Off by default, so apps with their own endpoint aren't affected.

Decisions, as the issue leaves them open:

  • Requests without a token get 401, as in cloud-registry.
  • Hook fields never replace FOCA's fields, as clients rely on them.
  • The endpoint isn't remembered as an API of the app (_register_api): the helpers for paths stay with the app's API in hooks, and the app's rules aren't checked against it, as policies aren't enforced on it.
  • It needs the base path /, as Connexion names blueprints after base paths: apps with an API at / get a clear error, and declare the endpoint in that API instead.

Docs: docs/access-control.md (The current caller, with how cloud-registry's response maps to it), docs/configuration/access-control.md, docs/reference.md.

How it was verified

  • Local checks on Python 3.13 with own MongoDB: ruff, mypy, docs, pytest, 100% coverage.
  • Tests: anonymous, user, admin from groups and rules, opt-in, service client, no identity, hook fields, failing hooks, moved path, registration with access control, and rules with {base_path}/*.
  • Reviewers: both, two rounds. Round 1 found the endpoint counted as permissions API in rule checks, and path helpers pointing to it in hooks; both fixed by not remembering it as an API. Round 2 had low findings only.
  • CI: see below.

Declined review findings

  • admin_available counts only the opt-in role admin, not opt-in roles that rules link to it: same as cloud-registry; documented.
  • No warning if me_path takes over the redirect of a route with a trailing slash (/api/{id}/): that route still works at its own path.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Provide an opt-in current-caller endpoint that gives clients consistent identity and access-control information while supporting application-specific fields.

New Features:

  • Add an optional authenticated GET /me endpoint reporting the caller’s identity, groups, client, and admin status.
  • Allow applications to configure the endpoint path and extend its response with application-provided fields through a hook.

Enhancements:

  • Validate endpoint configuration, detect route conflicts, and support integration with existing access-control registration and token handling.

Documentation:

  • Document the current-caller endpoint, configuration options, response fields, hook behavior, and API reference.

Tests:

  • Add comprehensive coverage for authentication, identities, admin availability, hooks, custom paths, registration, conflicts, and configuration validation.

uniqueg and others added 4 commits November 4, 2026 09:27
Clients need to know who the caller is and what they may do. With
me_enabled, FOCA serves the subject, client, groups and admin status of
callers with a valid token, plus fields from an app's function
(me_hook). Off by default, so apps with their own endpoint are unaffected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
From review: helpers for paths in hooks stay with the app's APIs, rules
of the app aren't checked against the endpoint, hooks are checked also
without the endpoint, routes it shadows are logged, and callers without
an identity aren't told that being an admin is available.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Without access control, me_hook and me_path have no effect, so they are
refused like me_enabled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg added this to the v0.21.0 milestone Nov 4, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR adds an opt-in, authenticated GET /me endpoint that reports the current caller and authorization state, supports application-provided hook fields, validates and registers the endpoint independently of the permissions API, and documents and tests its configuration, OpenAPI contract, conflict handling, and security behavior.

Sequence diagram for the authenticated GET /me request

sequenceDiagram
    actor Caller
    participant FOCA as FOCA /me endpoint
    participant Hook as Application me_hook
    participant Access as Access-control helpers

    Caller->>FOCA: GET me_path with token
    FOCA->>Access: get_current_subject()
    FOCA->>Access: get_current_client()
    FOCA->>Access: get_current_groups()
    FOCA->>Access: is_admin()
    FOCA->>Access: get_available_opt_in_roles()
    opt me_hook configured
        FOCA->>Hook: me_hook()
        Hook-->>FOCA: Application fields
    end
    FOCA-->>Caller: subject, client, groups, admin, admin_available, hook fields
Loading

Flow diagram for optional GET /me registration

flowchart TD
    Config["me_enabled configured"] --> Enabled{me_enabled?}
    Enabled -- No --> Skip["Do not register endpoint"]
    Enabled -- Yes --> Base{"API has base path /?"}
    Base -- Yes --> Error["Raise configuration error"]
    Base -- No --> Conflict{"me_path conflicts with route?"}
    Conflict -- Yes --> Error2["Raise route conflict error"]
    Conflict -- No --> Hook{"me_hook configured?"}
    Hook -- Yes --> Import["import_me_hook()"]
    Hook -- No --> Register["Register GET me_path with OpenAPI spec"]
    Import --> Register
Loading

File-Level Changes

Change Details Files
Adds an opt-in authenticated current-caller endpoint with stable FOCA identity and authorization fields plus application-extensible response data.
  • Introduces GET /me registration controlled by me_enabled, with configurable path and OpenAPI specification.
  • Returns subject, service client, groups, admin status, and admin availability using existing access-control helpers.
  • Loads and validates an application hook at startup, invokes it per request, and prevents hook fields from overriding FOCA-owned fields.
  • Requires valid authentication, handles anonymous and hook failures through standard error responses, and respects invalid-token configuration.
foca/security/access_control/me.py
foca/security/access_control/register_access_control.py
foca/security/access_control/api/me-specs.yaml
foca/security/access_control/constants.py
Extends access-control configuration and validation for the new endpoint while preserving its separation from the permissions API and policy enforcement.
  • Adds me_enabled, me_path, and me_hook configuration with path validation and dependency checks on enabled access control.
  • Detects root-base-path conflicts and route collisions or precedence issues before registration.
  • Keeps the endpoint out of remembered APIs and policy path checks so app API path helpers and rule semantics remain unchanged.
foca/models/config.py
foca/security/access_control/register_access_control.py
Documents the endpoint contract, configuration, hook behavior, identity semantics, and deployment constraints.
  • Describes response fields, authentication behavior, admin availability, hook rules, and cloud-registry field mapping.
  • Publishes the endpoint module in the reference documentation and adds configuration examples.
docs/access-control.md
docs/configuration/access-control.md
docs/reference.md
Adds comprehensive automated coverage for endpoint behavior, registration, configuration, conflicts, and integration paths.
  • Covers anonymous, user, admin, opt-in, service-client, and identity-less tokens.
  • Tests hooks, override protection, request context, failures, custom paths, route conflicts, root APIs, access-control registration, policies, invalid tokens, and error media types.
tests/security/access_control/test_me.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a61c742) to head (78c2a8c).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #687   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           61        62    +1     
  Lines         8071      8130   +59     
=========================================
+ Hits          8071      8130   +59     
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 4, 2026 08:56

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="foca/security/access_control/register_access_control.py" line_range="193" />
<code_context>
         app=cnx_app, access_control_config=access_control_config
     )
     _check_routes_enforced(cnx_app.app)
+    cnx_app = register_me_endpoint(
+        app=cnx_app, access_control_config=access_control_config
+    )
</code_context>
<issue_to_address>
**Failed startup leaves partial registration**

When `me_enabled` is set and the configured path conflicts with a route, or an API has the root base path, and the caller retries using the same Connexion app after the registration error, a route conflict or root-base-path check in `register_me_endpoint()` raises only after `register_access_control()` has registered the Mongo collection and permission API; retrying registration on the same Connexion app encounters leftover routes and configuration instead of starting cleanly.

Validate the `/me` configuration and route constraints before registering the permission API or mutating database configuration, or roll back those registrations if `/me` registration fails.
</issue_to_address>

### Comment 2
<location path="foca/security/access_control/register_access_control.py" line_range="705" />
<code_context>
+    Raises:
+        ValueError: It is the path of a route of the app.
+    """
+    if any(rule.rule == path for rule in app.url_map.iter_rules()):
+        raise ValueError(
+            f"'security.access_control.me_path' is taken by another route: '{path}'"
</code_context>
<issue_to_address>
**POST routes block caller endpoint**

When an application already has a POST-only route at the configured `me_path`, the method-blind path check raises `ValueError` before checking whether the existing route handles `GET`, so a valid `GET /me` cannot coexist with an unrelated `POST` route at the configured path.

Reject exact-path routes only when their methods conflict with GET (including Flask's automatic HEAD/OPTIONS behavior).
</issue_to_address>

Linked issues

#610 feat(access-control): add GET /me with app hook · 3 of 4 met · 1 to check by hand

# Requirement Status Notes
1 Provide an optional GET /me returning caller information and app hook fields. ✅ Met The endpoint is opt-in and returns subject, groups, admin status, and hook fields; it also reports client and admin_available.
2 Test anonymous, user, admin, service client, and hook-fields cases. ✅ Met The added tests cover each listed case.
3 Achieve 100% coverage. 👀 Check by hand (runtime) Confirm that the reported test run reaches 100% coverage; the diff alone does not establish executed coverage.
4 Document the endpoint on the access-control page, including how cloud-registry's response maps to it. ✅ Met The access-control documentation describes the endpoint and includes the cloud-registry-to-FOCA field mapping.

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and any caller with a valid token can reach this endpoint without access-control policies, and its response reveals caller identity, group claims, and admin status derived by FOCA. Reverting stops future responses, but information already disclosed cannot be taken back.

Blocking findings: foca/security/access_control/register_access_control.py:193, foca/security/access_control/register_access_control.py:705


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@uniqueg
uniqueg merged commit aa06849 into dev Nov 4, 2026
59 checks passed
@uniqueg
uniqueg deleted the feat/610-get-me branch November 4, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(access-control): add GET /me with app hook

1 participant