All files / src/middleware pollingBypassAuthentication.coffee

65% Statements 13/20
0% Branches 0/2
0% Functions 0/2
65% Lines 13/20
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 391x 1x 1x   1x 1x 1x 1x 1x   1x 1x   1x           1x               1x                      
Q = require "q"
Client = require("../model/clients").Client
logger = require "winston"
 
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
 
dummyClient = new Client
  clientID: 'DUMMY-POLLING-USER'
  clientDomain: 'openhim.org'
  name: 'DUMMY-POLLING-USER'
  roles: ['polling']
 
exports.authenticateUser = (ctx, done) ->
  ctx.authenticated = dummyClient
  done null, dummyClient
  
 
###
# Koa middleware for bypassing authentication for polling requests
###
exports.koaMiddleware = (next) ->
  startTime = new Date() if statsdServer.enabled
  authenticateUser = Q.denodeify exports.authenticateUser
  yield authenticateUser this
 
  if this.authenticated?
    sdc.timing "#{domain}.pollingBypassAuthenticationMiddleware", startTime if statsdServer.enabled
    yield next
  else
    this.response.status = 401
    sdc.timing "#{domain}.pollingBypassAuthenticationMiddleware", startTime if statsdServer.enabled