You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.6 KiB
45 lines
1.6 KiB
import {Middleware} from '../../http/routing/Middleware'
|
|
import {Inject, Injectable, Instantiable} from '../../di'
|
|
import {Config} from '../../service/Config'
|
|
import {Logging} from '../../service/Logging'
|
|
import {AuthenticatableRepository} from '../types'
|
|
import {Maybe} from '../../util'
|
|
import {AuthenticationConfig, isAuthenticationConfig} from '../config'
|
|
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> {
|
|
this.logging.debug('Applying session auth middleware.')
|
|
const context = <SessionSecurityContext> this.make(SessionSecurityContext, this.getRepository())
|
|
this.request.registerSingletonInstance(SecurityContext, context)
|
|
await context.resume()
|
|
}
|
|
|
|
/**
|
|
* Build the correct AuthenticatableRepository based on the auth config.
|
|
* @protected
|
|
*/
|
|
protected getRepository(): AuthenticatableRepository {
|
|
const config: Maybe<AuthenticationConfig> = this.config.get('auth')
|
|
if ( !isAuthenticationConfig(config) ) {
|
|
throw new TypeError('Invalid authentication config.')
|
|
}
|
|
|
|
const repo: Instantiable<AuthenticatableRepository> = config.storage
|
|
return this.make(repo)
|
|
}
|
|
}
|