Send With Confidence
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.
Time to read: 1 minute
$ npm install koa koa-jwt co-body --save |
$ openssl genrsa -out demo.rsa 1024 # the 1024 is the size of the key we are generating | |
$ openssl rsa -in demo.rsa -pubout > demo.rsa.pub |
var koa = require('koa'); | |
var parse = require('co-body'); | |
var jwt = require('koa-jwt'); | |
var fs = require('fs'); | |
var app = koa(); | |
var publicKey = fs.readFileSync('demo.rsa.pub'); | |
var privateKey = fs.readFileSync('demo.rsa'); | |
// JWT Error Catcher | |
app.use(function *(next) { | |
try { | |
yield next; //Attempt to go through the JWT Validator | |
} catch(e) { | |
if (e.status == 401 ) { | |
// Prepare response to user. | |
this.status = e.status; | |
this.body = 'You don\'t have a signed token dude :(' | |
} else { | |
throw e; // Pass the error to the next handler since it wasn't a JWT error. | |
} | |
} | |
}); | |
// Public endpoint to login. | |
app.use(function *(next) { | |
if (this.url.match(/^\/login/)) { | |
var claims = yield parse(this); | |
var token = jwt.sign(claims, privateKey, {algorithm: 'RS256'}); | |
this.status = 200; | |
this.body = {token: token}; | |
} else { | |
yield next; | |
} | |
}); | |
// Everything behind this will be protected. | |
app.use(jwt({ | |
secret: publicKey, | |
algorithm: 'RS256' | |
})); | |
app.use(function *() { | |
this.status = 200; | |
this.body = 'You are logged in dude! Welcome!'; | |
}); | |
app.listen(3000); |
$ curl localhost:3000/api | |
# You don't have a signed token dude :( | |
$ curl -X POST -H "Content-Type: application/json" localhost:3000/login -d '{"username": "elbuo8"}' | |
# {"token": "verylongtokenstring :)"} | |
$ curl -X POST -H "Authorization: Bearer verylongtokenstring :)" localhost:3000/api -d '{"username": "elbuo8"}' | |
# You are logged in dude! Welcome! |
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.