2021-06-05 18:24:12 +00:00
|
|
|
import {Inject, Injectable, Instantiable, StaticClass} from '../di'
|
2021-06-05 17:02:36 +00:00
|
|
|
import {Unit} from '../lifecycle/Unit'
|
|
|
|
import {Logging} from '../service/Logging'
|
2021-06-05 18:24:12 +00:00
|
|
|
import {CanonicalResolver} from '../service/Canonical'
|
|
|
|
import {Middleware} from '../http/routing/Middleware'
|
|
|
|
import {SessionAuthMiddleware} from './middleware/SessionAuthMiddleware'
|
|
|
|
import {AuthRequiredMiddleware} from './middleware/AuthRequiredMiddleware'
|
|
|
|
import {GuestRequiredMiddleware} from './middleware/GuestRequiredMiddleware'
|
|
|
|
import {Middlewares} from '../service/Middlewares'
|
2021-06-05 17:02:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit class that bootstraps the authentication framework.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class Authentication extends Unit {
|
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
2021-06-05 18:24:12 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly middleware!: Middlewares
|
|
|
|
|
2021-06-05 17:02:36 +00:00
|
|
|
async up(): Promise<void> {
|
|
|
|
this.container()
|
2021-06-05 18:24:12 +00:00
|
|
|
this.middleware.registerNamespace('@auth', this.getMiddlewareResolver())
|
|
|
|
}
|
|
|
|
|
2021-06-18 00:35:31 +00:00
|
|
|
/**
|
|
|
|
* Create the canonical namespace resolver for auth middleware.
|
|
|
|
* @protected
|
|
|
|
*/
|
2021-06-05 18:24:12 +00:00
|
|
|
protected getMiddlewareResolver(): CanonicalResolver<StaticClass<Middleware, Instantiable<Middleware>>> {
|
|
|
|
return (key: string) => {
|
|
|
|
return ({
|
|
|
|
web: SessionAuthMiddleware,
|
|
|
|
required: AuthRequiredMiddleware,
|
|
|
|
guest: GuestRequiredMiddleware,
|
|
|
|
})[key]
|
|
|
|
}
|
2021-06-05 17:02:36 +00:00
|
|
|
}
|
|
|
|
}
|