lib/src/auth/types.ts

44 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-06-05 17:02:36 +00:00
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 {
2021-11-26 20:32:25 +00:00
/** Get the globally-unique identifier of the user. */
abstract getUniqueIdentifier(): AuthenticatableIdentifier
/** Get the repository-unique identifier of the user. */
2021-06-05 17:02:36 +00:00
abstract getIdentifier(): AuthenticatableIdentifier
/** Get the human-readable identifier of the user. */
2021-11-26 20:32:25 +00:00
abstract getDisplay(): string
/** Attempt to validate a credential of the user. */
abstract validateCredential(credential: string): Awaitable<boolean>
2021-06-05 17:02:36 +00:00
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>>
2021-11-26 20:32:25 +00:00
/** 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>
2021-11-26 20:32:25 +00:00
/** Create a user in this repository from basic credentials. */
abstract createFromCredentials(username: string, password: string): Awaitable<Authenticatable>
2021-06-05 17:02:36 +00:00
}