Skip to content

Commit

Permalink
useJWT plugin: allow overriding any JwksClient options (#3182)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Cocaud <v.cocaud@gmail.com>
  • Loading branch information
bgentry and EmrysMyrddin committed May 17, 2024
1 parent 71f276e commit 8663e78
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lovely-ladybugs-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-yoga/plugin-jwt': minor
---

Add the possibility to customize JwksClient options
8 changes: 7 additions & 1 deletion packages/plugins/jwt/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createGraphQLError, Plugin } from 'graphql-yoga';
import jsonwebtoken, { Algorithm, JwtPayload, VerifyOptions } from 'jsonwebtoken';
import { JwksClient } from 'jwks-rsa';
import { JwksClient, Options as JwksClientOptions } from 'jwks-rsa';

const { decode } = jsonwebtoken;

Expand Down Expand Up @@ -39,6 +39,10 @@ export interface JwtPluginOptionsBase extends VerifyOptions {
}

export interface JwtPluginOptionsWithJWKS extends JwtPluginOptionsBase {
/**
* Options to be passed to the jwks-rsa client.
*/
jwksOpts?: JwksClientOptions;
/**
* The endpoint to fetch keys from.
*
Expand All @@ -54,6 +58,7 @@ export interface JwtPluginOptionsWithSigningKey extends JwtPluginOptionsBase {
* You can also use the jwks option to fetch the key from a JWKS endpoint
*/
signingKey: string;
jwksOpts?: never;
jwksUri?: never;
}

Expand All @@ -77,6 +82,7 @@ export function useJWT(options: JwtPluginOptions): Plugin {
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: options.jwksUri,
...options.jwksOpts,
});
}

Expand Down

0 comments on commit 8663e78

Please sign in to comment.