Skip to content

Commit

Permalink
Fix issues with authenticated fetch: generator was being created each…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
corrideat committed Jun 5, 2023
1 parent 2f662e1 commit ac0a465
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
28 changes: 16 additions & 12 deletions src/lib/authenticatedFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TAuthenticatedFetchParams>,
): 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<string> => {
const tokenRequest = await fetch(
new Request(config['tokenEndpointUri'], {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ac0a465

Please sign in to comment.