{"id":19023084,"url":"https://github.com/platformatic/undici-oidc-interceptor","last_synced_at":"2026-02-10T14:02:47.757Z","repository":{"id":210251705,"uuid":"720217647","full_name":"platformatic/undici-oidc-interceptor","owner":"platformatic","description":null,"archived":false,"fork":false,"pushed_at":"2025-04-15T14:27:36.000Z","size":1039,"stargazers_count":5,"open_issues_count":4,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-20T19:52:28.894Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/platformatic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-17T21:03:15.000Z","updated_at":"2025-03-18T17:31:57.000Z","dependencies_parsed_at":"2023-12-07T11:39:31.064Z","dependency_job_id":"82be6067-44c9-4970-b1ee-06fc04521384","html_url":"https://github.com/platformatic/undici-oidc-interceptor","commit_stats":null,"previous_names":["platformatic/undici-oauth-interceptor","platformatic/undici-oidc-interceptor"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformatic%2Fundici-oidc-interceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformatic%2Fundici-oidc-interceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformatic%2Fundici-oidc-interceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformatic%2Fundici-oidc-interceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/platformatic","download_url":"https://codeload.github.com/platformatic/undici-oidc-interceptor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250400018,"owners_count":21424280,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T20:28:40.980Z","updated_at":"2026-02-10T14:02:47.752Z","avatar_url":"https://github.com/platformatic.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# undici-oidc-interceptor\n\n\n[![NPM version](https://img.shields.io/npm/v/undici-oidc-interceptor.svg?style=flat)](https://www.npmjs.com/package/undici-oidc-interceptor)\n\n`undici-oidc-interceptor` manages an [OIDC](https://openid.net/specs/openid-connect-core-1_0.html) access token and transparently sets the `Authorization` header on any\nrequest that is going to a limited set of domains.\n\nThe token is automatically renewed after it expires. It supports both a `refresh_token`\nand `client_credentials` grants.\n\n## Install\n\n```bash\nnpm i undici undici-oidc-interceptor\n```\n\n## Usage with client credentials\n\n```js\nconst { Agent } = require('undici')\nconst { createOidcInterceptor } = require('undici-oidc-interceptor')\nconst dispatcher = new Agent().compose(createOidcInterceptor({\n      // The paramerts for the cliend_credentials grant of OIDC\n      clientId: 'FILLME',\n      clientSecret: 'FILLME',\n      idpTokenUrl: 'https://your-idp.com/token',\n\n      // Set an array of status codes that the interceptor should refresh and\n      // retry the request on\n      retryOnStatusCodes: [401],\n\n      // The array of urls that this interceptor will be appending `Authorization` header\n      // for automatically\n      urls: ['FILLME'],\n\n      // OPTIONAL: an initial access token\n      accessToken: ''\n    }))\n``` \n\n## Usage with refresh token\n\n```js\nconst { Agent } = require('undici')\nconst { createOidcInterceptor } = require('undici-oidc-interceptor')\nconst dispatcher = new Agent().compose(createOidcInterceptor({\n      // Provide a refresh token so the interceptor can manage the access token\n      // The refresh token must include an issuer (`iss`)\n      refreshToken: '',\n      idpTokenUrl: 'https://your-idp.com/token',\n      clientId: 'FILLME',\n\n      // Set an array of status codes that the interceptor should refresh and\n      // retry the request on\n      retryOnStatusCodes: [401],\n\n      // The array of urls that this interceptor will be appending `Authorization` header\n      // for automatically\n      urls: [],\n\n      // OPTIONAL: an initial access token\n      accessToken: ''\n    }))\n```\n\n## Custom authentication decision\n\n```js\nconst { Agent } = require('undici')\nconst { createOidcInterceptor } = require('undici-oidc-interceptor')\nconst dispatcher = new Agent().compose(createOidcInterceptor({\n      // Provide a refresh token so the interceptor can manage the access token\n      // The refresh token must include an issuer (`iss`)\n      refreshToken: '',\n      idpTokenUrl: 'https://your-idp.com/token',\n      clientId: 'FILLME',\n\n      // Set an array of status codes that the interceptor should refresh and\n      // retry the request on\n      retryOnStatusCodes: [401],\n\n      // OPTIONAL: a callback function, if return 'true' then this interceptor will include `Authorization` header\n      shouldAuthenticate: (opts) =\u003e opts.header['something'] === 'test',\n\n      // OPTIONAL: an initial access token\n      accessToken: ''\n    }))\n```\n\n\u003e Note: `shouldAuthenticate` has higher priority than urls. Fallback to urls behavior when shouldAuthenticate is not provided\n\n## Per-request scope override\n\nYou can override the default scope for individual requests:\n\n```js\nconst { Agent, setGlobalDispatcher, request } = require('undici')\nconst { createOidcInterceptor } = require('undici-oidc-interceptor')\n\nconst agent = new Agent().compose(createOidcInterceptor({\n  clientId: 'FILLME',\n  clientSecret: 'FILLME',\n  idpTokenUrl: 'https://your-idp.com/token',\n  urls: ['https://api.example.com'],\n  scope: 'read write'\n}))\n\nsetGlobalDispatcher(agent)\n\n// Uses default scope from interceptor config\nawait request('https://api.example.com/resource')\n\n// Uses custom scope for this request\nawait request('https://api.example.com/admin', {\n  oidc: { scope: 'admin' }\n})\n```\n\n## Token store\n\nThis interceptor uses the [async-cache-dedupe](https://github.com/mcollina/async-cache-dedupe) package to cache access tokens. This improves efficiency by enabling token reuse across processes or instances and avoids unnecessary token refresh requests.\n\n- **Default:** In-memory storage.\n- **Custom storage:** You can provide your own backend (e.g., Redis) by supplying a compatible async-cache-dedupe configuration.\n\nExample: Using Redis as the token store\n```js\nconst Redis = require('ioredis')\nconst { Agent } = require('undici')\nconst { createOidcInterceptor } = require('undici-oidc-interceptor')\n\nconst redisClient = new Redis()\n\nconst dispatcher = new Agent().compose(createOidcInterceptor({\n  ...options,\n  tokenStore: {\n    name: 'test-cache',\n    ttl: 100,\n    storage: { \n      type: 'redis', \n      options: { \n        client: redisClient \n      } \n    }\n  }\n}))\n```\n\n### Custom TTL\nIf you want to customize the TTL for tokens based on the `expiresIn` value from the OIDC response, you can provide a custom function.\n\nExample:\n```js\nconst Redis = require('ioredis')\nconst { Agent } = require('undici')\nconst { createOidcInterceptor } = require('undici-oidc-interceptor')\n\nconst redisClient = new Redis()\n\nconst dispatcher = new Agent().compose(createOidcInterceptor({\n  ...options,\n  tokenStore: {\n    name: 'test-cache',\n    ttl: (tokenPayload) =\u003e tokenPayload.expiresIn * 80 / 100, // Sets token TTL to 80% of the OIDC expiry time\n    storage: { \n      type: 'redis', \n      options: { \n        client: redisClient \n      } \n    }\n  }\n}))\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplatformatic%2Fundici-oidc-interceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplatformatic%2Fundici-oidc-interceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplatformatic%2Fundici-oidc-interceptor/lists"}