openhim-core
Version:
The OpenHIM core application that provides logging and routing of http requests
30 lines (26 loc) • 809 B
JavaScript
import { ClientModel } from '../model/clients'
import { config } from '../config'
import { promisify } from 'util'
export function authenticateUser (ctx, done) {
return ClientModel.findOne({_id: ctx.request.header.clientid}, (err, client) => {
if (err) { return done(err) }
ctx.authenticated = client
ctx.parentID = ctx.request.header.parentid
ctx.taskID = ctx.request.header.taskid
return done(null, client)
})
}
/*
* Koa middleware for authentication by basic auth
*/
export async function koaMiddleware (ctx, next) {
const _authenticateUser = promisify(authenticateUser)
await _authenticateUser(ctx)
if (ctx.authenticated != null) {
await next()
} else {
ctx.authenticated = {ip: '127.0.0.1'}
// This is a public channel, allow rerun
await next()
}
}