-
Notifications
You must be signed in to change notification settings - Fork 57
/
example.js
167 lines (136 loc) · 4.28 KB
/
example.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict'
/*
Register a user:
curl -i 'http://127.0.0.1:3000/register' -H 'content-type: application/json' --data '{"user": "myuser","password":"mypass"}'
Will return:
{"token":"YOUR_JWT_TOKEN"}
The application then:
1. generates a JWT token (from 'supersecret') and adds to the response headers
1. inserts user in the leveldb
Check it's all working by using one or the other auth mechanisms:
1. Auth using username and password (you can also use JWT on this endpoint)
curl 'http://127.0.0.1:3000/auth-multiple' -H 'content-type: application/json' --data '{"user": "myuser","password":"mypass"}'
{"hello":"world"}
1. Auth using JWT token
curl -i 'http://127.0.0.1:3000/auth' -H 'content-type: application/json' -H "auth: YOUR_JWT_TOKEN"
*/
const Fastify = require('fastify')
function build (opts) {
const fastify = Fastify(opts)
fastify.register(require('@fastify/jwt'), { secret: 'supersecret' })
fastify.register(require('@fastify/leveldb'), { name: 'authdb' })
fastify.register(require('../auth')) // just 'fastify-auth' IRL
fastify.after(routes)
fastify.decorate('verifyJWTandLevelDB', verifyJWTandLevelDB)
fastify.decorate('verifyUserAndPassword', verifyUserAndPassword)
function verifyJWTandLevelDB (request, reply, done) {
const jwt = this.jwt
const level = this.level.authdb
if (request.body && request.body.failureWithReply) {
reply.code(401).send({ error: 'Unauthorized' })
return done(new Error())
}
if (!request.raw.headers.auth) {
return done(new Error('Missing token header'))
}
jwt.verify(request.raw.headers.auth, onVerify)
function onVerify (err, decoded) {
if (err || !decoded.user || !decoded.password) {
return done(new Error('Token not valid'))
}
level.get(decoded.user, onUser)
function onUser (err, password) {
if (err) {
if (err.notFound) {
return done(new Error('Token not valid'))
}
return done(err)
}
if (!password || password !== decoded.password) {
return done(new Error('Token not valid'))
}
done()
}
}
}
function verifyUserAndPassword (request, reply, done) {
const level = this.level.authdb
if (!request.body || !request.body.user) {
return done(new Error('Missing user in request body'))
}
level.get(request.body.user, onUser)
function onUser (err, password) {
if (err) {
if (err.notFound) {
return done(new Error('Password not valid'))
}
return done(err)
}
if (!password || password !== request.body.password) {
return done(new Error('Password not valid'))
}
done()
}
}
function routes () {
fastify.route({
method: 'POST',
url: '/register',
schema: {
body: {
type: 'object',
properties: {
user: { type: 'string' },
password: { type: 'string' }
},
required: ['user', 'password']
}
},
handler: (req, reply) => {
req.log.info('Creating new user')
fastify.level.authdb.put(req.body.user, req.body.password, onPut)
function onPut (err) {
if (err) return reply.send(err)
fastify.jwt.sign(req.body, onToken)
}
function onToken (err, token) {
if (err) return reply.send(err)
req.log.info('User created')
reply.send({ token })
}
}
})
fastify.route({
method: 'GET',
url: '/no-auth',
handler: (req, reply) => {
req.log.info('Auth free route')
reply.send({ hello: 'world' })
}
})
fastify.route({
method: 'GET',
url: '/auth',
preHandler: fastify.auth([fastify.verifyJWTandLevelDB]),
handler: (req, reply) => {
req.log.info('Auth route')
reply.send({ hello: 'world' })
}
})
fastify.route({
method: 'POST',
url: '/auth-multiple',
preHandler: fastify.auth([
// Only one of these has to pass
fastify.verifyJWTandLevelDB,
fastify.verifyUserAndPassword
]),
handler: (req, reply) => {
req.log.info('Auth route')
reply.send({ hello: 'world' })
}
})
}
return fastify
}
module.exports = build