Start auth framework
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-06-05 12:02:36 -05:00
parent c264d45927
commit 91abcdf8ef
13 changed files with 656 additions and 5 deletions

View File

@@ -0,0 +1,41 @@
import {Authenticatable, AuthenticatableIdentifier, AuthenticatableRepository} from '../types'
import {Awaitable, Maybe} from '../../util'
import {ORMUser} from './ORMUser'
import {Singleton} from '../../di'
/**
* A user repository implementation that looks up users stored in the database.
*/
@Singleton()
export class ORMUserRepository extends AuthenticatableRepository {
/** Look up the user by their username. */
getByIdentifier(id: AuthenticatableIdentifier): Awaitable<Maybe<Authenticatable>> {
return ORMUser.query<ORMUser>()
.where('username', '=', id)
.first()
}
/**
* Try to look up a user by the credentials provided.
* If a securityIdentifier is specified, look up the user by username.
* If username/password are specified, look up the user and verify the password.
* @param credentials
*/
async getByCredentials(credentials: Record<string, string>): Promise<Maybe<Authenticatable>> {
if ( credentials.securityIdentifier ) {
return ORMUser.query<ORMUser>()
.where('username', '=', credentials.securityIdentifier)
.first()
}
if ( credentials.username && credentials.password ) {
const user = await ORMUser.query<ORMUser>()
.where('username', '=', credentials.username)
.first()
if ( user && await user.verifyPassword(credentials.password) ) {
return user
}
}
}
}