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>>
}
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: {
hash: Base64String
alg: string
@ -97,7 +98,7 @@ type CredentialStore<T extends CredentialCategory> = T extends 'password' ? {
: never
/** Passkey store */
type Passkey = {
export type Passkey = {
/** User UUID */
user: UUID
/** WebAuthn registration key id */

View file

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