Rework authentication system
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-11-26 14:32:25 -06:00
parent bd7d6a2dbd
commit 5175d64e36
28 changed files with 372 additions and 720 deletions

View File

@@ -1,29 +1,41 @@
import {Instantiable} from '../di'
import {ORMUserRepository} from './orm/ORMUserRepository'
import {OAuth2LoginConfig} from './external/oauth2/OAuth2LoginController'
import {Instantiable, isInstantiable} from '../di'
import {AuthenticatableRepository} from './types'
import {hasOwnProperty} from '../util'
/**
* Inferface for type-checking the AuthenticatableRepositories values.
*/
export interface AuthenticatableRepositoryMapping {
orm: Instantiable<ORMUserRepository>,
}
/**
* String mapping of AuthenticatableRepository implementations.
*/
export const AuthenticatableRepositories: AuthenticatableRepositoryMapping = {
orm: ORMUserRepository,
}
/**
* Interface for making the auth config type-safe.
*/
export interface AuthConfig {
repositories: {
session: keyof AuthenticatableRepositoryMapping,
},
export interface AuthenticationConfig {
storage: Instantiable<AuthenticatableRepository>,
sources?: {
[key: string]: OAuth2LoginConfig,
[key: string]: Instantiable<AuthenticatableRepository>,
},
}
export function isAuthenticationConfig(what: unknown): what is AuthenticationConfig {
if ( typeof what !== 'object' || !what ) {
return false
}
if ( !hasOwnProperty(what, 'storage') || !hasOwnProperty(what, 'sources') ) {
return false
}
if ( !isInstantiable(what.storage) || !(what.storage.prototype instanceof AuthenticatableRepository) ) {
return false
}
if ( typeof what.sources !== 'object' ) {
return false
}
for ( const key in what.sources ) {
if ( !hasOwnProperty(what.sources, key) ) {
continue
}
const source = what.sources[key]
if ( !isInstantiable(source) || !(source.prototype instanceof AuthenticatableRepository) ) {
return false
}
}
return true
}