Skip to content

Commit

Permalink
Merge pull request #171 from screwdriver-cd/user-tokens
Browse files Browse the repository at this point in the history
fix(tokens): Use URL-safe values, add toJson
  • Loading branch information
minzcmu authored Jun 7, 2017
2 parents dc5de96 + 35164fa commit 6cfb766
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/generateToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const nodeify = require('./nodeify');
const crypto = require('crypto');
const base64url = require('base64url');
const ALGORITHM = 'sha256';
const ENCODING = 'base64';
const TOKEN_LENGTH = 256; // Measured in bits

/**
Expand All @@ -13,7 +13,7 @@ const TOKEN_LENGTH = 256; // Measured in bits
*/
function generateValue() {
return nodeify.withContext(crypto, 'randomBytes', [TOKEN_LENGTH / 8])
.then(buffer => buffer.toString(ENCODING));
.then(buffer => base64url(buffer.toString()));
}

/**
Expand All @@ -22,7 +22,7 @@ function generateValue() {
* @return {String} Hashed value
*/
function hashValue(value) {
return crypto.createHash(ALGORITHM).update(value).digest(ENCODING);
return base64url(crypto.createHash(ALGORITHM).update(value).digest());
}

module.exports = {
Expand Down
17 changes: 17 additions & 0 deletions lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ class TokenModel extends BaseModel {
return model;
});
}

/**
* Get the token as JSON, including value if it exists
* @method toJson
* @return {Object}
*/
toJson() {
const output = super.toJson();

if (this.value) {
output.value = this.value;
}

delete output.hash;

return output;
}
}

module.exports = TokenModel;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"dependencies": {
"async": "^2.0.1",
"base64url": "^2.0.0",
"compare-versions": "^3.0.0",
"hoek": "^4.0.1",
"iron": "^4.0.1",
Expand Down
4 changes: 2 additions & 2 deletions test/lib/generateToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ sinon.assert.expose(assert, { prefix: '' });
describe('generateToken', () => {
const RANDOM_BYTES = 'some random bytes';
// Result of passing 'some random bytes' through a sha256 hash, in base64
const HASH = 'mF0EvjvxWMrVz5ZGJcnbe0ZPooUlv/DAB9VrV6bmZmg=';
const HASH = 'mF0EvjvxWMrVz5ZGJcnbe0ZPooUlv_DAB9VrV6bmZmg';
let firstValue;

it('generates a value', () =>
generateToken.generateValue()
.then((value) => {
firstValue = value;
// Check that it's a base64 value of the right length
assert.match(value, /[a-zA-Z0-9+/]{43}=/);
assert.match(value, /[a-zA-Z0-9_-]{43}/);
}));

it('generates a different value on a second call', () => {
Expand Down
27 changes: 24 additions & 3 deletions test/lib/token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const schema = require('screwdriver-data-schema');
sinon.assert.expose(assert, { prefix: '' });

describe('Token Model', () => {
const password = 'password';
let datastore;
let generateTokenMock;
let BaseModel;
Expand Down Expand Up @@ -47,8 +46,7 @@ describe('Token Model', () => {
id: 6789,
name: 'Mobile client auth token',
description: 'For the mobile app',
lastUsed: '2017-05-10T01:49:59.327Z',
password
lastUsed: '2017-05-10T01:49:59.327Z'
};
token = new TokenModel(createConfig);
});
Expand Down Expand Up @@ -102,4 +100,27 @@ describe('Token Model', () => {
});
});
});

describe('toJson', () => {
const expected = {
userId: 12345,
id: 6789,
name: 'Mobile client auth token',
description: 'For the mobile app',
lastUsed: '2017-05-10T01:49:59.327Z'
};
const value = 'tokenValue';

it('functions normally if no value is present', () => {
const output = token.toJson();

assert.deepEqual(output, expected);
});

it('adds the value field if present', () => {
token.value = value;

assert.deepEqual(token.toJson(), Object.assign({}, expected, { value }));
});
});
});

0 comments on commit 6cfb766

Please sign in to comment.