Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
Add simple way to generate deploy tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
yamalight committed Aug 2, 2017
1 parent 58b6b2d commit 7b8d36a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,26 @@ module.exports = server =>
handler(request, reply) {
const replyObj = {
message: 'Token is valid',
credentials: _.omit(request.auth.credentials, ['password']),
credentials: request.auth.credentials,
};
reply(replyObj);
},
});

server.route({
method: 'GET',
path: '/deployToken',
config: {auth: 'token'},
handler(request, reply) {
// generate new deploy token
const user = request.auth.credentials;
const token = jwt.sign({loggedIn: true, user, deploy: true}, auth.privateKey, {
algorithm: 'HS256',
});
reply({token});
},
});

server.route({
method: 'GET',
path: '/login',
Expand Down
27 changes: 25 additions & 2 deletions test/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ module.exports = server =>
t.ok(result.token, 'Has token');

const decodedUser = jwt.verify(result.token, authConfig.privateKey);
delete decodedUser.iat;
delete decodedUser.exp;

t.equal(decodedUser.user.username, 'admin', 'Login matches request');
t.ok(decodedUser.loggedIn, 'Is logged in');
Expand All @@ -68,6 +66,31 @@ module.exports = server =>
});
});

tap.test('Should generate valid deploy token', t => {
const options = {
method: 'GET',
url: '/deployToken',
headers: {
Authorization: `Bearer ${token}`,
},
};

server.inject(options, response => {
const result = response.result;

t.equal(response.statusCode, 200, 'Correct status code');
t.ok(result.token, 'Has token');

const decodedUser = jwt.verify(result.token, authConfig.privateKey);

t.equal(decodedUser.user.username, 'admin', 'Login matches request');
t.ok(decodedUser.loggedIn, 'Is logged in');
t.ok(decodedUser.deploy, 'Is logged in');

t.end();
});
});

tap.test('Should not login without a token', t => {
const options = {
method: 'POST',
Expand Down

0 comments on commit 7b8d36a

Please sign in to comment.