feat(auth): add authenticator challenges model

This commit is contained in:
Karol Sójko
2022-12-28 12:40:13 +01:00
parent 305190b64e
commit fa9bf0b448
6 changed files with 71 additions and 0 deletions
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
export class addAuthenticatorChallenges1672227471677 implements MigrationInterface {
name = 'addAuthenticatorChallenges1672227471677'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
'CREATE TABLE `authenticator_challenges` (`uuid` varchar(36) NOT NULL, `user_uuid` varchar(36) NOT NULL, `challenge` varchar(255) NOT NULL, `created_at` bigint NOT NULL, INDEX `user_uuid_and_challenge` (`user_uuid`, `challenge`), PRIMARY KEY (`uuid`)) ENGINE=InnoDB',
)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('DROP INDEX `user_uuid_and_challenge` ON `authenticator_challenges`')
await queryRunner.query('DROP TABLE `authenticator_challenges`')
}
}
@@ -11,6 +11,7 @@ import { OfflineUserSubscription } from '../Domain/Subscription/OfflineUserSubsc
import { UserSubscription } from '../Domain/Subscription/UserSubscription'
import { User } from '../Domain/User/User'
import { TypeORMAuthenticator } from '../Infra/TypeORM/TypeORMAuthenticator'
import { TypeORMAuthenticatorChallenge } from '../Infra/TypeORM/TypeORMAuthenticatorChallenge'
import { TypeORMSessionTrace } from '../Infra/TypeORM/TypeORMSessionTrace'
import { Env } from './Env'
@@ -60,6 +61,7 @@ export const AppDataSource = new DataSource({
SubscriptionSetting,
TypeORMSessionTrace,
TypeORMAuthenticator,
TypeORMAuthenticatorChallenge,
],
migrations: [env.get('DB_MIGRATIONS_PATH', true) ?? 'dist/migrations/*.js'],
migrationsRun: true,
@@ -0,0 +1,17 @@
import { Entity, Result, UniqueEntityId } from '@standardnotes/domain-core'
import { AuthenticatorChallengeProps } from './AuthenticatorChallengeProps'
export class AuthenticatorChallenge extends Entity<AuthenticatorChallengeProps> {
get id(): UniqueEntityId {
return this._id
}
private constructor(props: AuthenticatorChallengeProps, id?: UniqueEntityId) {
super(props, id)
}
static create(props: AuthenticatorChallengeProps, id?: UniqueEntityId): Result<AuthenticatorChallenge> {
return Result.ok<AuthenticatorChallenge>(new AuthenticatorChallenge(props, id))
}
}
@@ -0,0 +1,6 @@
import { Uuid } from '@standardnotes/domain-core'
export interface AuthenticatorChallengeProps {
userUuid: Uuid
challenge: string
}
@@ -1,4 +1,7 @@
import { Uuid } from '@standardnotes/domain-core'
export interface AuthenticatorProps {
userUuid: Uuid
credentialId: Buffer
credentialPublicKey: Buffer
counter: number
@@ -0,0 +1,27 @@
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'
@Entity({ name: 'authenticator_challenges' })
@Index('user_uuid_and_challenge', ['userUuid', 'challenge'])
export class TypeORMAuthenticatorChallenge {
@PrimaryGeneratedColumn('uuid')
declare uuid: string
@Column({
name: 'user_uuid',
length: 36,
})
declare userUuid: string
@Column({
name: 'challenge',
type: 'varchar',
length: 255,
})
declare challenge: Buffer
@Column({
name: 'created_at',
type: 'bigint',
})
declare createdAt: number
}