Last active
April 24, 2020 12:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
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
Thanks for this!