From e596bddb27739f04948838791d70bf3c9390efec Mon Sep 17 00:00:00 2001 From: Nikky Date: Fri, 31 Dec 2021 08:15:51 +0300 Subject: [PATCH] typings --- src/index.js | 4 ++-- src/logic/cache.js | 22 +++++++++++----------- src/logic/session.js | 14 ++++++++++++-- src/logic/utils.js | 2 +- src/model/model.d.ts | 5 +++++ src/model/user.model.js | 13 ++++++++----- src/route/auth.controller.js | 25 ++++++++++++------------- src/route/route.d.ts | 7 +++++++ src/route/user.controller.js | 7 ++----- src/type/database.d.ts | 7 +++++++ src/type/webserver.d.ts | 6 ++++++ 11 files changed, 73 insertions(+), 39 deletions(-) create mode 100644 src/model/model.d.ts create mode 100644 src/route/route.d.ts create mode 100644 src/type/database.d.ts create mode 100644 src/type/webserver.d.ts diff --git a/src/index.js b/src/index.js index 75a2487..802d9ee 100644 --- a/src/index.js +++ b/src/index.js @@ -15,8 +15,8 @@ async function Database(){ const opts = {db}; UserEntity(opts); } - await db.authenticate(); - await db.sync({alter: true}); + await db.authenticate(); //connect to the database of choice + await db.sync({alter: true}); //add missing tables and columns } async function WebApp(){ diff --git a/src/logic/cache.js b/src/logic/cache.js index 62c8506..55a7dbe 100644 --- a/src/logic/cache.js +++ b/src/logic/cache.js @@ -20,12 +20,12 @@ export class CacheTable{ this.entries[key] = val; return val; } - remove(key){ - if(this.entries[key] !== undefined){ - delete this.entries[key]; - this.count--; - } - } + remove(key){ + if(this.entries[key] !== undefined){ + delete this.entries[key]; + this.count--; + } + } get(key){ return this.entries[key]; } allEntries(){ return this.entries; } } @@ -37,11 +37,11 @@ export class SpamCache extends CacheTable{ super(1024); } - /** - * @param string key - * @param {[[maxHits: number, durationMs: number]]} constraints - * @returns - */ + /** + * @param string key + * @param {[[maxHits: number, durationMs: number]]} constraints + * @returns + */ check(key, constraints){ const now = Date.now(); let record = this.get(key); diff --git a/src/logic/session.js b/src/logic/session.js index 8a726c5..e235b61 100644 --- a/src/logic/session.js +++ b/src/logic/session.js @@ -1,12 +1,12 @@ import { CacheTable, SpamCache } from "./cache.js"; -import { requestIp } from "./utils.js"; +import { ipAddress } from "./utils.js"; export class UserSession{ constructor(user, request){ this.user = user; this.createdAt = Date.now(); - this.ip = requestIp(request); + this.ip = ipAddress(request); this.spam = new SpamCache(); userCache.set(user.token, this); } @@ -18,6 +18,16 @@ export class UserSession{ static find(token){ return userCache.get(token); } + static onUpdate(user){ + //todo: figure out where exactly + //this hook is necessary, if it is + const session = find(user); + if(session) session.user = user; + } + static onDestroy(user){ + const session = find(user); + if(session) session.kill(); + } } const userCache = new CacheTable(8192); diff --git a/src/logic/utils.js b/src/logic/utils.js index 8b35192..760d5f8 100644 --- a/src/logic/utils.js +++ b/src/logic/utils.js @@ -12,7 +12,7 @@ export function errorOut(reply, msg, code){ reply.send(msg || 'Bad request.'); } -export function requestIp(request){ +export function ipAddress(request){ return request.ip; } diff --git a/src/model/model.d.ts b/src/model/model.d.ts new file mode 100644 index 0000000..fc878c8 --- /dev/null +++ b/src/model/model.d.ts @@ -0,0 +1,5 @@ +import { database } from '../type/database'; + +export interface props{ + db: database +}; diff --git a/src/model/user.model.js b/src/model/user.model.js index 3ff49c8..b9b7a26 100644 --- a/src/model/user.model.js +++ b/src/model/user.model.js @@ -1,9 +1,8 @@ import Sequelize from 'sequelize'; +import { UserSession } from '../logic/session.js'; + +/** @param {import('./model').props} */ -/** - * @param {Object} props - * @param {import('sequelize/types').Sequelize} props.db - */ const UserEntity = ({db}) => ( db.define('Users', { @@ -24,7 +23,11 @@ const UserEntity = ({db}) => ( indexes: [ { fields: ['token'], unique: true }, { fields: ['email'], unique: true } - ] + ], + hooks: { + beforeUpdate: UserSession.onUpdate, + afterDestroy: UserSession.onDestroy + } }) ); diff --git a/src/route/auth.controller.js b/src/route/auth.controller.js index 9cbb11d..e24ad40 100644 --- a/src/route/auth.controller.js +++ b/src/route/auth.controller.js @@ -1,22 +1,21 @@ -import { checkStringParam, errorOut, notYet, randomElement, requestIp, reverseString, hours, minutes } from '../logic/utils.js'; +import { checkStringParam, errorOut, notYet, randomElement, ipAddress, reverseString, hours, minutes } from '../logic/utils.js'; import { Animals } from '../misc/animals.js'; import { reissueToken, generateToken, newTokenExpiry, hashPassword, doesPasswordMatch, isEndpointAllowedForBannedUsers, isEndpointProtected, generateRestoreCode, restoreValidity, restoreAttempts } from '../logic/security.js'; import { sendRestorationLink } from '../logic/email.js'; import { UserSession } from '../logic/session.js'; import { SpamCache } from '../logic/cache.js'; -/** - * @param {Object} props - * @param {import('fastify').FastifyInstance} props.app - * @param {import('sequelize/types').Sequelize} props.db - */ +/** @param {import('./route').props} */ + function AuthController({app, db}){ const { Users } = db.models; - const signInSpam = new SpamCache(); - const signUpSpam = new SpamCache(); - const restoreApplySpam = new SpamCache(); + const spamCheck = { + signIn: new SpamCache(), + signUp: new SpamCache(), + restoreApply: new SpamCache() + }; { //validate token header and put .session in every request app.decorateRequest('session', null); @@ -45,7 +44,7 @@ function AuthController({app, db}){ app.post('/auth/sign-in', async (request, reply) => { const {email, paswd} = request.body || {}; - if(signInSpam.check(requestIp(request), [[5000, 2*hours]])){ + if(spamCheck.signIn.check(ipAddress(request), [[5000, 2*hours]])){ return errorOut(reply, 'error.too_fast'); } @@ -70,7 +69,7 @@ function AuthController({app, db}){ app.post('/auth/sign-up', async (request, reply) => { const {email, username, paswd} = request.body || {}; - if(signUpSpam.check(requestIp(request), [[5000, 2*hours], [100, 10*minutes]])){ + if(spamCheck.signUp.check(ipAddress(request), [[5000, 2*hours], [100, 10*minutes]])){ return errorOut(reply, 'error.too_fast'); } @@ -101,7 +100,7 @@ function AuthController({app, db}){ tokenExpiry: newTokenExpiry(), paswd: hashPassword(reverseString(paswd)), role: 'user', - firstIp: requestIp(request) + firstIp: ipAddress(request) }; await Users.create(newUser); return {token: newUser.token}; @@ -130,7 +129,7 @@ function AuthController({app, db}){ const {email, code, newpaswd} = request.body || {}; const changeRequested = (newpaswd != null); - if(restoreApplySpam.check(requestIp(request), [[100, 12*hours], [25, 30*minutes]])){ + if(spamCheck.restoreApply.check(ipAddress(request), [[100, 12*hours], [25, 30*minutes]])){ return errorOut(reply, 'error.too_fast'); } diff --git a/src/route/route.d.ts b/src/route/route.d.ts new file mode 100644 index 0000000..90c0c16 --- /dev/null +++ b/src/route/route.d.ts @@ -0,0 +1,7 @@ +import { webserver } from '../type/webserver'; +import { database } from '../type/database'; + +export interface props{ + app: webserver, + db: database +}; diff --git a/src/route/user.controller.js b/src/route/user.controller.js index 66f42f3..df08f1d 100644 --- a/src/route/user.controller.js +++ b/src/route/user.controller.js @@ -1,10 +1,7 @@ import { reissueToken, tokenLifetime, tokenReissue } from '../logic/security.js'; -/** - * @param {Object} props - * @param {import('fastify').FastifyInstance} props.app - * @param {import('sequelize/types').Sequelize} props.db - */ +/** @param {import('./route').props} */ + function UserController({app, db}){ const { Users } = db.models; diff --git a/src/type/database.d.ts b/src/type/database.d.ts new file mode 100644 index 0000000..ecf98a3 --- /dev/null +++ b/src/type/database.d.ts @@ -0,0 +1,7 @@ +import { Sequelize } from 'sequelize/types'; + +export type database = Sequelize & { + models: { + potato: string + } +}; diff --git a/src/type/webserver.d.ts b/src/type/webserver.d.ts new file mode 100644 index 0000000..47dae4b --- /dev/null +++ b/src/type/webserver.d.ts @@ -0,0 +1,6 @@ +import { FastifyInstance } from 'fastify/types/instance'; +import { UserSession } from '../logic/session.js'; + +export type webserver = FastifyInstance & { + session: UserSession +};