Skip to content

Commit

Permalink
feat: validate account.tokens.type
Browse files Browse the repository at this point in the history
  • Loading branch information
Taekyoon authored and gr2m committed Nov 20, 2016
1 parent e9e4f59 commit f5adbaf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/account.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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')
var isValidTokenType = require('./utils/is-valid-token-type')

function account (setupPromise, state, findAccountOptions) {
return {
tokens: {
add: function (tokenOptions) {
if (!isValidTokenType(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: 'Token type must consist of lowercase letters (a-z), digits (0-9), _ and - only. Must begin with a letter',
status: 400
})
12 changes: 12 additions & 0 deletions lib/utils/is-valid-token-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = isValidTokenType

function isValidTokenType (token) {
var validPattern = /^[a-z][a-z0-9\-_]*$/

if (typeof token !== 'string' ||
!validPattern.test(token)) {
return false
}

return true
}

0 comments on commit f5adbaf

Please sign in to comment.