feat(model): refactor Credential model and add Passkey credential store type

This commit is contained in:
Julien Oculi 2024-06-17 18:31:28 +02:00
parent f0899797d8
commit a5b31dd967

View file

@ -1,26 +1,29 @@
import type { Base64String, ToJson, UUID } from '@/types.ts' import type { Base64String, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
export class Credential extends Ressource { export class Credential<T extends CredentialCategory> extends Ressource {
static fromJSON( static fromJSON<T extends CredentialCategory>(
json: ToJson<Credential>, json: ToJson<Credential<T>>,
): Credential { ): Credential<T> {
return new Credential(json) return new Credential(json)
} }
static create( static create<T extends CredentialCategory>(
{ category, store, name }: Pick<Credential, 'category' | 'store' | 'name'>, { category, store, name }: Pick<
): Credential { Credential<T>,
'category' | 'store' | 'name'
>,
): Credential<T> {
const { uuid, createdAt, updatedAt } = super.create({ name }) const { uuid, createdAt, updatedAt } = super.create({ name })
return new Credential({ uuid, createdAt, updatedAt, name, category, store }) return new Credential({ uuid, createdAt, updatedAt, name, category, store })
} }
#category: 'password' | 'ssh' | 'passkey' #category: T
#store: CredentialCategory<Credential['category']> #store: Readonly<CredentialStore<T>>
private constructor( private constructor(
{ category, store, ...props }: { category, store, ...props }:
& Pick<Credential, 'category' | 'store'> & Pick<Credential<T>, 'category' | 'store'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>, & Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
) { ) {
super(props) super(props)
@ -43,14 +46,14 @@ export class Credential extends Ressource {
} }
get store() { get store() {
return Object.freeze(this.#store) return this.#store
} }
update( update<T extends CredentialCategory>(
props: Partial<Omit<Credential, 'type' | 'uuid' | 'createdAt'>>, props: Partial<Omit<Credential<T>, 'type' | 'uuid' | 'createdAt'>>,
): Credential { ): Credential<T> {
const { updatedAt } = super.update(props) const { updatedAt } = super.update(props)
const credential = new Credential({ ...this, ...props, updatedAt }) const credential = new Credential<T>({ ...this, ...props, updatedAt })
return credential return credential
} }
@ -68,14 +71,15 @@ export class Credential extends Ressource {
} }
} }
export interface Credential extends Ressource { export interface Credential<T extends CredentialCategory> extends Ressource {
type: 'credential' type: 'credential'
category: 'password' | 'ssh' | 'passkey' category: T
store: Readonly<CredentialCategory<Credential['category']>> store: Readonly<CredentialStore<T>>
} }
type CredentialCategory<T extends 'password' | 'ssh' | 'passkey'> = T extends type CredentialCategory = 'password' | 'ssh' | 'passkey'
'password' ? {
type CredentialStore<T extends CredentialCategory> = T extends 'password' ? {
store: { store: {
hash: Base64String hash: Base64String
alg: string alg: string