Compare commits
No commits in common. "28a517323b2b5d28c7e9b6ac67de79262247ee8e" and "dda72bf67d4bb8a88ea018c4f4d0bc86408faf73" have entirely different histories.
28a517323b
...
dda72bf67d
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,8 +1,6 @@
|
|||
# Directories
|
||||
docs
|
||||
tmp
|
||||
temp
|
||||
|
||||
# Files
|
||||
.env
|
||||
*.sqlite*
|
||||
*.sqlite
|
||||
|
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"conventionalCommits.scopes": ["model", "chore", "db"]
|
||||
}
|
|
@ -1,10 +1,5 @@
|
|||
{
|
||||
"name": "@cohabit/ressources",
|
||||
"version": "0.1.0",
|
||||
"exports": {
|
||||
".": "./mod.ts",
|
||||
"./models": "./src/models/mod.ts"
|
||||
},
|
||||
"tasks": {
|
||||
"start": "deno run --unstable-kv ./mod.ts"
|
||||
},
|
||||
|
|
|
@ -1,12 +1,4 @@
|
|||
import {
|
||||
Credential,
|
||||
Group,
|
||||
Machine,
|
||||
type Ressource,
|
||||
Service,
|
||||
User,
|
||||
} from '@models'
|
||||
import type { CredentialCategory } from '@models/credential.ts'
|
||||
import { Credential, Group, Machine, Ressource, Service, User } from '@models'
|
||||
|
||||
//!TODO link ressources (get, list)
|
||||
//!TODO Purge unused ressources (delete)
|
||||
|
@ -34,10 +26,7 @@ export class Db {
|
|||
|
||||
get ressource() {
|
||||
return {
|
||||
credential: this.#ressourceStorage<Credential<CredentialCategory>>(
|
||||
'credential',
|
||||
Credential,
|
||||
),
|
||||
credential: this.#ressourceStorage<Credential>('credential', Credential),
|
||||
group: this.#ressourceStorage<Group>('group', Group),
|
||||
machine: this.#ressourceStorage<Machine>('machine', Machine),
|
||||
service: this.#ressourceStorage<Service>('service', Service),
|
||||
|
@ -55,9 +44,8 @@ export class Db {
|
|||
set: (ressources: T[]) => this.#set<T>(type, ressources),
|
||||
delete: (ressources: Pick<T, 'uuid'>[]) =>
|
||||
this.#delete<T>(type, ressources),
|
||||
list: (
|
||||
filter: (ressource: T) => boolean | Promise<boolean> = () => true,
|
||||
) => this.#list<T>(type, Builder, filter),
|
||||
list: (filter: (ressource: T) => boolean = () => true) =>
|
||||
this.#list<T>(type, Builder, filter),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,7 +94,7 @@ export class Db {
|
|||
async *#list<T extends Ressource>(
|
||||
type: RessourceType<T>,
|
||||
Builder: RessourceBuilder<T>,
|
||||
filter: (entry: T) => boolean | Promise<boolean>,
|
||||
filter: (entry: T) => boolean,
|
||||
): AsyncGenerator<T, void, void> {
|
||||
const list = this.#kv.list<RessourceJson<T>>({
|
||||
prefix: [this.prefix.ressource, type],
|
||||
|
@ -115,23 +103,22 @@ export class Db {
|
|||
const value = entry.value
|
||||
//@ts-expect-error Type union of Ressource types for Builder
|
||||
const ressource = Builder.fromJSON(value) as T
|
||||
if (await filter(ressource)) {
|
||||
if (filter(ressource)) {
|
||||
yield ressource
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type RessourceType<T extends Ressource> = T extends
|
||||
Credential<CredentialCategory> ? 'credential'
|
||||
type RessourceType<T extends Ressource> = T extends Credential ? 'credential'
|
||||
: T extends Group ? 'group'
|
||||
: T extends Machine ? 'machine'
|
||||
: T extends Service ? 'service'
|
||||
: T extends User ? 'user'
|
||||
: never
|
||||
|
||||
type RessourceBuilder<T extends Ressource> = T extends
|
||||
Credential<CredentialCategory> ? typeof Credential
|
||||
type RessourceBuilder<T extends Ressource> = T extends Credential
|
||||
? typeof Credential
|
||||
: T extends Group ? typeof Group
|
||||
: T extends Machine ? typeof Machine
|
||||
: T extends Service ? typeof Service
|
||||
|
|
|
@ -1,54 +1,27 @@
|
|||
import type { Base64String, ToJson, UUID } from '@/types.ts'
|
||||
import { ToJson } from '@/types.ts'
|
||||
import { Ressource } from '@models/ressource.ts'
|
||||
import type { Ref } from '@models'
|
||||
import { Avatar } from '@/src/models/utils/avatar.ts'
|
||||
|
||||
export class Credential<T extends CredentialCategory> extends Ressource {
|
||||
static fromJSON<T extends CredentialCategory>(
|
||||
json: ToJson<Credential<T>>,
|
||||
): Credential<T> {
|
||||
export class Credential extends Ressource {
|
||||
static fromJSON(
|
||||
json: ToJson<Credential>,
|
||||
): Credential {
|
||||
return new Credential(json)
|
||||
}
|
||||
|
||||
static create<T extends CredentialCategory>(
|
||||
{ category, store, name, avatar }: Pick<
|
||||
Credential<T>,
|
||||
'category' | 'store' | 'name' | 'avatar'
|
||||
>,
|
||||
): Credential<T> {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
|
||||
return new Credential({
|
||||
uuid,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
name,
|
||||
category,
|
||||
store,
|
||||
avatar,
|
||||
})
|
||||
static create(
|
||||
{ category, store, name }: Pick<Credential, 'category' | 'store' | 'name'>,
|
||||
): Credential {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name })
|
||||
return new Credential({ uuid, createdAt, updatedAt, name, category, store })
|
||||
}
|
||||
|
||||
static load<T extends CredentialCategory>({
|
||||
category,
|
||||
store,
|
||||
name,
|
||||
avatar = Avatar.fromEmoji('🔑'),
|
||||
}: {
|
||||
name: Credential<T>['name']
|
||||
category: T
|
||||
store: Credential<T>['store']
|
||||
avatar?: Credential<T>['avatar']
|
||||
}): Credential<T> {
|
||||
return this.create({ category, store, name, avatar })
|
||||
}
|
||||
|
||||
#category: T
|
||||
#store: Readonly<CredentialStore<T>>
|
||||
#category: 'password' | 'ssh' | 'passkey'
|
||||
#store: CredentialCategory<Credential['category']>
|
||||
|
||||
private constructor(
|
||||
{ category, store, ...props }:
|
||||
& Pick<Credential<T>, 'category' | 'store'>
|
||||
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
|
||||
& Pick<Credential, 'category' | 'store'>
|
||||
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
|
||||
) {
|
||||
super(props)
|
||||
|
||||
|
@ -70,14 +43,14 @@ export class Credential<T extends CredentialCategory> extends Ressource {
|
|||
}
|
||||
|
||||
get store() {
|
||||
return this.#store
|
||||
return Object.freeze(this.#store)
|
||||
}
|
||||
|
||||
update<T extends CredentialCategory>(
|
||||
props: Partial<Omit<Credential<T>, 'type' | 'uuid' | 'createdAt'>>,
|
||||
): Credential<T> {
|
||||
update(
|
||||
props: Partial<Omit<Credential, 'type' | 'uuid' | 'createdAt'>>,
|
||||
): Credential {
|
||||
const { updatedAt } = super.update(props)
|
||||
const credential = new Credential<T>({ ...this, ...props, updatedAt })
|
||||
const credential = new Credential({ ...this, ...props, updatedAt })
|
||||
return credential
|
||||
}
|
||||
|
||||
|
@ -93,55 +66,40 @@ export class Credential<T extends CredentialCategory> extends Ressource {
|
|||
toString(): string {
|
||||
return `Credential (${JSON.stringify(this)})`
|
||||
}
|
||||
|
||||
toRef(): Ref<Credential<T>> {
|
||||
return super.toRef() as Ref<Credential<T>>
|
||||
}
|
||||
}
|
||||
|
||||
export interface Credential<T extends CredentialCategory> extends Ressource {
|
||||
export interface Credential extends Ressource {
|
||||
type: 'credential'
|
||||
category: T
|
||||
store: Readonly<CredentialStore<T>>
|
||||
category: 'password' | 'ssh' | 'passkey'
|
||||
store: Readonly<CredentialCategory<Credential['category']>>
|
||||
}
|
||||
|
||||
export type CredentialCategory = 'password' | 'ssh' | 'passkey'
|
||||
|
||||
export type CredentialStore<T extends CredentialCategory> = T extends 'password'
|
||||
? {
|
||||
type CredentialCategory<T extends 'password' | 'ssh' | 'passkey'> = T extends
|
||||
'password' ? {
|
||||
store: {
|
||||
hash: Base64String
|
||||
hash: string //hex or b64 of Uint8Array
|
||||
alg: string
|
||||
salt: Base64String
|
||||
salt: string //hex or b64 of Uint8Array
|
||||
}
|
||||
}
|
||||
: T extends 'ssh' ? {
|
||||
store: {
|
||||
publicKey: Base64String
|
||||
publicKey: string
|
||||
}
|
||||
}
|
||||
: T extends 'passkey' ? {
|
||||
store: Passkey
|
||||
store: Record<string, unknown>
|
||||
}
|
||||
: never
|
||||
|
||||
/** Passkey store */
|
||||
export type Passkey = {
|
||||
/** User UUID */
|
||||
user: UUID
|
||||
/** WebAuthn registration key id */
|
||||
webAuthnUserID: string
|
||||
/** Passkey credential unique id */
|
||||
id: string
|
||||
/** Passkey user public key */
|
||||
publicKey: Base64String
|
||||
/** Number of times the authenticator has been used */
|
||||
/*
|
||||
PassKey store:
|
||||
{
|
||||
publicKey: Uint8Array
|
||||
keyId: string
|
||||
transport: string
|
||||
counter: number
|
||||
/** Whether the passkey is single-device or multi-device */
|
||||
deviceType: 'singleDevice' | 'multiDevice'
|
||||
/** Whether the passkey has been backed up in some way */
|
||||
backedUp: boolean
|
||||
/** Passkey physical transport layer */
|
||||
transports?:
|
||||
('ble' | 'cable' | 'hybrid' | 'internal' | 'nfc' | 'smart-card' | 'usb')[]
|
||||
}
|
||||
*/
|
||||
|
||||
//new Uint8Array(Object.values(JSON.parse(JSON.stringify(new Uint8Array([1, 2, 3])))))
|
||||
|
|
|
@ -1,68 +1,33 @@
|
|||
import type { Posix, ToJson, UUID } from '@/types.ts'
|
||||
import { Posix, ToJson, UUID } from '@/types.ts'
|
||||
import { Ressource } from '@models/ressource.ts'
|
||||
import { Ref } from '@models'
|
||||
import { Avatar } from '@/src/models/utils/avatar.ts'
|
||||
|
||||
export class Group extends Ressource {
|
||||
static fromJSON(
|
||||
{ posix, groups, ...json }: ToJson<Group>,
|
||||
{ posix, ...json }: ToJson<Group>,
|
||||
): Group {
|
||||
return new Group({
|
||||
...json,
|
||||
posix: posix ?? undefined,
|
||||
groups: groups.map((group) => Ref.fromString<Group>(group)),
|
||||
})
|
||||
return new Group({ ...json, posix: posix ?? undefined })
|
||||
}
|
||||
|
||||
static create(
|
||||
{ posix, permissions, name, avatar, groups }: Pick<
|
||||
Group,
|
||||
'posix' | 'permissions' | 'name' | 'avatar' | 'groups'
|
||||
>,
|
||||
{ posix, permissions, name }: Pick<Group, 'posix' | 'permissions' | 'name'>,
|
||||
): Group {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
|
||||
return new Group({
|
||||
uuid,
|
||||
avatar,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
name,
|
||||
posix,
|
||||
permissions,
|
||||
groups,
|
||||
})
|
||||
}
|
||||
|
||||
static load({
|
||||
name,
|
||||
groups = [],
|
||||
permissions = {},
|
||||
posix,
|
||||
avatar = Avatar.fromEmoji('👥'),
|
||||
}: {
|
||||
name: Group['name']
|
||||
permissions?: Group['permissions']
|
||||
posix?: Group['posix']
|
||||
avatar?: Group['avatar']
|
||||
groups?: Group['groups']
|
||||
}): Group {
|
||||
return this.create({ name, groups, permissions, posix, avatar })
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name })
|
||||
return new Group({ uuid, createdAt, updatedAt, name, posix, permissions })
|
||||
}
|
||||
|
||||
#posix?: Posix
|
||||
#permissions: Readonly<{
|
||||
#permissions: {
|
||||
[serviceOrMachine: UUID]: {
|
||||
read: boolean
|
||||
write: boolean
|
||||
execute: boolean
|
||||
}
|
||||
}>
|
||||
#groups: readonly Ref<Group>[]
|
||||
} = {}
|
||||
|
||||
private constructor(
|
||||
{ posix, permissions, groups, ...props }:
|
||||
& Pick<Group, 'posix' | 'permissions' | 'groups'>
|
||||
& Pick<Ressource, 'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'>,
|
||||
{ posix, permissions, ...props }:
|
||||
& Pick<Group, 'posix' | 'permissions'>
|
||||
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
|
||||
) {
|
||||
super(props)
|
||||
|
||||
|
@ -85,8 +50,7 @@ export class Group extends Ressource {
|
|||
this.#posix = posix
|
||||
}
|
||||
|
||||
this.#permissions = Object.freeze(permissions)
|
||||
this.#groups = Object.freeze(groups)
|
||||
this.permissions = Object.freeze(permissions)
|
||||
}
|
||||
|
||||
get type(): 'group' {
|
||||
|
@ -98,11 +62,7 @@ export class Group extends Ressource {
|
|||
}
|
||||
|
||||
get permissions() {
|
||||
return this.#permissions
|
||||
}
|
||||
|
||||
get groups() {
|
||||
return this.#groups
|
||||
return Object.freeze(this.#permissions)
|
||||
}
|
||||
|
||||
update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group {
|
||||
|
@ -117,23 +77,17 @@ export class Group extends Ressource {
|
|||
type: this.type,
|
||||
posix: this.posix ?? null,
|
||||
permissions: this.permissions,
|
||||
groups: this.groups.map((group) => group.toJSON()),
|
||||
} as const
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `Group (${JSON.stringify(this)})`
|
||||
}
|
||||
|
||||
toRef(): Ref<Group> {
|
||||
return super.toRef() as Ref<Group>
|
||||
}
|
||||
}
|
||||
|
||||
export interface Group extends Ressource {
|
||||
type: 'group'
|
||||
posix: Posix | undefined
|
||||
groups: readonly Ref<Group>[]
|
||||
permissions: Readonly<{
|
||||
[serviceOrMachine: UUID]: {
|
||||
read: boolean
|
||||
|
|
|
@ -1,58 +1,29 @@
|
|||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
import type { ToJson, UrlString } from '@/types.ts'
|
||||
import type { Group } from '@models/group.ts'
|
||||
import { ToJson } from '@/types.ts'
|
||||
import { Group } from '@models/group.ts'
|
||||
import { Ressource } from '@models/ressource.ts'
|
||||
import { Avatar } from '@/src/models/utils/avatar.ts'
|
||||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
|
||||
export class Machine extends Ressource {
|
||||
static fromJSON(
|
||||
{ url, ...json }: ToJson<Machine>,
|
||||
json: ToJson<Machine>,
|
||||
): Machine {
|
||||
const url = new URL(json.url)
|
||||
const avatar = new URL(json.avatar)
|
||||
const groups = json.groups.map((group) => Ref.fromString<Group>(group))
|
||||
|
||||
return new Machine({ ...json, url, groups })
|
||||
return new Machine({ ...json, url, avatar, groups })
|
||||
}
|
||||
|
||||
static create(
|
||||
{ tags, url, status, groups, avatar }: Pick<
|
||||
Machine,
|
||||
'name' | 'tags' | 'url' | 'status' | 'avatar' | 'groups'
|
||||
>,
|
||||
{ tags, url, status, avatar, groups, ...props }:
|
||||
& Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'>
|
||||
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
|
||||
): Machine {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
|
||||
return new Machine({
|
||||
uuid,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
avatar,
|
||||
name,
|
||||
tags,
|
||||
url,
|
||||
status,
|
||||
groups,
|
||||
})
|
||||
}
|
||||
|
||||
static load({
|
||||
name,
|
||||
avatar = Avatar.fromEmoji('🖨️'),
|
||||
tags = [],
|
||||
url,
|
||||
status = 'unknown',
|
||||
groups = [],
|
||||
}: {
|
||||
name: Machine['name']
|
||||
avatar?: Machine['avatar']
|
||||
tags?: Machine['tags']
|
||||
url: Machine['url']
|
||||
status?: Machine['status']
|
||||
groups?: Machine['groups']
|
||||
}): Machine {
|
||||
return this.create({ name, avatar, tags, url, status, groups })
|
||||
return new Machine({ ...props, tags, url, status, avatar, groups })
|
||||
}
|
||||
|
||||
#tags: readonly string[]
|
||||
#url: UrlString
|
||||
#url: URL
|
||||
#status:
|
||||
| 'ready'
|
||||
| 'busy'
|
||||
|
@ -60,23 +31,25 @@ export class Machine extends Ressource {
|
|||
| 'discontinued'
|
||||
| 'error'
|
||||
| 'unknown'
|
||||
#avatar: URL
|
||||
#groups: readonly Ref<Group>[]
|
||||
|
||||
private constructor(
|
||||
{ tags, url, status, groups, ...props }:
|
||||
{ tags, url, status, avatar, groups, ...props }:
|
||||
& Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'>
|
||||
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
|
||||
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
|
||||
) {
|
||||
super(props)
|
||||
|
||||
this.#tags = Object.freeze(tags)
|
||||
this.#url = new URL(url).href as UrlString
|
||||
this.#url = new URL(url)
|
||||
if (!['working', 'pending', 'ready', 'unavailable'].includes(status)) {
|
||||
throw new TypeError(
|
||||
`status is "${status}" but ('ready' | 'busy' | 'unavailable' | 'discontinued' | 'error' | 'unknown') is required`,
|
||||
)
|
||||
}
|
||||
this.#status = status
|
||||
this.#avatar = new URL(avatar)
|
||||
this.#groups = Object.freeze(groups)
|
||||
}
|
||||
|
||||
|
@ -84,14 +57,17 @@ export class Machine extends Ressource {
|
|||
return 'machine'
|
||||
}
|
||||
get tags() {
|
||||
return this.#tags
|
||||
return this.#tags.slice()
|
||||
}
|
||||
get url() {
|
||||
return this.#url
|
||||
return new URL(this.#url)
|
||||
}
|
||||
get status() {
|
||||
return this.#status
|
||||
}
|
||||
get avatar() {
|
||||
return new URL(this.#avatar)
|
||||
}
|
||||
get groups() {
|
||||
return this.#groups
|
||||
}
|
||||
|
@ -108,9 +84,10 @@ export class Machine extends Ressource {
|
|||
return {
|
||||
...super.toJSON(),
|
||||
type: this.type,
|
||||
tags: this.tags,
|
||||
url: this.url,
|
||||
tags: this.tags.slice(),
|
||||
url: this.url.toJSON(),
|
||||
status: this.status,
|
||||
avatar: this.avatar.toJSON(),
|
||||
groups: Object.freeze(this.groups.map((group) => group.toJSON())),
|
||||
} as const
|
||||
}
|
||||
|
@ -118,16 +95,12 @@ export class Machine extends Ressource {
|
|||
toString(): string {
|
||||
return `Machine (${JSON.stringify(this)})`
|
||||
}
|
||||
|
||||
toRef(): Ref<Machine> {
|
||||
return super.toRef() as Ref<Machine>
|
||||
}
|
||||
}
|
||||
|
||||
export interface Machine extends Ressource {
|
||||
type: 'machine'
|
||||
tags: readonly string[]
|
||||
url: UrlString
|
||||
tags: string[]
|
||||
url: URL
|
||||
status:
|
||||
| 'ready'
|
||||
| 'busy'
|
||||
|
@ -135,5 +108,6 @@ export interface Machine extends Ressource {
|
|||
| 'discontinued'
|
||||
| 'error'
|
||||
| 'unknown'
|
||||
avatar: URL
|
||||
groups: readonly Ref<Group>[]
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
import type { DateString, ToJson, UrlString, UUID } from '@/types.ts'
|
||||
import { DateString, ToJson, UUID } from '@/types.ts'
|
||||
import { regex } from '@/utils.ts'
|
||||
|
||||
export class Ressource {
|
||||
|
@ -8,37 +8,29 @@ export class Ressource {
|
|||
): Ressource {
|
||||
return new Ressource({
|
||||
name: json.name,
|
||||
avatar: json.avatar,
|
||||
uuid: json.uuid,
|
||||
createdAt: json.createdAt,
|
||||
updatedAt: json.updatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
static create(
|
||||
{ name, avatar }: Pick<Ressource, 'name' | 'avatar'>,
|
||||
): Ressource {
|
||||
static create({ name }: Pick<Ressource, 'name'>): Ressource {
|
||||
const uuid = crypto.randomUUID() as UUID
|
||||
const createdAt = new Date().toISOString() as DateString
|
||||
const updatedAt = createdAt
|
||||
|
||||
return new Ressource({ name, avatar, uuid, createdAt, updatedAt })
|
||||
}
|
||||
|
||||
static load(_options: never): Ressource {
|
||||
throw new Error('not implemented')
|
||||
return new Ressource({ name, uuid, createdAt, updatedAt })
|
||||
}
|
||||
|
||||
#name: string
|
||||
#uuid: UUID
|
||||
#createdAt: DateString
|
||||
#updatedAt: DateString
|
||||
#avatar: UrlString
|
||||
|
||||
protected constructor(
|
||||
{ name, avatar, uuid, createdAt, updatedAt }: Pick<
|
||||
{ name, uuid, createdAt, updatedAt }: Pick<
|
||||
Ressource,
|
||||
'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'
|
||||
'name' | 'uuid' | 'createdAt' | 'updatedAt'
|
||||
>,
|
||||
) {
|
||||
this.#name = name
|
||||
|
@ -47,7 +39,6 @@ export class Ressource {
|
|||
`the following uuid "${uuid}" is not a valid UUID V4`,
|
||||
)
|
||||
}
|
||||
this.#avatar = new URL(avatar).href as UrlString
|
||||
this.#uuid = uuid
|
||||
if (!regex.isoDateString.test(createdAt)) {
|
||||
throw new SyntaxError(
|
||||
|
@ -72,9 +63,6 @@ export class Ressource {
|
|||
get name(): string {
|
||||
return this.#name
|
||||
}
|
||||
get avatar(): UrlString {
|
||||
return this.#avatar
|
||||
}
|
||||
get createdAt(): DateString {
|
||||
return this.#createdAt
|
||||
}
|
||||
|
@ -95,7 +83,6 @@ export class Ressource {
|
|||
type: this.type,
|
||||
uuid: this.uuid,
|
||||
name: this.name,
|
||||
avatar: this.avatar,
|
||||
createdAt: this.createdAt,
|
||||
updatedAt: this.updatedAt,
|
||||
} as const
|
||||
|
@ -114,7 +101,6 @@ export interface Ressource {
|
|||
type: 'user' | 'machine' | 'service' | 'group' | 'credential'
|
||||
uuid: UUID
|
||||
name: string
|
||||
avatar: UrlString
|
||||
createdAt: DateString
|
||||
updatedAt: DateString
|
||||
}
|
||||
|
|
|
@ -1,26 +1,27 @@
|
|||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
import type { ToJson, UrlString } from '@/types.ts'
|
||||
import type { Group } from '@models/group.ts'
|
||||
import { ToJson } from '@/types.ts'
|
||||
import { Group } from '@models/group.ts'
|
||||
import { Ressource } from '@models/ressource.ts'
|
||||
import { Avatar } from '@/src/models/utils/avatar.ts'
|
||||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
|
||||
export class Service extends Ressource {
|
||||
static fromJSON(
|
||||
{ groups, ...json }: ToJson<Service>,
|
||||
{ groups, url, avatar, ...json }: ToJson<Service>,
|
||||
): Service {
|
||||
return new Service({
|
||||
groups: groups.map((group) => Ref.fromString<Group>(group)),
|
||||
url: new URL(url),
|
||||
avatar: new URL(avatar),
|
||||
...json,
|
||||
})
|
||||
}
|
||||
|
||||
static create(
|
||||
{ category, tags, url, groups, avatar, name }: Pick<
|
||||
{ category, url, groups, avatar, name }: Pick<
|
||||
Service,
|
||||
'category' | 'tags' | 'url' | 'groups' | 'avatar' | 'name'
|
||||
'category' | 'url' | 'groups' | 'avatar' | 'name'
|
||||
>,
|
||||
): Service {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name })
|
||||
return new Service({
|
||||
uuid,
|
||||
createdAt,
|
||||
|
@ -28,40 +29,21 @@ export class Service extends Ressource {
|
|||
name,
|
||||
category,
|
||||
url,
|
||||
tags,
|
||||
groups,
|
||||
avatar,
|
||||
})
|
||||
}
|
||||
|
||||
static load({
|
||||
name,
|
||||
avatar = Avatar.fromEmoji('⚙️'),
|
||||
tags = [],
|
||||
url,
|
||||
category,
|
||||
groups = [],
|
||||
}: {
|
||||
name: Service['name']
|
||||
url: Service['url']
|
||||
avatar?: Service['avatar']
|
||||
tags?: Service['tags']
|
||||
category: Service['category']
|
||||
groups?: Service['groups']
|
||||
}): Service {
|
||||
return this.create({ name, avatar, tags, url, category, groups })
|
||||
}
|
||||
|
||||
#category: 'web' | 'fs' | 'git'
|
||||
#url: UrlString
|
||||
#category: 'bin' | 'web' | 'fs' | 'git'
|
||||
#url: URL
|
||||
#groups: readonly Ref<Group>[]
|
||||
#tags: readonly string[]
|
||||
#avatar: URL
|
||||
|
||||
private constructor({
|
||||
category,
|
||||
tags,
|
||||
url,
|
||||
groups,
|
||||
avatar,
|
||||
...ressource
|
||||
}: Pick<
|
||||
Service,
|
||||
|
@ -71,21 +53,20 @@ export class Service extends Ressource {
|
|||
| 'name'
|
||||
| 'category'
|
||||
| 'url'
|
||||
| 'tags'
|
||||
| 'groups'
|
||||
| 'avatar'
|
||||
>) {
|
||||
super(ressource)
|
||||
|
||||
if (!['web', 'fs', 'git'].includes(category)) {
|
||||
if (!['bin', 'web', 'fs', 'git'].includes(category)) {
|
||||
throw new TypeError(
|
||||
`category is "${category}" but ('web' | 'fs' | 'git') is required`,
|
||||
`category is "${category}" but ('bin' | 'web' | 'fs' | 'git') is required`,
|
||||
)
|
||||
}
|
||||
this.#category = category
|
||||
this.#url = new URL(url).href as UrlString
|
||||
this.#url = url
|
||||
this.#groups = Object.freeze(groups)
|
||||
this.#tags = Object.freeze(tags)
|
||||
this.#avatar = avatar
|
||||
}
|
||||
|
||||
get type(): 'service' {
|
||||
|
@ -96,16 +77,16 @@ export class Service extends Ressource {
|
|||
return this.#category
|
||||
}
|
||||
|
||||
get tags() {
|
||||
return this.#tags
|
||||
}
|
||||
|
||||
get url() {
|
||||
return this.#url
|
||||
return new URL(this.#url)
|
||||
}
|
||||
|
||||
get groups() {
|
||||
return this.#groups
|
||||
return Object.freeze(this.#groups)
|
||||
}
|
||||
|
||||
get avatar() {
|
||||
return new URL(this.#avatar)
|
||||
}
|
||||
|
||||
update(
|
||||
|
@ -121,25 +102,21 @@ export class Service extends Ressource {
|
|||
...super.toJSON(),
|
||||
type: this.type,
|
||||
category: this.category,
|
||||
tags: this.tags,
|
||||
url: this.url,
|
||||
url: this.url.toJSON(),
|
||||
groups: this.groups.map((group) => group.toJSON()),
|
||||
avatar: this.avatar.toJSON(),
|
||||
} as const
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `Service (${JSON.stringify(this)})`
|
||||
}
|
||||
|
||||
toRef(): Ref<Service> {
|
||||
return super.toRef() as Ref<Service>
|
||||
}
|
||||
}
|
||||
|
||||
export interface Service extends Ressource {
|
||||
type: 'service'
|
||||
category: 'web' | 'fs' | 'git'
|
||||
tags: readonly string[]
|
||||
url: UrlString
|
||||
category: 'bin' | 'web' | 'fs' | 'git'
|
||||
url: URL
|
||||
groups: readonly Ref<Group>[]
|
||||
avatar: URL
|
||||
}
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
import type { Login, MailAddress, Posix, ToJson } from '@/types.ts'
|
||||
import { Login, MailAddress, Posix, ToJson } from '@/types.ts'
|
||||
import { regex, toLogin } from '@/utils.ts'
|
||||
import type { Credential, CredentialCategory } from '@models/credential.ts'
|
||||
import type { Group } from '@models/group.ts'
|
||||
import { Credential } from '@models/credential.ts'
|
||||
import { Group } from '@models/group.ts'
|
||||
import { Ressource } from '@models/ressource.ts'
|
||||
import { Avatar } from '@/src/models/utils/avatar.ts'
|
||||
import { Ref } from '@/src/models/utils/ref.ts'
|
||||
|
||||
export class User extends Ressource {
|
||||
static fromJSON(json: ToJson<User>): User {
|
||||
static fromJSON(
|
||||
json: ToJson<User>,
|
||||
): User {
|
||||
if (json.type !== 'user') {
|
||||
throw new TypeError(`type is "${json.type}" but "user" is required`)
|
||||
}
|
||||
|
@ -21,10 +22,7 @@ export class User extends Ressource {
|
|||
`firstname type is "${typeof json.firstname}" but "string" is required`,
|
||||
)
|
||||
}
|
||||
if (
|
||||
typeof json.login !== 'string' &&
|
||||
/(\w|_)+\.(\w|_)+/.test(json.login)
|
||||
) {
|
||||
if (typeof json.login !== 'string' && /(\w|_)+\.(\w|_)+/.test(json.login)) {
|
||||
throw new TypeError(
|
||||
`login is "${json.login}" but "\${string}.\${string}" is required`,
|
||||
)
|
||||
|
@ -37,20 +35,20 @@ export class User extends Ressource {
|
|||
|
||||
const credentials = Object.freeze(
|
||||
json.credentials.map((credential) =>
|
||||
Ref.fromString<Credential<CredentialCategory>>(credential)
|
||||
Ref.fromString<Credential>(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 })
|
||||
return new User({ ...json, posix, credentials, groups, avatar })
|
||||
}
|
||||
|
||||
static create({
|
||||
name,
|
||||
avatar,
|
||||
...props
|
||||
}: Pick<
|
||||
User,
|
||||
|
@ -63,38 +61,8 @@ export class User extends Ressource {
|
|||
| 'avatar'
|
||||
| 'credentials'
|
||||
>): User {
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
|
||||
return new User({ name, avatar, uuid, createdAt, updatedAt, ...props })
|
||||
}
|
||||
|
||||
static load({
|
||||
lastname,
|
||||
firstname,
|
||||
mail,
|
||||
groups = [],
|
||||
posix,
|
||||
avatar = Avatar.fromEmoji('👤'),
|
||||
credentials = [],
|
||||
}: {
|
||||
lastname: User['lastname']
|
||||
firstname: User['firstname']
|
||||
mail: User['mail']
|
||||
groups?: User['groups']
|
||||
posix?: User['posix']
|
||||
avatar?: User['avatar']
|
||||
credentials?: User['credentials']
|
||||
}): User {
|
||||
const name = `${firstname} ${lastname}`
|
||||
return this.create({
|
||||
name,
|
||||
lastname,
|
||||
firstname,
|
||||
mail,
|
||||
groups,
|
||||
posix,
|
||||
avatar,
|
||||
credentials,
|
||||
})
|
||||
const { uuid, createdAt, updatedAt } = super.create({ name })
|
||||
return new User({ name, uuid, createdAt, updatedAt, ...props })
|
||||
}
|
||||
|
||||
#lastname: string
|
||||
|
@ -103,7 +71,8 @@ export class User extends Ressource {
|
|||
#mail: MailAddress
|
||||
#groups: readonly Ref<Group>[]
|
||||
#posix?: Posix
|
||||
#credentials: readonly Ref<Credential<CredentialCategory>>[]
|
||||
#avatar: URL
|
||||
#credentials: readonly Ref<Credential>[]
|
||||
|
||||
private constructor({
|
||||
lastname,
|
||||
|
@ -111,6 +80,7 @@ export class User extends Ressource {
|
|||
mail,
|
||||
groups,
|
||||
posix,
|
||||
avatar,
|
||||
credentials,
|
||||
...ressource
|
||||
}: Pick<
|
||||
|
@ -143,6 +113,7 @@ export class User extends Ressource {
|
|||
}
|
||||
}
|
||||
this.#posix = posix
|
||||
this.#avatar = avatar
|
||||
this.#login = toLogin({ firstname, lastname })
|
||||
this.#credentials = Object.freeze(credentials)
|
||||
}
|
||||
|
@ -168,6 +139,9 @@ export class User extends Ressource {
|
|||
get posix() {
|
||||
return this.#posix
|
||||
}
|
||||
get avatar() {
|
||||
return this.#avatar
|
||||
}
|
||||
get credentials() {
|
||||
return this.#credentials
|
||||
}
|
||||
|
@ -187,7 +161,7 @@ export class User extends Ressource {
|
|||
mail: this.mail,
|
||||
groups: this.groups.map((group) => group.toJSON()),
|
||||
posix: this.posix ?? null,
|
||||
avatar: this.avatar,
|
||||
avatar: this.avatar.toJSON(),
|
||||
credentials: this.credentials.map((credential) => credential.toJSON()),
|
||||
} as const
|
||||
}
|
||||
|
@ -195,10 +169,6 @@ export class User extends Ressource {
|
|||
toString(): string {
|
||||
return `User (${JSON.stringify(this)})`
|
||||
}
|
||||
|
||||
toRef(): Ref<User> {
|
||||
return super.toRef() as Ref<User>
|
||||
}
|
||||
}
|
||||
|
||||
export interface User extends Ressource {
|
||||
|
@ -209,5 +179,6 @@ export interface User extends Ressource {
|
|||
mail: MailAddress
|
||||
groups: readonly Ref<Group>[]
|
||||
posix: Posix | undefined
|
||||
credentials: readonly Ref<Credential<CredentialCategory>>[]
|
||||
avatar: URL
|
||||
credentials: readonly Ref<Credential>[]
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
import type { UrlString } from '@/types.ts'
|
||||
|
||||
export class Avatar {
|
||||
static fromEmoji(emoji: string): UrlString {
|
||||
return `data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%2210 0 100 100%22><text y=%22.90em%22 font-size=%2290%22>${emoji}</text></svg>`
|
||||
}
|
||||
private constructor() {}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import type { Db } from '@/mod.ts'
|
||||
import { Db } from '@/mod.ts'
|
||||
import type { UUID } from '@/types.ts'
|
||||
import {
|
||||
Credential,
|
||||
|
@ -38,9 +38,8 @@ export class Ref<T extends Ressource> extends String {
|
|||
}
|
||||
|
||||
static dbResolver(db: Db) {
|
||||
return <T extends Ressource>(ref: RefString<T>): Promise<T> => {
|
||||
return <T extends Ressource>(ref: RefString<T>) => {
|
||||
const { type, uuid } = Ref.parse(ref)
|
||||
//@ts-expect-error force type casting to fix
|
||||
return db.ressource[type].get({ uuid })
|
||||
}
|
||||
}
|
||||
|
|
8
types.ts
8
types.ts
|
@ -26,11 +26,3 @@ export type Posix = {
|
|||
home: string
|
||||
shell: string
|
||||
}
|
||||
|
||||
export type Base64String = `${string}`
|
||||
export type UrlString = `${
|
||||
| 'data:'
|
||||
| 'file://'
|
||||
| 'http://'
|
||||
| 'https://'
|
||||
| 'git:'}${string}`
|
||||
|
|
Loading…
Reference in a new issue