lib/src/auth/config.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-11-26 20:32:25 +00:00
import {Instantiable, isInstantiable} from '../di'
import {AuthenticatableRepository} from './types'
import {hasOwnProperty} from '../util'
import {LoginProvider, LoginProviderConfig} from './provider/LoginProvider'
import {Middleware} from '../http/routing/Middleware'
2021-11-26 20:32:25 +00:00
export interface AuthenticationConfig {
storage: Instantiable<AuthenticatableRepository>,
middleware?: Instantiable<Middleware>,
2022-01-20 06:55:21 +00:00
providers?: {
[key: string]: {
driver: Instantiable<LoginProvider<LoginProviderConfig>>,
config: LoginProviderConfig,
},
2021-10-18 17:48:16 +00:00
},
}
2021-11-26 20:32:25 +00:00
export function isAuthenticationConfig(what: unknown): what is AuthenticationConfig {
if ( typeof what !== 'object' || !what ) {
return false
}
if ( !hasOwnProperty(what, 'storage') || !hasOwnProperty(what, 'providers') ) {
2021-11-26 20:32:25 +00:00
return false
}
if ( !isInstantiable(what.storage) || !(what.storage.prototype instanceof AuthenticatableRepository) ) {
return false
}
if ( typeof what.providers !== 'object' ) {
2021-11-26 20:32:25 +00:00
return false
}
for ( const key in what.providers ) {
if ( !hasOwnProperty(what.providers, key) ) {
2021-11-26 20:32:25 +00:00
continue
}
const source = what.providers[key]
if ( typeof source !== 'object' || source === null || !hasOwnProperty(source, 'driver') ) {
return false
}
if ( !isInstantiable(source.driver) || !(source.driver.prototype instanceof LoginProvider) ) {
2021-11-26 20:32:25 +00:00
return false
}
}
return true
}