Skip to content

Commit

Permalink
Simplify request wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpatrick committed Feb 19, 2021
1 parent 596e944 commit 3d962c6
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 462 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const client = jwksClient({
strictSsl: true, // Default value
jwksUri: 'https://sandrino.auth0.com/.well-known/jwks.json',
requestHeaders: {}, // Optional
requestAgentOptions: {}, // Optional
timeout: 30000, // Defaults to 30s
proxy: '[protocol]://[username]:[pass]@[address]:[port]', // Optional
timeout: 30000 // Defaults to 30s
});

const kid = 'RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg';
Expand Down Expand Up @@ -95,13 +93,14 @@ certificate authority to establish TLS communication with the `jwks_uri`.

```js
const jwksClient = require("jwks-rsa");
const https = require('https');
const client = jwksClient({
strictSsl: true, // Default value
jwksUri: 'https://my-enterprise-id-provider/.well-known/jwks.json',
requestHeaders: {}, // Optional
requestAgentOptions: {
requestAgent: new https.Agent({
ca: fs.readFileSync(caFile)
}
})
});
```

Expand All @@ -110,9 +109,7 @@ documentation](https://github.com/request/request#using-optionsagentoptions).

### Proxy configuration

There are two ways to configure the usage of a proxy:
- Provide the ```proxy``` option when initialiting the client as shown above
- Provide the ```HTTP_PROXY```, ```HTTPS_PROXY``` and ```NO_PROXY``` environment variables
You can configure using a proxy with a [custom http(s) agent](https://github.com/TooTallNate/node-https-proxy-agent).

### Loading keys from local file, environment variable, or other externals

Expand Down
33 changes: 9 additions & 24 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { SecretCallback, SecretCallbackLong } from 'express-jwt';
import { AgentOptions as HttpAgentOptions } from 'http';
import { AgentOptions as HttpsAgentOptions } from 'https';

declare function JwksRsa(options: JwksRsa.ClientOptions): JwksRsa.JwksClient;
declare function JwksRsa(options: JwksRsa.Options): JwksRsa.JwksClient;

declare namespace JwksRsa {
class JwksClient {
constructor(options: ClientOptions | ClientOptionsWithObject);
constructor(options: Options | OptionsWithObject);

getKeys(cb: (err: Error | null, keys: unknown) => void): void;
getKeysAsync(): Promise<unknown>;
Expand All @@ -20,22 +18,22 @@ declare namespace JwksRsa {
[key: string]: string;
}

interface ClientOptions {
interface Options {
jwksUri: string;
rateLimit?: boolean;
cache?: boolean;
cacheMaxEntries?: number;
cacheMaxAge?: number;
jwksRequestsPerMinute?: number;
proxy?: string;
strictSsl?: boolean;
requestHeaders?: Headers;
timeout?: number;
requestAgentOptions?: HttpAgentOptions | HttpsAgentOptions;
requestAgent?: any;
fetcher?(jwksUri: string): Promise<unknown>;
getKeysInterceptor?(cb: (err: Error | null, keys: SigningKey[]) => void): void;
}

interface ClientOptionsWithObject extends Omit<ClientOptions, 'jwksUri'> {
interface OptionsWithObject extends Omit<Options, 'jwksUri'> {
/**
* @deprecated jwksObject should not be used. Use getKeysInterceptor as a replacement
*/
Expand All @@ -48,19 +46,6 @@ declare namespace JwksRsa {
publicKey: string;
}

interface Options {
jwksUri: string;
rateLimit?: boolean;
cache?: boolean;
cacheMaxEntries?: number;
cacheMaxAge?: number;
jwksRequestsPerMinute?: number;
strictSsl?: boolean;
requestHeaders?: Headers;
requestAgentOptions?: HttpAgentOptions | HttpsAgentOptions;
handleSigningKeyError?(err: Error, cb: (err: Error) => void): any;
}

interface RsaSigningKey {
kid: string;
getPublicKey(): string;
Expand All @@ -73,13 +58,13 @@ declare namespace JwksRsa {

function passportJwtSecret(options: ExpressJwtOptions): SecretCallback;

interface ExpressJwtOptions extends ClientOptions {
interface ExpressJwtOptions extends Options {
handleSigningKeyError?: (err: Error | null, cb: (err: Error | null) => void) => void;
}

function hapiJwt2Key(options: HapiJwtOptions): (decodedToken: DecodedToken, cb: HapiCallback) => void;

interface HapiJwtOptions extends ClientOptions {
interface HapiJwtOptions extends Options {
handleSigningKeyError?: (err: Error | null, cb: HapiCallback) => void;
}

Expand All @@ -98,7 +83,7 @@ declare namespace JwksRsa {

function koaJwtSecret(options: KoaJwtOptions): (header: TokenHeader) => Promise<string>;

interface KoaJwtOptions extends ClientOptions {
interface KoaJwtOptions extends Options {
handleSigningKeyError?(err: Error | null): Promise<void>;
}

Expand Down
Loading

0 comments on commit 3d962c6

Please sign in to comment.