-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-server.js
53 lines (44 loc) · 1.38 KB
/
test-server.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
var koa = require('koa');
var koajwt = require('./index');
var profile = {
id: 123
};
var token = koajwt.sign(profile, 'secret', { expiresInMinutes: 60*5 });
console.log('Starting koa-jwt test server on http://localhost:3000/');
console.log('');
console.log('You can test the server by issuing curl commands like the following:');
console.log('')
console.log(' curl http://localhost:3000/public/foo # should succeed (return "unprotected")');
console.log(' curl http://localhost:3000/api/foo # should fail (return "401 Unauthorized ...")');
console.log(' curl -H "Authorization: Bearer ' + token + '" http://localhost:3000/api/foo # should succeed (return "protected")');
console.log('')
var app = koa();
// Custom 401 handling
app.use(function *(next){
try {
yield next;
} catch (err) {
if (401 == err.status) {
this.status = 401;
this.body = '401 Unauthorized - Protected resource, use Authorization header to get access\n';
} else {
throw err;
}
}
});
// Unprotected middleware
app.use(function *(next){
if (this.url.match(/^\/public/)) {
this.body = 'unprotected\n';
} else {
yield next;
}
});
// Middleware below this line is only reached if JWT token is valid
app.use(koajwt({ secret: 'secret' }));
app.use(function *(){
if (this.url.match(/^\/api/)) {
this.body = 'protected\n';
}
});
app.listen(3000);