-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
52 lines (40 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict'
const fastifyPlugin = require('fastify-plugin')
const fastifyJwtJwks = require('fastify-jwt-jwks').fastifyJwtJwks
const errorMessages = {
missingOptions: 'Please provide at least one of the "domain" or "secret" options.'
}
function fastifyAuth0Verify(instance, options, done) {
try {
let { domain, secret } = options
if (domain) {
delete options.domain
domain = domain.toString()
if (!domain.match(/^http(?:s?)/)) {
domain = new URL(`https://${domain}`).toString()
} else {
// Add missing trailing slash if it hasn't been provided in the config
domain = new URL(domain).toString()
}
options.jwksUrl = `${domain}.well-known/jwks.json`
} else if (!secret) {
// Throw missing options error here to prevent confusing error
// message from fastify-jwt-jwks being thrown to user
throw new Error(errorMessages.missingOptions)
}
return fastifyJwtJwks(instance, options, function (e) {
if (e) {
return done(e)
}
instance.decorate('auth0Verify', instance.jwtJwks)
done()
})
} catch (e) {
done(e)
}
}
module.exports = fastifyPlugin(fastifyAuth0Verify, { name: 'fastify-auth0-verify', fastify: '5.x' })
// Set the default export to the fastifyAuth0Verify function for ES module compatibility
module.exports.default = fastifyAuth0Verify
// Add a named export for the fastifyAuth0Verify function for CommonJS compatibility
module.exports.fastifyAuth0Verify = fastifyAuth0Verify