refactor(model): ♻️ update references to credentials

This commit is contained in:
Julien Oculi 2024-06-17 18:38:57 +02:00
parent a5b31dd967
commit 9520d222ae
2 changed files with 14 additions and 14 deletions

View file

@ -77,9 +77,10 @@ export interface Credential<T extends CredentialCategory> extends Ressource {
store: Readonly<CredentialStore<T>> store: Readonly<CredentialStore<T>>
} }
type CredentialCategory = 'password' | 'ssh' | 'passkey' export type CredentialCategory = 'password' | 'ssh' | 'passkey'
type CredentialStore<T extends CredentialCategory> = T extends 'password' ? { export type CredentialStore<T extends CredentialCategory> = T extends 'password'
? {
store: { store: {
hash: Base64String hash: Base64String
alg: string alg: string
@ -97,7 +98,7 @@ type CredentialStore<T extends CredentialCategory> = T extends 'password' ? {
: never : never
/** Passkey store */ /** Passkey store */
type Passkey = { export type Passkey = {
/** User UUID */ /** User UUID */
user: UUID user: UUID
/** WebAuthn registration key id */ /** WebAuthn registration key id */

View file

@ -1,7 +1,7 @@
import { Ref } from '@/src/models/utils/ref.ts' import { Ref } from '@/src/models/utils/ref.ts'
import type { Login, MailAddress, Posix, ToJson } from '@/types.ts' import type { Login, MailAddress, Posix, ToJson, UrlString } from '@/types.ts'
import { regex, toLogin } from '@/utils.ts' import { regex, toLogin } from '@/utils.ts'
import type { Credential } from '@models/credential.ts' import type { Credential, CredentialCategory } from '@models/credential.ts'
import type { Group } from '@models/group.ts' import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
@ -35,16 +35,15 @@ export class User extends Ressource {
const credentials = Object.freeze( const credentials = Object.freeze(
json.credentials.map((credential) => json.credentials.map((credential) =>
Ref.fromString<Credential>(credential) Ref.fromString<Credential<CredentialCategory>>(credential)
), ),
) )
const groups = Object.freeze( const groups = Object.freeze(
json.groups.map((group) => Ref.fromString<Group>(group)), json.groups.map((group) => Ref.fromString<Group>(group)),
) )
const avatar = new URL(json.avatar)
const posix = json.posix ?? undefined const posix = json.posix ?? undefined
return new User({ ...json, posix, credentials, groups, avatar }) return new User({ ...json, posix, credentials, groups })
} }
static create({ static create({
@ -71,8 +70,8 @@ export class User extends Ressource {
#mail: MailAddress #mail: MailAddress
#groups: readonly Ref<Group>[] #groups: readonly Ref<Group>[]
#posix?: Posix #posix?: Posix
#avatar: URL #avatar: UrlString
#credentials: readonly Ref<Credential>[] #credentials: readonly Ref<Credential<CredentialCategory>>[]
private constructor({ private constructor({
lastname, lastname,
@ -113,7 +112,7 @@ export class User extends Ressource {
} }
} }
this.#posix = posix this.#posix = posix
this.#avatar = avatar this.#avatar = new URL(avatar).href as UrlString
this.#login = toLogin({ firstname, lastname }) this.#login = toLogin({ firstname, lastname })
this.#credentials = Object.freeze(credentials) this.#credentials = Object.freeze(credentials)
} }
@ -161,7 +160,7 @@ export class User extends Ressource {
mail: this.mail, mail: this.mail,
groups: this.groups.map((group) => group.toJSON()), groups: this.groups.map((group) => group.toJSON()),
posix: this.posix ?? null, posix: this.posix ?? null,
avatar: this.avatar.toJSON(), avatar: this.avatar,
credentials: this.credentials.map((credential) => credential.toJSON()), credentials: this.credentials.map((credential) => credential.toJSON()),
} as const } as const
} }
@ -179,6 +178,6 @@ export interface User extends Ressource {
mail: MailAddress mail: MailAddress
groups: readonly Ref<Group>[] groups: readonly Ref<Group>[]
posix: Posix | undefined posix: Posix | undefined
avatar: URL avatar: UrlString
credentials: readonly Ref<Credential>[] credentials: readonly Ref<Credential<CredentialCategory>>[]
} }