|
|
|
import {Awaitable, JSONState, Maybe, Rehydratable} from '../util'
|
|
|
|
|
|
|
|
/** Value that can be used to uniquely identify a user. */
|
|
|
|
export type AuthenticatableIdentifier = string | number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for entities that can be authenticated.
|
|
|
|
*/
|
|
|
|
export abstract class Authenticatable implements Rehydratable {
|
|
|
|
|
|
|
|
/** Get the globally-unique identifier of the user. */
|
|
|
|
abstract getUniqueIdentifier(): AuthenticatableIdentifier
|
|
|
|
|
|
|
|
/** Get the repository-unique identifier of the user. */
|
|
|
|
abstract getIdentifier(): AuthenticatableIdentifier
|
|
|
|
|
|
|
|
/** Get the human-readable identifier of the user. */
|
|
|
|
abstract getDisplay(): string
|
|
|
|
|
|
|
|
/** Attempt to validate a credential of the user. */
|
|
|
|
abstract validateCredential(credential: string): Awaitable<boolean>
|
|
|
|
|
|
|
|
abstract dehydrate(): Promise<JSONState>
|
|
|
|
|
|
|
|
abstract rehydrate(state: JSONState): Awaitable<void>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for a repository that stores and recalls users.
|
|
|
|
*/
|
|
|
|
export abstract class AuthenticatableRepository {
|
|
|
|
/** Look up the user by their unique identifier. */
|
|
|
|
abstract getByIdentifier(id: AuthenticatableIdentifier): Awaitable<Maybe<Authenticatable>>
|
|
|
|
|
|
|
|
/** Returns true if this repository supports registering users. */
|
|
|
|
abstract supportsRegistration(): boolean
|
|
|
|
|
|
|
|
/** Create a user in this repository from an external Authenticatable instance. */
|
|
|
|
abstract createFromExternal(user: Authenticatable): Awaitable<Authenticatable>
|
|
|
|
|
|
|
|
/** Create a user in this repository from basic credentials. */
|
|
|
|
abstract createFromCredentials(username: string, password: string): Awaitable<Authenticatable>
|
|
|
|
}
|