lib/src/auth/middleware/SessionAuthMiddleware.ts

30 lines
1.1 KiB
TypeScript
Raw Normal View History

import {Middleware} from '../../http/routing/Middleware'
import {Inject, Injectable} from '../../di'
import {Config} from '../../service/Config'
import {Logging} from '../../service/Logging'
2021-11-26 20:32:25 +00:00
import {AuthenticatableRepository} from '../types'
import {ResponseObject} from '../../http/routing/Route'
import {SessionSecurityContext} from '../context/SessionSecurityContext'
import {SecurityContext} from '../context/SecurityContext'
/**
* Injects a SessionSecurityContext into the request and attempts to
* resume the user's authentication.
*/
@Injectable()
export class SessionAuthMiddleware extends Middleware {
@Inject()
protected readonly config!: Config
@Inject()
protected readonly logging!: Logging
async apply(): Promise<ResponseObject> {
2021-11-26 20:32:25 +00:00
this.logging.debug('Applying session auth middleware.')
const repo = <AuthenticatableRepository> this.make(AuthenticatableRepository)
const context = <SessionSecurityContext> this.make(SessionSecurityContext, repo)
this.request.registerSingletonInstance(SecurityContext, context)
await context.resume()
}
}