From baf9dc54c66af49ee7c2f3418c5ce2ce3169df8c Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Tue, 5 Aug 2025 13:35:22 +0200 Subject: [PATCH] wip(auth): WIP --- src/@shared/src/database/index.ts | 49 ++++++++++++++++------------ src/@shared/src/database/init.dbml | 16 ++------- src/@shared/src/database/users.ts | 7 ++++ src/auth/extra/providers-schema.json | 35 ++++++++++++++++++++ src/auth/extra/providers.toml | 7 ++++ 5 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 src/@shared/src/database/users.ts create mode 100644 src/auth/extra/providers-schema.json create mode 100644 src/auth/extra/providers.toml diff --git a/src/@shared/src/database/index.ts b/src/@shared/src/database/index.ts index 6a36d02..04bcaae 100644 --- a/src/@shared/src/database/index.ts +++ b/src/@shared/src/database/index.ts @@ -1,26 +1,27 @@ -// ************************************************************************** // -// // -// ::: :::::::: // -// index.ts :+: :+: :+: // -// +:+ +:+ +:+ // -// By: maiboyer +#+ +:+ +#+ // -// +#+#+#+#+#+ +#+ // -// Created: 2025/07/28 17:36:22 by maiboyer #+# #+# // -// Updated: 2025/08/03 13:36:25 by maiboyer ### ########.fr // -// // -// ************************************************************************** // - import fp from 'fastify-plugin' import { FastifyInstance } from 'fastify' import sqlite from 'better-sqlite3' +// @ts-ignore: this file is included using vite, typescript doesn't know how to include this... import initSql from "./init.sql?raw" -import { newUUIDv7, UUIDv7 } from '@shared/uuid' - -export class DBUserExists extends Error { - public readonly type = 'db-user-exists'; -} +/** + * represent a unique user (by its ID.) + * Having this means that the user does exist (aka it hasn't been deleted) + */ +export type UserID = number & { readonly __brand: unique symbol }; +/** + * The full representation of an user + * + * @property id [UserID]: The id of the user (unique) + * @property name [string]: The username of the user (unique) + * @property password [?string]: The password hash of the user (if password is defined) + */ +export type DbUser = { + readonly id: UserID, + readonly name: string, + readonly password: string | null, +}; // Only way to use the database. Everything must be done through this. // Prefer to use prepared statement `using this.db.prepare` @@ -28,6 +29,7 @@ export class Database { private db: sqlite.Database; private st: Map = new Map(); + /** * Create a new instance of the database, and init it to a known state * the file ./init.sql will be ran onto the database, creating any table that might be missing @@ -66,6 +68,10 @@ export class Database { this.st.set(query, st); return st; } + + public getUser(user: UserID): DbUser { + + }; } // When using .decorate you have to specify added properties for Typescript @@ -79,11 +85,12 @@ export type DatabaseOption = { path: string; }; -export const uDatabase = fp(async function( - _fastify: FastifyInstance, +export const useDatabase = fp(async function( + f: FastifyInstance, _options: DatabaseOption) { - console.log("Database has been hooked up to fastify ?!"); + f.log.info("Database has been hooked up to fastify ?!"); + f.log.warn("TODO: actually hook up database to fastify..."); }); -export default uDatabase; +export default useDatabase; diff --git a/src/@shared/src/database/init.dbml b/src/@shared/src/database/init.dbml index fd44c65..2fc5971 100644 --- a/src/@shared/src/database/init.dbml +++ b/src/@shared/src/database/init.dbml @@ -25,7 +25,7 @@ Table user { Table auth { id integer [PK, not null, increment] - provider integer [ref: > provider.id, not null] + provider text [not null] user integer [ref: > user.id, not null] oauth2_user text [not null, unique, Note: ''' This makes sure that an oauth2 login is the always the same `user` @@ -33,25 +33,13 @@ Table auth { '''] } -Table provider { - id integer [PK, not null, increment] - name text [PK, not null] - displayName text [not null] - secret text [not null] - token_url text [not null] - auth_url text [not null] - me_url text [not null] - - Note: "Oauth2 Providers" -} - Table session { id integer [PK, not null, increment] cookie text [PK, unique, not null] userid integer [ref: > user.id, not null] createAt text [not null] userAgent text [not null] - reason integer [null, ref: > provider.id] + reason integer [null] Note: "Every session for users" } diff --git a/src/@shared/src/database/users.ts b/src/@shared/src/database/users.ts new file mode 100644 index 0000000..e22c244 --- /dev/null +++ b/src/@shared/src/database/users.ts @@ -0,0 +1,7 @@ +import { Database } from "@shared/database"; + +export type UserID = Number & { readonly __brand: unique symbol }; + +export async function getUser(this: Database, id: UserID) { + console.log(this); +} diff --git a/src/auth/extra/providers-schema.json b/src/auth/extra/providers-schema.json new file mode 100644 index 0000000..a312144 --- /dev/null +++ b/src/auth/extra/providers-schema.json @@ -0,0 +1,35 @@ +{ + "type": "object", + "properties": { + "providers": { + "required": [], + "type": "object", + "additionalProperties": { + "type": "object", + "properties": { + "redirect_url": { + "type": "string" + }, + "token_url": { + "type": "string" + }, + "client_id": { + "type": "string" + }, + "secret_env": { + "type": "string" + } + }, + "required": [ + "redirect_url", + "token_url", + "client_id", + "secret_env" + ] + } + } + }, + "required": [ + "providers" + ] +} diff --git a/src/auth/extra/providers.toml b/src/auth/extra/providers.toml new file mode 100644 index 0000000..ab99f42 --- /dev/null +++ b/src/auth/extra/providers.toml @@ -0,0 +1,7 @@ +#:schema ./providers-schema.json + +[providers.42] +token_url = "" # which url to use +redirect_url = "" # redirect_url +client_id = "" # the client_id for the provider +secret_env = "" # env containing the secret for the provider