Skip to content

🔩 refactor: Share OpenID Issuer Matching and Cache Expiry Helpers - #18434

Open
AtefBellaaj wants to merge 1 commit into
devfrom
atef/ai-2415-bcl-01-issuer-cache
Open

AtefBellaaj wants to merge 1 commit into
devfrom
atef/ai-2415-bcl-01-issuer-cache

Conversation

@AtefBellaaj

@AtefBellaaj AtefBellaaj commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Pull Request

Before submitting, please review the Contributing Guide.

Documentation changes belong in the LibreChat documentation repository.

Summary

Part 1 of 9 in the OpenID back-channel logout stack. It is based on dev, and #18435 builds on it.

Two small helpers that back-channel logout needs are not reusable today. The OpenID issuer match lives as private code inside api/strategies/openIdJwtStrategy.js, so the logout receiver cannot use the same rule. The single-use cache read (consumeCacheEntry) returns only the value, so a caller that consumes an entry and then fails cannot put the entry back with its remaining lifetime.

This PR moves the issuer match into packages/api/src/auth/openid.ts as isOpenIdIssuerMatch, and the JWT strategy now calls it. consumeCacheEntry now returns { value, expires }, and a new restoreCacheEntry puts a consumed entry back. The only current caller, exchangeAdminCode, reads consumed.value. Runtime behavior does not change.

How it works

The JWT strategy drops its private copy and delegates to the shared helper. The helper keeps the same rules: an exact match after normalization, or a {tenantid} template match against one path segment (Microsoft Entra).

-const issuerTemplateRegexCache = new Map();
-function issuerMatchesTemplate(expected, actual) { ... }
-const isOpenIdIssuerAllowed = (payload, openIdConfig) => { ... };
+const isOpenIdIssuerAllowed = (payload, openIdConfig) =>
+  isOpenIdIssuerMatch(openIdConfig.serverMetadata().issuer, payload?.iss);

restoreCacheEntry gives back only the time the entry had left. It does nothing for an entry that already expired, because Keyv reads a TTL of 0 as "never expires".

restoreCacheEntry(cache, key, consumed)
  if consumed.expires is unknown
    set key with the cache default TTL
  else if consumed.expires <= now
    skip                       # ttl 0 would mean "no expiry"
  else
    set key with ttl = consumed.expires - now

Type of change

  • Refactor

Testing

Tested environments/configuration:

No configuration needed. The change has no runtime effect.

Automated tests:

  • Added cases in packages/api/src/auth/openid.spec.ts for exact, normalized and {tenantid} template issuer matches.
  • Added packages/api/src/cache/consume.spec.ts cases for the returned expiry and for restore with a known, unknown and past expiry.
  • Updated the mock in api/strategies/openIdJwtStrategy.spec.js.
  • CI runs typecheck and the full suites.

Screenshots / recordings

No user-facing change.

Risk / compatibility

consumeCacheEntry changes its return type. exchangeAdminCode is its only caller, and this PR updates it.

Checklist

  • I reviewed my own changes
  • Relevant tests have been added or updated
  • Existing relevant tests pass
  • The change does not introduce new warnings or errors
  • User-facing or complex behavior is documented where necessary
  • Required dependency changes have been merged/published
  • Required documentation PR: N/A

@AtefBellaaj

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T19:59:42.429533Z ce04ffd Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: ce04ffd895

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Move isOpenIdIssuerMatch into packages/api so the JWT strategy and the
back-channel logout code share one issuer comparison. The single-use cache
read now returns the entry with its expiry, and restoreCacheEntry puts an
entry back with its remaining TTL.

Related to AI-2415.
@AtefBellaaj

Copy link
Copy Markdown
Collaborator Author

/quality check.all

@github-actions

Copy link
Copy Markdown
Contributor

Quality request was not started.

Use /quality check-all|risk|mutation scopes=all|data-schemas,api,server static=off|on or /quality ignore. Defaults: scopes=all static=off. Mutation supports data-schemas only.

@AtefBellaaj

Copy link
Copy Markdown
Collaborator Author

/quality check-all

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Quality report

Gate: open. The selected checks completed. Scores and risk levels are advisory.

Status: completed (success). Actions run, attempt 1.

  • Commit: 0202a6584af25cd3b68456e10ed4b209997a06a9
  • Requested by: @AtefBellaaj
  • Check: check-all
  • Scopes: data-schemas, api, server
  • Static mutants: off
  • Change risk: data-schemas, api, server
  • Mutation testing (experimental, data-schemas only): data-schemas (four shards)

Change risk

Takeaway: Every changed line has a test. Highest CRAP among changed functions: consumeCacheEntry at packages/api/src/cache/consume.ts:16, CRAP 13.0 with 94% coverage.

Workspace Files measured Incomplete Unavailable Changed lines with no test Highest CRAP
api 3 of 3 0 0 0 13.0
server 1 of 1 0 0 0 2.0

Refactor hints (highest CRAP first)

Function Complexity Coverage CRAP Location
consumeCacheEntry 13 94% 13.0 packages/api/src/cache/consume.ts:16
exchangeAdminCode 8 100% 8.0 packages/api/src/auth/exchange.ts:149
restoreCacheEntry 5 100% 5.0 packages/api/src/cache/consume.ts:58
isOpenIdIssuerMatch 4 100% 4.0 packages/api/src/auth/openid.ts:76
issuerMatchesTemplate 3 100% 3.0 packages/api/src/auth/openid.ts:62
isOpenIdIssuerAllowed 2 100% 2.0 api/strategies/openIdJwtStrategy.js:60
escapeRegExp 1 100% 1.0 packages/api/src/auth/openid.ts:56

CRAP = complexity² × (1 − coverage)³ + complexity

Jobs (2)
Check Result
Change risk (api) success
Change risk (server) success

All data (quality-report artifact) · Run page

The gate opens only when the workflow, source selection, tooling verification, and every report job succeed. A report job fails on a failed test in its workspace's own Jest run, a tool error, a job timeout, or an incomplete mutation report, including a mutation shard that reaches its execution budget. A cancelled workflow keeps the gate closed. Scores are advisory. A change-risk file marked incomplete or unavailable, for example by a failed api/ source-alias suite, does not close the gate.

@danny-avila danny-avila left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed as part of the full stack. No issues found here.

@lia-by-librechat lia-by-librechat Bot changed the title refactor(auth): share OpenID issuer matching and cache entry expiry 🔩 refactor: Share OpenID Issuer Matching and Cache Expiry Helpers Nov 20, 2026
@AtefBellaaj
AtefBellaaj removed this pull request from stack #18441 November 20, 2026 17:09
@AtefBellaaj
AtefBellaaj added this pull request to stack #18634 November 20, 2026 17:12

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants