From ac0a4651bac5f81b78faa3f186133d67ef8ecb95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Iv=C3=A1n=20Vieitez=20Parra?= <3857362+corrideat@users.noreply.github.com> Date: Mon, 5 Jun 2023 13:34:30 +0200 Subject: [PATCH] Fix issues with authenticated fetch: generator was being created each time --- README.md | 4 ++-- package-lock.json | 4 ++-- package.json | 2 +- src/lib/authenticatedFetch.ts | 28 ++++++++++++++++------------ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b000167..2b61064 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,9 @@ server(listeners.node) ## 🤝 Contributing 🎉 We appreciate contributions from the community! If you have any ideas, -suggestions, or find any issues, feel free to open an issue or submit a pull +suggestions or find any issues, feel free to open an issue or submit a pull request on our -[GitHub repository](https://github.com/Exact-Realty/hydra-rfc8693). +[GitHub repository](https://github.com/Exact-Realty/ts-hydra-rfc8693). ## ❗️ Disclaimer diff --git a/package-lock.json b/package-lock.json index 84f72c5..556ab4f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@exact-realty/hydra-rfc8693", - "version": "1.1.2", + "version": "1.1.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@exact-realty/hydra-rfc8693", - "version": "1.1.2", + "version": "1.1.4", "hasInstallScript": true, "license": "Apache-2.0 WITH LLVM-exception", "devDependencies": { diff --git a/package.json b/package.json index b122fcf..909fd00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@exact-realty/hydra-rfc8693", - "version": "1.1.3", + "version": "1.1.4", "description": "An implementation of RFC 8693 for Ory Hydra, providing powerful capabilities for token exchange in OAuth 2.0 and OpenID Connect servers.", "main": "dist/index.js", "module": "./dist/index.mjs", diff --git a/src/lib/authenticatedFetch.ts b/src/lib/authenticatedFetch.ts index 4d299be..c9da07a 100644 --- a/src/lib/authenticatedFetch.ts +++ b/src/lib/authenticatedFetch.ts @@ -20,38 +20,41 @@ const expiresSymbol = Symbol(); // TODO: Add RFC 9068 support -export type TAuthenticatedFetchParams = +export type TAuthenticatedFetchParams = { + ['scope']?: string; + ['audience']?: string; + ['fetchFn']?: typeof fetch; +} & ( | { ['tokenEndpointUri']?: string; ['clientAuthMethod']: 'none'; ['clientId']?: string; ['clientSecret']?: string; - ['scope']?: string; - ['audience']?: string; } | { ['tokenEndpointUri']: string; ['clientAuthMethod']: 'client_secret_basic' | 'client_secret_post'; ['clientId']: string; ['clientSecret']: string; - ['scope']?: string; - ['audience']?: string; - }; + } +); const authenticatedFetch = ( config_: Readonly, ): typeof fetch => { + const fetchFn = config_['fetchFn'] ?? fetch; + if (config_['clientAuthMethod'] === 'none') { - return fetch; + return fetchFn; } // Redefine config for it to have the correct type const config = config_; - const token: { [accessTokenSymbol]: string; [expiresSymbol]: bigint } = - Object.create(null); - async function* getToken() { + const token: { [accessTokenSymbol]: string; [expiresSymbol]: bigint } = + Object.create(null); + const refreshToken = async (): Promise => { const tokenRequest = await fetch( new Request(config['tokenEndpointUri'], { @@ -147,18 +150,19 @@ const authenticatedFetch = ( } } + const token = getToken(); + const authenticatedFetch: typeof fetch = async (...args) => { const input = args[0]; const init = args[1]; const request = new Request(input, init); - const token = getToken(); request.headers.set( 'authorization', 'Bearer ' + (await token.next()).value, ); - return fetch(request); + return fetchFn(request); }; return authenticatedFetch;