All files / lib/util accessTokenParser.ts

61.54% Statements 8/13
50% Branches 6/12
100% Functions 2/2
61.54% Lines 8/13

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    16x                 16x 1x 1x 1x                           16x 1x       1x    
import Debug from 'debug'
 
const debug = 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
  Eif (!v) {
    return null
  }
 
  const parts = v
    .trim()
    .replace(/( )+/g, ' ')
    .split(' ')
  if (parts.length !== 2 || parts[0] !== 'Bearer') {
    return null
  }
 
  return parts[1]
}
 
export const parseAccessToken = req => {
  Iif (!req) {
    throw new Error('req required.')
  }
 
  return extractBearerToken(req.headers) || req.query.access_token || req.body.access_token || null
}