52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
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'
|
|
|
|
export interface AuthenticationConfig {
|
|
storage: Instantiable<AuthenticatableRepository>,
|
|
middleware?: Instantiable<Middleware>,
|
|
providers?: {
|
|
[key: string]: {
|
|
driver: Instantiable<LoginProvider<LoginProviderConfig>>,
|
|
config: LoginProviderConfig,
|
|
},
|
|
},
|
|
}
|
|
|
|
export function isAuthenticationConfig(what: unknown): what is AuthenticationConfig {
|
|
if ( typeof what !== 'object' || !what ) {
|
|
return false
|
|
}
|
|
|
|
if ( !hasOwnProperty(what, 'storage') || !hasOwnProperty(what, 'providers') ) {
|
|
return false
|
|
}
|
|
|
|
if ( !isInstantiable(what.storage) || !(what.storage.prototype instanceof AuthenticatableRepository) ) {
|
|
return false
|
|
}
|
|
|
|
if ( typeof what.providers !== 'object' ) {
|
|
return false
|
|
}
|
|
|
|
for ( const key in what.providers ) {
|
|
if ( !hasOwnProperty(what.providers, key) ) {
|
|
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) ) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|