# All files / src/middleware rerunBypassAuthentication.coffee

63.64% Statements 14/22
100% Branches 0/0
0% Functions 0/3
63.64% Lines 14/22

    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
    40
    41
    42
    431x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x

    1x

     auth = require 'basic-auth'
    Q = require "q"
    Client = require("../model/clients").Client
    logger = require "winston"
    crypto = require "crypto"

    config = require '../config/config'
    statsdServer = config.get 'statsd'
    application = config.get 'application'
    SDC = require 'statsd-client'
    os = require 'os'

    domain = "#{os.hostname()}.#{application.name}.appMetrics"
    sdc = new SDC statsdServer

    exports.authenticateUser = (ctx, done) ->

      Client.findOne { _id: ctx.request.header.clientid }, (err, client) ->
        ctx.authenticated = client
        ctx.parentID = ctx.request.header.parentid
        ctx.taskID = ctx.request.header.taskid
        done null, client

    ###
    # Koa middleware for authentication by basic auth
    ###
    exports.koaMiddleware = (next) ->
      startTime = new Date() if statsdServer.enabled
      authenticateUser = Q.denodeify exports.authenticateUser
      yield authenticateUser this

      if this.authenticated?
        sdc.timing "#{domain}.rerunBypassAuthenticationMiddleware", startTime if statsdServer.enabled
        yield next
      else
        this.authenticated =
          ip : '127.0.0.1'
        # This is a public channel, allow rerun
        sdc.timing "#{domain}.rerunBypassAuthenticationMiddleware", startTime if statsdServer.enabled
        yield next