Skip to content

Commit

Permalink
feat: validate account.tokens.type
Browse files Browse the repository at this point in the history
Added validating account.tokens.type in account.js that might cause invalidate form of tokens
  • Loading branch information
Taekyoon authored and gr2m committed Nov 11, 2016
1 parent 44d694d commit 0f0e8b3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/account.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module.exports = account

var addTokenToUserDoc = require('./utils/add-token-to-user-doc')
var errors = require('./utils/errors')
var findUserDoc = require('./utils/find-user-doc-by-username-or-id-or-token')

function account (setupPromise, state, findAccountOptions) {
return {
tokens: {
add: function (tokenOptions) {
var validPattern = /^([a-z])([a-z0-9\-_])*$/

if (typeof tokenOptions.type !== 'string' ||
!validPattern.test(tokenOptions.type)) {
return Promise.reject(errors.TOKEN_TYPE_INVALID)
}

return setupPromise

.then(function () {
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ module.exports.USERNAME_EMPTY = hoodieError({
message: 'username must be set',
status: 400
})

module.exports.TOKEN_TYPE_INVALID = hoodieError({
name: 'Bad Request',
message: 'Type must be a string of lowercase characters, numbers, -, or _, and must begin with a character.',
status: 400
})
26 changes: 26 additions & 0 deletions test/unit/account/tokens-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var test = require('tap').test

var account = require('../../../lib/account')

test('add', function (group) {
group.test('with invalid symbols', function (t) {
t.plan(3)

account().tokens.add({
id: 'secrettoken',
type: 'abc$123'
})

.then(function () {
t.fail('tokens.add should reject')
})

.catch(function (error) {
t.is(error.name, 'Bad Request')
t.is(error.status, 400)
t.is(error.message, 'Type must be a string of lowercase characters, numbers, -, or _, and must begin with a character.')
})
})

group.end()
})

0 comments on commit 0f0e8b3

Please sign in to comment.