Commit 121111d
committed
feat(auth): auto-refresh OIDC tokens when session is empty
When the OIDC JWT strategy finds no usable id_token (missing from both the
session and the openid_id_token cookie) but a refreshToken cookie exists,
transparently call the IdP's refresh-token endpoint, repopulate the session,
and continue the request. This makes in-memory session loss invisible to the
user — primarily addressing the deployment-restart scenario where the
MemoryStore is wiped while the browser still holds a valid Bearer token,
which previously caused empty 'Authorization: Bearer ' headers to reach
downstream services like LiteLLM.
If refresh is impossible (no refresh cookie, IdP rejects, etc.), the strategy
fails closed with 401 so the SPA's existing axios interceptor handles it.
- Add refreshOpenIDTokensFromCookie(req, res, userId) helper in AuthService.js
with module-private dedup map keyed on the refresh-token value, so
concurrent requests for the same credential share one IdP round-trip.
- Wire helper into openIdJwtStrategy.js: detect missing id_token before
populating user.federatedTokens, refresh if possible, fail closed otherwise.
- Lazy-require getOpenIdConfig to avoid pulling undici/openid-client into
unrelated AuthService consumers' module-init.
- Tests: 11 new helper tests (no-cookie short-circuit, IdP success/failure
paths, getOpenIdConfig throws, missing res, concurrent dedup, sequential
release, failure release) + 4 new strategy tests (refresh on empty session,
401 on helper failure, no refresh when not needed, no refresh cookie).
- Updates the existing 'should set id_token to undefined' strategy test to
reflect the new fail-closed behavior.1 parent d3c8add commit 121111d
4 files changed
Lines changed: 581 additions & 7 deletions
File tree
- api
- server/services
- strategies
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
| |||
714 | 716 | | |
715 | 717 | | |
716 | 718 | | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
717 | 803 | | |
718 | 804 | | |
719 | 805 | | |
| |||
783 | 869 | | |
784 | 870 | | |
785 | 871 | | |
| 872 | + | |
786 | 873 | | |
0 commit comments