'use strict'

var ComposrError = require('composr-core').ComposrError
var logger = require('../../utils/composrLogger')
var tokenVerifier = require('corbel-token-verifier')

/**
 * Token Object Middleware
 */
module.exports = function () {
  return function tokenObjectHook (req, res, next) {
    var authHeader = req.header('Authorization') || ''
    var tokenObject = tokenVerifier(authHeader)

    if (authHeader && !tokenObject) {
      logger.debug('[CorbelAuthHook]', 'Malformed user token')
      return next(new ComposrError('error:malformed:token', 'Your token is malformed', 400))
    } else if (!tokenObject) {
      logger.debug('[CorbelAuthHook]', 'Request without token')
    } else {
      logger.debug('[CorbelAuthHook]', 'Request with token')
      req.tokenObject = tokenObject
    }

    return next()
  }
}

'use strict'

var ComposrError = require('composr-core').ComposrError
var logger = require('../../utils/composrLogger')
var tokenVerifier = require('corbel-token-verifier')

/**
 * Token Object Middleware
 */
module.exports = function () {
  return function tokenObjectHook (req, res, next) {
    var authHeader = req.header('Authorization') || ''

    if(!authHeader){
      return next()
    }
    
    var tokenObject = tokenVerifier(authHeader)

    if (authHeader && !tokenObject) {
      logger.debug('[CorbelAuthHook]', 'Malformed user token')
      return next(new ComposrError('error:malformed:token', 'Your token is malformed', 400))
    } else if (!tokenObject) {
      logger.debug('[CorbelAuthHook]', 'Request without token')
    } else {
      logger.debug('[CorbelAuthHook]', 'Request with token')
      req.tokenObject = tokenObject
    }

    return next()
  }
}

Fix error when auth not present
Fix error when auth not present

Change-Id: I8222b18587954fa00c5887aaf0e67db958509c7e
