All files / util accessTokenParser.js

64.29% Statements 9/14
50% Branches 6/12
100% Functions 2/2
64.29% Lines 9/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39    15x                 15x 1x 1x 1x 1x                           15x   1x       1x      
'use strict'
 
const debug = require('debug')('crowi:util:accessTokenParser')
 
/**
 * Extract Bearer token from Authorization header.
 *
 * @param {Object} headers HTTP request headers
 * @param {string} headers.authorization Authorization header value.
 * @return {?string} found access_token or null.
 */
const extractBearerToken = headers => {
  const v = headers.authorization
  debug('Authorization', v)
  Eif (!v) {
    return null
  }
 
  const parts = v
    .trim()
    .replace(/( )+/g, ' ')
    .split(' ')
  if (parts.length !== 2 || parts[0] !== 'Bearer') {
    return null
  }
 
  return parts[1]
}
 
module.exports = {
  parseAccessToken: req => {
    Iif (!req) {
      throw new Error('req required.')
    }
 
    return extractBearerToken(req.headers) || req.query.access_token || req.body.access_token || null
  },
}