Skip to content

feat: add request and claims accessors #634

Closed
5 of 5 issues completed
Closed

Description

@uniqueg

Controllers in FOCA apps get their inputs in two ways:

  • from Connexion's injected arguments: path, query and body parameters, user, token_info;
  • from Flask: request.json, request.path, request.url, request.script_root, request.content_length, request.form/files, request_ctx.connexion_context.

FOCA's own code does the same: claims in enforcement.get_current_claims, path and method in is_permitted, script_root in utils/urls.py, the request line in utils/logging.py.

The ASGI release drops Flask. A scratch comparison of Connexion 2.15.1 and 3.3.0 showed:

  • Injected arguments behave the same on both (path, query with defaults, body, user, token_info, **kwargs).
  • Header parameters are injected in neither.
  • connexion.request is Flask's request on 2.x and a Starlette-based request on 3.x. The 3.x request has no .path and no .script_root.
  • connexion.context is a proxy on 2.x and a module on 3.x.
  • Paths differ: Starlette's scope["path"] includes the root path; Flask's request.path doesn't. Policies and owners are keyed by the path, so this is security-relevant.

So apps need one FOCA accessor that means the same on both, plus the rule to take everything else from injected arguments.

Do:

  • foca.context.current_request(), a read-only snapshot:

    • method;
    • path: below the root path, the same as Flask's request.path today;
    • root_path, url;
    • headers: case-insensitive, read-only;
    • content_length;
    • client_address;
    • route: the path template from the route table (refactor: check routes from specs, not url_map #632), plus path_params.

    It raises a clear error outside a request.

  • Claims: foca.security.auth.current_token_info() and current_claims(). The existing get_current_claims() uses them; they are empty when no token was validated.

  • Switch FOCA's internals to these accessors: enforcement, ownership, utils/urls.py, utils/logging.py. Read request bodies only via injected arguments. The access-control server's request.get_json() becomes a body argument.

  • Config: get_config()/get_custom_config() are the only supported ways to read the config in apps. current_app.config.foca and get_app_config(flask_app) are deprecated (DeprecationWarning where FOCA can emit one).

  • Responses: document (body, status, headers) tuples and NoContent as the portable return values, instead of flask.Response, make_response and send_file. They are verified to give the same responses on Connexion 2 and 3.

  • The 0.22.0 implementation reads from Flask and Connexion 2. Only its body changes in the ASGI release.

Acceptance criteria

  • current_request(), current_token_info() and current_claims() are public, typed, documented, and tested with a root path, a base path and headers.
  • foca/ reads no request data from flask.request, request_ctx or connexion.request outside the accessor module.
  • Policies and owners match exactly as before (existing access-control and ownership tests pass unchanged).
  • Docs ("Writing controllers"): inputs via injected arguments, everything else via the accessors, portable return values, config via get_config().

Decided

  • Names as proposed: foca.context.current_request(), foca.security.auth.current_claims() and current_token_info().
  • The request accessor is required in 0.22 (not optional or deferred).
  • Access control stays a resolver wrapper; this issue only changes where it reads the request from.

foca.context already exists with request_id() if the request ID issue is done first. Prepares #188.

Sub-issues

Sub-issues

5 of 5 Issues completed
Loading 5 sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: hightype: featureworkload: days

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions