fixes(eslint): fixing everything that eslint complained about

This commit is contained in:
Maieul BOYER 2025-09-29 11:50:53 +02:00
parent d11e4a4516
commit 404735fe22
17 changed files with 1566 additions and 69 deletions

View file

@ -26,11 +26,16 @@ declare module 'fastify' {
export interface FastifyContextConfig {
requireAuth?: boolean;
}
export interface RouteOptions {
[kRouteAuthDone]: boolean;
}
}
export const Otp = OTP;
let jwtAdded = false;
export const jwtPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
void _opts;
if (jwtAdded) return;
jwtAdded = true;
const env = process.env.JWT_SECRET;
@ -65,16 +70,18 @@ export type JwtType = Static<typeof JwtType>;
let authAdded = false;
export const authPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
void _opts;
if (authAdded) return void console.log('skipping');
authAdded = true;
await fastify.register(useDatabase as any, {});
await fastify.register(jwtPlugin as any, {});
await fastify.register(useDatabase as FastifyPluginAsync, {});
await fastify.register(jwtPlugin as FastifyPluginAsync, {});
await fastify.register(cookie);
if (!fastify.hasRequestDecorator('authUser')) {fastify.decorateRequest('authUser', undefined);}
if (!fastify.hasRequestDecorator('authUser')) { fastify.decorateRequest('authUser', undefined); }
fastify.addHook('onRoute', (routeOpts) => {
if (
routeOpts.config?.requireAuth &&
!(routeOpts as any)[kRouteAuthDone]
!routeOpts[kRouteAuthDone]
) {
const f: preValidationAsyncHookHandler = async function(req, res) {
try {
@ -119,7 +126,7 @@ export const authPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
routeOpts.preValidation = [routeOpts.preValidation, f];
}
(routeOpts as any)[kRouteAuthDone] = true;
routeOpts[kRouteAuthDone] = true;
}
});
});

View file

@ -21,14 +21,15 @@ let dbAdded = false;
export const useDatabase = fp<FastifyPluginAsync>(async function(
f: FastifyInstance,
_options: {}) {
if (dbAdded) {return;}
_options: object) {
void _options;
if (dbAdded) { return; }
dbAdded = true;
const path = process.env.DATABASE_DIR;
if (isNullish(path)) {throw 'env `DATABASE_DIR` not defined';}
if (isNullish(path)) { throw 'env `DATABASE_DIR` not defined'; }
f.log.info(`Opening database with path: ${path}/database.db`);
const db: Database = new DbImpl(`${path}/database.db`) as Database;
if (!f.hasDecorator('db')) {f.decorate('db', db);}
if (!f.hasDecorator('db')) { f.decorate('db', db); }
});
export default useDatabase;

View file

@ -1,6 +1,6 @@
import sqlite from 'better-sqlite3';
// @ts-ignore: this file is included using vite, typescript doesn't know how to include this...
// @ts-expect-error: this file is included using vite, typescript doesn't know how to include this...
import initSql from '../init.sql?raw';
export type SqliteReturn = object | undefined;

View file

@ -94,17 +94,17 @@ export const UserImpl: Omit<IUserDb, keyof Database> = {
},
getUserOtpSecret(this: IUserDb, id: UserId): string | undefined {
const otp: any = this.prepare('SELECT otp FROM user WHERE id = @id LIMIT 1').get({ id }) as SqliteReturn;
const otp = this.prepare('SELECT otp FROM user WHERE id = @id LIMIT 1').get({ id }) as ({ otp: string } | null | undefined);
if (isNullish(otp?.otp)) return undefined;
return otp.otp;
},
ensureUserOtpSecret(this: IUserDb, id: UserId): string | undefined {
const otp = this.getUserOtpSecret(id);
if (!isNullish(otp)) {return otp;}
if (!isNullish(otp)) { return otp; }
const otpGen = new Otp();
const res: any = this.prepare('UPDATE OR IGNORE user SET otp = @otp WHERE id = @id RETURNING otp')
.get({ id, otp: otpGen.secret });
const res = this.prepare('UPDATE OR IGNORE user SET otp = @otp WHERE id = @id RETURNING otp')
.get({ id, otp: otpGen.secret }) as ({ otp: string } | null | undefined);
return res?.otp;
},
@ -154,12 +154,14 @@ async function hashPassword(
*
* @returns The user if it exists, undefined otherwise
*/
function userFromRow(row: any): User | undefined {
function userFromRow(row: Partial<User>): User | undefined {
if (isNullish(row)) return undefined;
if (isNullish(row.id)) return undefined;
if (isNullish(row.name)) return undefined;
return {
id: row.id as UserId,
name: row.name || undefined,
password: row.password || undefined,
otp: row.otp || undefined,
id: row.id,
name: row.name,
password: row.password ?? undefined,
otp: row.otp ?? undefined,
};
}

View file

@ -1,4 +1,4 @@
import { Type } from '@sinclair/typebox';
import { TObject, TProperties, Type } from '@sinclair/typebox';
/**
* @description Represent a message key
@ -12,7 +12,7 @@ import { Type } from '@sinclair/typebox';
* @example `pong.you.lost`
*/
export type MessageKey = string;
export type ResponseBase<T = {}> = {
export type ResponseBase<T = object> = {
kind: string,
msg: MessageKey,
payload?: T,
@ -26,7 +26,7 @@ export type ResponseBase<T = {}> = {
* @example makeResponse("failure", "login.failure.invalid")
* @example makeResponse("success", "login.success", { token: "supersecrettoken" })
*/
export function makeResponse<T = {}>(kind: string, key: MessageKey, payload?: T): ResponseBase<T> {
export function makeResponse<T = object>(kind: string, key: MessageKey, payload?: T): ResponseBase<T> {
console.log(`making response {kind: ${JSON.stringify(kind)}; key: ${JSON.stringify(key)}}`);
return { kind, msg: key, payload };
}
@ -39,7 +39,7 @@ export function makeResponse<T = {}>(kind: string, key: MessageKey, payload?: T)
* @example typeResponse("otpRequired", "login.otpRequired", { token: Type.String() })
* @example typeResponse("success", "login.success", { token: Type.String() })
*/
export function typeResponse(kind: string, key: MessageKey | MessageKey[], payload?: any): any {
export function typeResponse(kind: string, key: MessageKey | MessageKey[], payload?: TProperties): TObject<TProperties> {
let tKey;
if (key instanceof Array) {
tKey = Type.Union(key.map(l => Type.Const(l)));

View file

@ -1,17 +1,14 @@
import { FastifyPluginAsync } from 'fastify';
import fastifyFormBody from '@fastify/formbody';
import fastifyMultipart from '@fastify/multipart';
import { mkdir } from 'node:fs/promises';
import fp from 'fastify-plugin';
import * as db from '@shared/database';
import * as auth from '@shared/auth';
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
// When using .decorate you have to specify added properties for Typescript
declare module 'fastify' {
export interface FastifyInstance {
@ -19,20 +16,18 @@ declare module 'fastify' {
}
}
const app: FastifyPluginAsync = async (
fastify,
opts,
): Promise<void> => {
await fastify.register(db.useDatabase as any, {});
await fastify.register(auth.jwtPlugin as any, {});
await fastify.register(auth.authPlugin as any, {});
const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
void opts;
await fastify.register(db.useDatabase as FastifyPluginAsync, {});
await fastify.register(auth.jwtPlugin as FastifyPluginAsync, {});
await fastify.register(auth.authPlugin as FastifyPluginAsync, {});
// Place here your custom code!
for (const plugin of Object.values(plugins)) {
void fastify.register(plugin as any, {});
void fastify.register(plugin as FastifyPluginAsync, {});
}
for (const route of Object.values(routes)) {
void fastify.register(route as any, {});
void fastify.register(route as FastifyPluginAsync, {});
}
void fastify.register(fastifyFormBody, {});

View file

@ -12,10 +12,12 @@ export const WhoAmIRes = Type.Union([
export type WhoAmIRes = Static<typeof WhoAmIRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.put(
'/api/auth/disableOtp',
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'disableOtp.failure.generic');}
this.db.deleteUserOtpSecret(req.authUser.id);
return makeResponse('success', 'disableOtp.success');

View file

@ -13,10 +13,12 @@ export const WhoAmIRes = Type.Union([
export type WhoAmIRes = Static<typeof WhoAmIRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.put(
'/api/auth/enableOtp',
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'enableOtp.failure.noUser');}
const otpSecret = this.db.ensureUserOtpSecret(req.authUser!.id);
if (isNullish(otpSecret)) {return makeResponse('failure', 'enableOtp.failure.noSecret');}

View file

@ -21,10 +21,12 @@ export const LoginRes = Type.Union([
export type LoginRes = Static<typeof LoginRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post<{ Body: LoginReq; Response: LoginRes }>(
'/api/auth/login',
{ schema: { body: LoginReq, response: { '2xx': LoginRes } } },
async function(req, _res) {
void _res;
try {
const { name, password } = req.body;
const user = this.db.getUserFromName(name);

View file

@ -1,9 +1,11 @@
import { FastifyPluginAsync } from 'fastify';
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post(
'/api/auth/logout',
async function(_req, res) {
void _req;
return res.clearCookie('token').send('{}');
},
);

View file

@ -21,30 +21,35 @@ type OtpRes = Static<typeof OtpRes>;
const OTP_TOKEN_TIMEOUT_SEC = 120;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post<{ Body: OtpReq }>(
'/api/auth/otp',
{ schema: { body: OtpReq, response: { '2xx': OtpRes } } },
async function(req, _res) {
void _res;
try {
const { token, code } = req.body;
// lets try to decode+verify the jwt
const dJwt = this.jwt.verify<JwtType>(token);
// is the jwt a valid `otp` jwt ?
if (dJwt.kind != 'otp')
// no ? fuck off then
{return makeResponse('failed', 'otp.failed.invalid');}
if (dJwt.kind != 'otp') {
// no ? fuck off then
return makeResponse('failed', 'otp.failed.invalid');
}
// is it too old ?
if (dJwt.createdAt + OTP_TOKEN_TIMEOUT_SEC * 1000 < Date.now())
// yes ? fuck off then, redo the password
{return makeResponse('failed', 'otp.failed.timeout');}
if (dJwt.createdAt + OTP_TOKEN_TIMEOUT_SEC * 1000 < Date.now()) {
// yes ? fuck off then, redo the password
return makeResponse('failed', 'otp.failed.timeout');
}
// get the Otp sercret from the db
const user = this.db.getUserFromName(dJwt.who);
if (isNullish(user?.otp))
// oops, either no user, or user without otpSecret
// fuck off
{return makeResponse('failed', 'otp.failed.noSecret');}
if (isNullish(user?.otp)) {
// oops, either no user, or user without otpSecret
// fuck off
return makeResponse('failed', 'otp.failed.noSecret');
}
// good lets now verify the token you gave us is the correct one...
const otpHandle = new Otp({ secret: user.otp });
@ -58,10 +63,11 @@ const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
];
// checking if any of the array match
if (tokens.some((c) => c === code))
// they do !
// gg you are now logged in !
{return makeResponse('success', 'otp.success', { token: this.signJwt('auth', dJwt.who) });}
if (tokens.some((c) => c === code)) {
// they do !
// gg you are now logged in !
return makeResponse('success', 'otp.success', { token: this.signJwt('auth', dJwt.who) });
}
}
catch {
return makeResponse('failed', 'otp.failed.generic');

View file

@ -3,7 +3,7 @@ import { FastifyPluginAsync } from 'fastify';
import { Static, Type } from '@sinclair/typebox';
import { typeResponse, makeResponse, isNullish } from '@shared/utils';
const USERNAME_CHECK: RegExp = /^[a-zA-Z\_0-9]+$/;
const USERNAME_CHECK: RegExp = /^[a-zA-Z_0-9]+$/;
const SignInReq = Type.Object({
name: Type.String(),
@ -28,11 +28,13 @@ const SignInRes = Type.Union([
type SignInRes = Static<typeof SignInRes>;
const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post<{ Body: SignInReq }>(
'/api/auth/signin',
{ schema: { body: SignInReq, response: { '200': SignInRes, '5xx': Type.Object({}) } } },
async function(req, res) {
async function(req, _res) {
void _res;
const { name, password } = req.body;
if (name.length < 4) {return makeResponse('failed', 'signin.failed.username.tooshort');}

View file

@ -14,10 +14,12 @@ export const StatusOtpRes = Type.Union([
export type StatusOtpRes = Static<typeof StatusOtpRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.get(
'/api/auth/statusOtp',
{ schema: { response: { '2xx': StatusOtpRes } }, config: { requireAuth: true } },
async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'statusOtp.failure.generic');}
const otpSecret = this.db.getUserOtpSecret(req.authUser.id);
if (isNullish(otpSecret)) {return makeResponse('success', 'statusOtp.success.disabled');}

View file

@ -12,10 +12,12 @@ export const WhoAmIRes = Type.Union([
export type WhoAmIRes = Static<typeof WhoAmIRes>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.get(
'/api/auth/whoami',
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
async function(req, _res) {
void _res;
if (isNullish(req.authUser)) {return makeResponse('failure', 'whoami.failure.generic');}
return makeResponse('success', 'whoami.success', { name: req.authUser.name });
},

View file

@ -4,10 +4,11 @@ import fastifyMultipart from '@fastify/multipart';
import { mkdir } from 'node:fs/promises';
import fp from 'fastify-plugin';
import * as db from '@shared/database';
import { authPlugin, jwtPlugin } from '@shared/auth';
// @ts-except-error: import.meta.glob is a vite thing. Typescript doesn't know this...
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
// @ts-except-error: import.meta.glob is a vite thing. Typescript doesn't know this...
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
@ -20,20 +21,23 @@ declare module 'fastify' {
const app: FastifyPluginAsync = async (
fastify,
opts,
_opts,
): Promise<void> => {
void _opts;
// Place here your custom code!
for (const plugin of Object.values(plugins)) {
void fastify.register(plugin as any, {});
void fastify.register(plugin as FastifyPluginAsync, {});
}
for (const route of Object.values(routes)) {
void fastify.register(route as any, {});
void fastify.register(route as FastifyPluginAsync, {});
}
await fastify.register(db.useDatabase as any, {});
await fastify.register(db.useDatabase as FastifyPluginAsync, {});
await fastify.register(authPlugin as FastifyPluginAsync, {});
await fastify.register(jwtPlugin as FastifyPluginAsync, {});
void fastify.register(fastifyFormBody, {});
void fastify.register(fastifyMultipart, {});
console.log(fastify.db.getUser(1));
// The use of fastify-plugin is required to be able
// to export the decorators to the outer scope

View file

@ -5,21 +5,22 @@ import sharp from 'sharp';
import rawBody from 'raw-body';
import { isNullish } from '@shared/utils';
const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
// await fastify.register(authMethod, {});
// here we register plugins that will be active for the current fastify instance (aka everything in this function)
// we register a route handler for: `/<USERID_HERE>`
// it sets some configuration options, and set the actual function that will handle the request
fastify.addContentTypeParser('*', function(request, payload, done: any) {
done();
fastify.addContentTypeParser('*', function(request, payload, done) {
done(null);
});
fastify.post('/:userid', async function(request, reply) {
fastify.post<{ Params: { userid: string } }>('/:userid', async function(request, reply) {
const buffer = await rawBody(request.raw);
// this is how we get the `:userid` part of things
const userid: string | undefined = (request.params as any)['userid'];
const userid: string | undefined = (request.params)['userid'];
if (isNullish(userid)) {
return await reply.code(403);
}
@ -38,10 +39,10 @@ const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
await image_file.write(data);
await image_file.close();
}
catch (e: any) {
catch (e) {
fastify.log.error(`Error: ${e}`);
reply.code(400);
return { status: 'error', message: e.toString() };
return { status: 'error' };
}
});
};

1469
src/package-lock.json generated

File diff suppressed because it is too large Load diff