Skip to content

Instantly share code, notes, and snippets.

@sergioalvz
Last active April 24, 2020 12:09
Show Gist options
  • Save sergioalvz/fe1388096f06097f0b82c800369adbff to your computer and use it in GitHub Desktop.
Save sergioalvz/fe1388096f06097f0b82c800369adbff to your computer and use it in GitHub Desktop.
A workaround to make jwks-rsa to be compliant with new hapi-auth-jwt2#v-17 API
import { hapiJwt2Key } from 'jwks-rsa';
async function validate(decoded) {
if (decoded && decoded.sub) {
return { isValid: true };
}
return { isValid: false };
}
const secretProvider = hapiJwt2Key({
cache: true,
jwksRequestsPerMinute: 5,
jwksUri: '{YOUR-AUTH0-DOMAIN}/.well-known/jwks.json',
rateLimit: true,
});
async function keyProvider(decoded) {
return new Promise((resolve, reject) => {
const cb = (err, key) => {
if (!key) {
reject(err);
} else {
resolve({
key,
});
}
};
// @ts-ignore wrong signature from jwks-rsa type definitions
secretProvider(decoded, cb);
});
}
export function mount(server) {
server.auth.strategy('jwt', 'jwt', {
complete: true,
key: keyProvider,
validate,
verifyOptions: {
audience: '{YOUR-API-AUDIENCE-ATTRIBUTE}',
issuer: "{YOUR-AUTH0-DOMAIN}",
algorithms: ['RS256'],
},
});
server.auth.default('jwt');
}
@mariuslazar93
Copy link

Thanks for this!

@calendee
Copy link

I'm using this as vanilla JS. When I try to hit a route, I get TypeError: secretProvider is not a function. Any idea why that is?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment