Skip to content

fix(auth): preserve API-key user across RequireAuth re-dispatch#8913

Open
lrf-nitro wants to merge 1 commit into
apache:mainfrom
AkerBP:fix/openapi-key-auth-401-requireauth
Open

fix(auth): preserve API-key user across RequireAuth re-dispatch#8913
lrf-nitro wants to merge 1 commit into
apache:mainfrom
AkerBP:fix/openapi-key-auth-401-requireauth

Conversation

@lrf-nitro

Copy link
Copy Markdown
Contributor

Open-API (/rest) requests authenticated with a valid API key were rejected with 401 "unauthorized" whenever AUTH_ENABLED is on (the default - unless explicitly disabled via env var). The API-key middleware authenticates the request and sets common.USER via c.Set, then re-dispatches the rewritten path through router.HandleContext. gin's HandleContext calls Context.reset(), which sets c.Keys = nil and destroys the user, so the terminal RequireAuth gate sees no user and 401s.

Carry the authenticated user on the request's context.Context, which survives HandleContext (only gin Keys are reset). GetUser now falls back to the request context when gin Keys are empty. This fixes every /rest endpoint (webhooks, push, etc.) at once without disabling auth.

Add regression coverage:

  • shared: user set via SetUserOnRequest survives a HandleContext re-dispatch while gin Keys do not.
  • api: the full RestAuthentication -> OIDCAuthentication -> RequireAuth chain with AUTH_ENABLED=true returns 200 for a valid key, 401 for missing token, 403 for an invalid key, and 401 for a userless protected route.

⚠️ Pre Checklist

Please complete ALL items in this checklist, and remove before submitting

  • I have read through the Contributing Documentation.
  • I have added relevant tests.
  • I have added relevant documentation.
  • I will add labels to the PR, such as pr-type/bug-fix, pr-type/feature-development, etc.

Open-API (/rest) requests authenticated with a valid API key were rejected
with 401 "unauthorized" whenever AUTH_ENABLED is on (the default). The
API-key middleware authenticates the request and sets common.USER via
c.Set, then re-dispatches the rewritten path through router.HandleContext.
gin's HandleContext calls Context.reset(), which sets c.Keys = nil and
destroys the user, so the terminal RequireAuth gate sees no user and 401s.

Carry the authenticated user on the request's context.Context, which
survives HandleContext (only gin Keys are reset). GetUser now falls back to
the request context when gin Keys are empty. This fixes every /rest endpoint
(webhooks, push, etc.) at once without disabling auth.

Add regression coverage:
- shared: user set via SetUserOnRequest survives a HandleContext re-dispatch
  while gin Keys do not.
- api: the full RestAuthentication -> OIDCAuthentication -> RequireAuth chain
  with AUTH_ENABLED=true returns 200 for a valid key, 401 for missing token,
  403 for an invalid key, and 401 for a userless protected route.
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.

1 participant