Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for hapi 17.x.x #38

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/hapi-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ server.register(jwt, (err) => {
complete: true,

// Dynamically provide a signing key based on the kid in the header and the singing keys provided by the JWKS endpoint.

/* If you're using Hapi 17.x.x you have to use version 8.x.x of hapi-auth-jwt2
(https://github.com/dwyl/hapi-auth-jwt2#compatibility) and use the promise based version jwksRsa.hapiJwt2KeyAsync instead of jwksRsa.hapiJwt2Key
*/

key: jwksRsa.hapiJwt2Key({
cache: true,
rateLimit: true,
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ declare module 'jwks-rsa' {

function hapiJwt2Key(options: JwksRsa.Options): (name: string, scheme: string, options?: any) => void;

function hapiJwt2KeyAsync(options: JwksRsa.Options): (name: string, scheme: string, options?: any) => void;

function koaJwtSecret(options: JwksRsa.Options): (name: string, scheme: string, options?: any) => void;

class ArgumentError extends Error {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { JwksClient } from './JwksClient';

import * as errors from './errors';
import { hapiJwt2Key } from './integrations/hapi';
import { hapiJwt2Key, hapiJwt2KeyAsync } from './integrations/hapi';
import { expressJwtSecret } from './integrations/express';
import { koaJwtSecret } from './integrations/koa';

Expand All @@ -16,4 +16,5 @@ module.exports.SigningKeyNotFoundError = errors.SigningKeyNotFoundError;

module.exports.expressJwtSecret = expressJwtSecret;
module.exports.hapiJwt2Key = hapiJwt2Key;
module.exports.hapiJwt2KeyAsync = hapiJwt2KeyAsync;
module.exports.koaJwtSecret = koaJwtSecret;
17 changes: 17 additions & 0 deletions src/integrations/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ const handleSigningKeyError = (err, cb) => {
}
};

/**
* Call hapiJwt2Key as a Promise
* @param {object} options
* @returns {Promise}
*/
module.exports.hapiJwt2KeyAsync = (options) => {
const secretProvider = module.exports.hapiJwt2Key(options);
return function(decoded) {
return new Promise((resolve, reject) => {
const cb = (err, key) => {
(!key || err) ? reject(err) : resolve({ key });
};
secretProvider(decoded, cb);
});
};
};

module.exports.hapiJwt2Key = (options) => {
if (options === null || options === undefined) {
throw new ArgumentError('An options object must be provided when initializing expressJwtSecret');
Expand Down