Skip to content

fix(auth): reuse oauth clients during token exchange - #4918

Merged
shepilov merged 1 commit into
masterfrom
fix/reuse-token-exchange-clients
Oct 2, 2026
Merged

shepilov merged 1 commit into
masterfrom
fix/reuse-token-exchange-clients

Conversation

@shepilov

@shepilov shepilov commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Address Unlimited growth of oauth client when using token exchange #4916 by reusing OAuth clients for the same OIDC provider/context, instance, session, and application in cozy-stack, including across allowed origins.
  • Serialize concurrent exchanges, preserve reused clients on failures, and cover credential reuse across origins, scope isolation, revocation, and storage failures with regression tests. Document that existing duplicate clients remain intact.
  • Reuse LockOAuthClient for token exchange and propagate lock acquisition errors in all callers, with a regression test for a closed Redis connection.
  • Validation with isolated CouchDB: go test -count=1 -p 1 -timeout 10m ./web/auth ./web/settings ./web/oidc ./model/oauth ./model/oidc/binding passed all auth/OIDC packages; the settings RabbitMQ integration test failed because Docker could not bind a host port. The remaining settings tests passed with go test -count=1 -v -skip '^TestForceInstanceDeletion$/^PublishDeletionMessageWithRealRabbitMQ$' ./web/settings.
  • Earlier make unit-tests run: 80 packages passed; the unchanged RabbitMQ connection tests failed because their Docker containers exited during startup or could not bind a host port.

@shepilov
shepilov marked this pull request as draft September 14, 2026 14:02
@shepilov
shepilov force-pushed the fix/reuse-token-exchange-clients branch from 930c17d to 5b40284 Compare September 14, 2026 15:52
@shepilov
shepilov force-pushed the fix/reuse-token-exchange-clients branch 4 times, most recently from 45a7bc2 to 064cd22 Compare September 30, 2026 13:51
@shepilov
shepilov marked this pull request as ready for review September 30, 2026 13:51
Comment thread docs/auth.md

Reuse relies on the existing OIDC session bindings. If a binding is lost or
expires (after 31 days without renewal in Redis), another client can be created.
Existing duplicate clients are not automatically deleted by token exchange.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we have a way to clean up old clients? I know that today we have a daily cron job to remove pending oauth client, but I don't know if we can make a distinction between a client created by token exchange and not usable anymore and other ones

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unfortunately, no, only backchannel logout

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@shepilov
shepilov force-pushed the fix/reuse-token-exchange-clients branch from 064cd22 to 86ec13d Compare October 1, 2026 09:21
Comment thread web/auth/token_exchange.go
Comment thread web/auth/token_exchange.go
Comment thread web/auth/token_exchange.go
Repeated token exchanges within one OIDC session created a new OAuth
client every time. Reuse the existing client when the OIDC context,
instance, session (sid) and software_id match, including across allowed
origins, so a session keeps a single client per application.

A session-scoped lock serialises concurrent exchanges for the same
session so they reuse one client instead of racing to create duplicates;
the per-client lock still guards binding against refresh/revoke/update.
Reused clients are re-read and re-validated under their lock, only
newly-created clients are deleted on a binding failure, and the
registration token is regenerated for reused clients.

LockOAuthClient now returns lock-acquisition errors and every caller
handles them instead of proceeding without the lock.

Document the reuse semantics and their limits in docs/auth.md.

Refs #4916
@shepilov
shepilov force-pushed the fix/reuse-token-exchange-clients branch from 86ec13d to 958e364 Compare October 1, 2026 16:48
@shepilov
shepilov merged commit f95446b into master Oct 2, 2026
4 checks passed
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.

3 participants