Skip to content

refactor: allow auth_methods on route configurations#20096

Open
miketheman wants to merge 8 commits into
pypi:mainfrom
miketheman:miketheman/refactor-security-policies
Open

refactor: allow auth_methods on route configurations#20096
miketheman wants to merge 8 commits into
pypi:mainfrom
miketheman:miketheman/refactor-security-policies

Conversation

@miketheman
Copy link
Copy Markdown
Member

@miketheman miketheman commented May 22, 2026

See #13854 for historical approach to this idea, but this approach uses route predicates instead.

auth_methods is a route predicate, not a view predicate.

The security policies read this in their identity() methods (SessionSecurityPolicy and BasicAuthSecurityPolicy).
identity() runs at authentication time, right after Pyramid matches the route.
The matched route is sitting there to inspect via request.matched_route.predicates.
The view isn't - it hasn't been resolved yet, and there's no request attribute that exposes view predicates anyway. Put this on a view predicate and the policy is blind to it at the one moment it needs the answer.

It's also where the concept belongs. Which credentials an endpoint accepts is a property of the route, not of one view callable. A route can fan out to several views (different methods, accept headers), and all of them should answer to the same auth contract - set once, where the route is defined. This is different than permissions, which may vary based on the HTTP verbs (GET vs POST) - so they can be combined without having to recall the auth_method on each view_config (and it's "too late" anyhow).

The predicate is storage-only: __call__ always returns True,
so it never influences routing or view selection.
We already do this with the domain route predicate,
which is the same kind of thing: route-level metadata that infrastructure reads.

Fixes #7266

As a precursor to refactoring more permissions logic,
extract from inline into a lookup table.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Create a way for routes to declare which authentication methods they
expect to receive.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Declare the authentication methods each route accepts:
- basic-auth and macaroon for file uploads
- macaroon for the danger-api endpoints

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Replace the hardcoded route-name checks in the session
and basic-auth identity() methods with a lookup against
the route's auth_methods predicate,
via a new auth_methods_for_route() helper.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Reserve an AuthenticationMethod for the dedicated API auth surface
that will replace danger-api's macaroon abuse.
No policy implements it yet.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@miketheman miketheman requested a review from a team as a code owner May 22, 2026 16:24
@miketheman miketheman added security Security-related issues and pull requests core-team labels May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-team security Security-related issues and pull requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor authentication mechanisms

1 participant