-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
getToken endpoint to allow users to create durable JWT tokens.
- Loading branch information
1 parent
e79ac58
commit 2df439d
Showing
4 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const jwt = require('jsonwebtoken'); | ||
const config = require('../config'); | ||
|
||
function createToken (id, name, email, scope, expiration) { | ||
// Sign the JWT | ||
return jwt.sign( | ||
{ | ||
_id: id, | ||
name, | ||
contact_email: email, | ||
scope: scope | ||
}, | ||
config.jwtSecret, | ||
{ | ||
algorithm: 'HS256', | ||
expiresIn: expiration | ||
}); | ||
} | ||
|
||
module.exports = createToken; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const Hapi = require('hapi'); | ||
const proxyquire = require('proxyquire').noCallThru(); | ||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
const sinonChai = require('sinon-chai'); | ||
const authentication = require('../../plugins/authentication.js'); | ||
|
||
const expect = chai.expect; | ||
chai.should(); | ||
chai.use(sinonChai); | ||
const sandbox = sinon.sandbox.create(); | ||
|
||
const buildStubs = () => { | ||
const user = { | ||
_id: 'id', | ||
name: 'name', | ||
contact_email: 'email' | ||
}; | ||
const findOne = sandbox.stub().resolves(user); | ||
const token = 'token'; | ||
const createToken = sandbox.stub().resolves(token); | ||
const stubs = { | ||
'../models/user': { findOne }, | ||
'../models/createToken': createToken | ||
}; | ||
return { stubs, findOne, createToken, user, token }; | ||
}; | ||
|
||
const getServer = (stubs) => { | ||
const uploads = proxyquire('../../routes/auth.js', stubs); | ||
const server = new Hapi.Server(); | ||
server.connection({ port: 4000 }); | ||
return server.register(authentication).then(() => { | ||
server.route(uploads); | ||
return server; | ||
}); | ||
}; | ||
|
||
describe('auth getToken', () => { | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('Should create and return a new JWT token', () => { | ||
const { stubs, findOne, createToken, user, token } = buildStubs(); | ||
const credentials = { | ||
session_id: 'id' | ||
}; | ||
const options = { | ||
method: 'GET', | ||
url: '/getToken', | ||
credentials | ||
}; | ||
return getServer(stubs) | ||
.then((server) => { | ||
return server.inject(options).then((res) => { | ||
expect(findOne).to.have.been.calledWith(credentials); | ||
expect(createToken.firstCall.args[0]).to.equal(user._id); | ||
expect(createToken.firstCall.args[1]).to.equal(user.name); | ||
expect(createToken.firstCall.args[2]).to.equal(user.contact_email); | ||
expect(createToken.firstCall.args[3]).to.equal('user'); | ||
expect(createToken.firstCall.args[4]).to.equal('365d'); | ||
expect(res.result.token).to.equal(token); | ||
}); | ||
}); | ||
}); | ||
}); |