2021-06-05 17:02:36 +00:00
|
|
|
import {Unit} from '../lifecycle/Unit'
|
2021-11-26 20:32:25 +00:00
|
|
|
import {Injectable, Inject, StaticInstantiable} from '../di'
|
2021-06-05 17:02:36 +00:00
|
|
|
import {Logging} from '../service/Logging'
|
2021-11-26 20:32:25 +00:00
|
|
|
import {Middlewares} from '../service/Middlewares'
|
2021-06-05 18:24:12 +00:00
|
|
|
import {CanonicalResolver} from '../service/Canonical'
|
|
|
|
import {Middleware} from '../http/routing/Middleware'
|
|
|
|
import {AuthRequiredMiddleware} from './middleware/AuthRequiredMiddleware'
|
|
|
|
import {GuestRequiredMiddleware} from './middleware/GuestRequiredMiddleware'
|
2021-11-26 20:32:25 +00:00
|
|
|
import {SessionAuthMiddleware} from './middleware/SessionAuthMiddleware'
|
2021-11-27 16:30:49 +00:00
|
|
|
import {ViewEngine} from '../views/ViewEngine'
|
|
|
|
import {SecurityContext} from './context/SecurityContext'
|
2022-02-23 21:15:02 +00:00
|
|
|
import {LoginProvider, LoginProviderConfig} from './provider/LoginProvider'
|
|
|
|
import {Config} from '../service/Config'
|
|
|
|
import {ErrorWithContext, hasOwnProperty} from '../util'
|
|
|
|
import {Route} from '../http/routing/Route'
|
2021-06-05 17:02:36 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class Authentication extends Unit {
|
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
2021-06-05 18:24:12 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly middleware!: Middlewares
|
|
|
|
|
2022-02-23 21:15:02 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly config!: Config
|
|
|
|
|
|
|
|
protected providers: {[name: string]: LoginProvider<LoginProviderConfig>} = {}
|
|
|
|
|
2022-04-10 01:08:50 +00:00
|
|
|
getProvider(name: string): LoginProvider<LoginProviderConfig> {
|
|
|
|
const provider = this.providers[name]
|
|
|
|
if ( !provider ) {
|
|
|
|
throw new ErrorWithContext('Invalid auth provider name: ' + name, { name })
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
2021-06-05 17:02:36 +00:00
|
|
|
async up(): Promise<void> {
|
2021-06-05 18:24:12 +00:00
|
|
|
this.middleware.registerNamespace('@auth', this.getMiddlewareResolver())
|
2021-11-27 16:30:49 +00:00
|
|
|
|
|
|
|
this.container().onResolve<ViewEngine>(ViewEngine)
|
|
|
|
.then((engine: ViewEngine) => {
|
|
|
|
engine.registerGlobalFactory('user', req => {
|
|
|
|
return () => req?.make<SecurityContext>(SecurityContext)?.getUser()
|
|
|
|
})
|
|
|
|
})
|
2022-02-23 21:15:02 +00:00
|
|
|
|
|
|
|
const config = this.config.get('auth.providers', {})
|
|
|
|
const middleware = this.config.get('auth.middleware', SessionAuthMiddleware)
|
|
|
|
|
|
|
|
if ( !(middleware?.prototype instanceof Middleware) ) {
|
|
|
|
throw new ErrorWithContext('Auth middleware must extend Middleware base class', {
|
|
|
|
providedValue: middleware,
|
|
|
|
configKey: 'auth.middleware',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const name in config ) {
|
|
|
|
if ( !hasOwnProperty(config, name) ) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.providers[name] ) {
|
|
|
|
this.logging.warn(`Registering duplicate authentication provider: ${name}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logging.verbose(`Registered authentication provider: ${name}`)
|
|
|
|
this.providers[name] = this.make(config[name].driver, name, config[name].config)
|
|
|
|
|
|
|
|
Route.group(`/auth/${name}`, () => {
|
|
|
|
this.providers[name].routes()
|
2022-03-29 06:14:46 +00:00
|
|
|
}).pre(middleware)
|
2022-02-23 21:15:02 +00:00
|
|
|
}
|
2021-06-05 18:24:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 20:32:25 +00:00
|
|
|
protected getMiddlewareResolver(): CanonicalResolver<StaticInstantiable<Middleware>> {
|
2021-06-05 18:24:12 +00:00
|
|
|
return (key: string) => {
|
|
|
|
return ({
|
|
|
|
required: AuthRequiredMiddleware,
|
|
|
|
guest: GuestRequiredMiddleware,
|
2021-11-26 20:32:25 +00:00
|
|
|
web: SessionAuthMiddleware,
|
2021-06-05 18:24:12 +00:00
|
|
|
})[key]
|
|
|
|
}
|
2021-06-05 17:02:36 +00:00
|
|
|
}
|
|
|
|
}
|