import { GetCustomHandlers } from "@onebro/appmaster"; import { SendReqOpts,sendreq, } from "@onebro/oba-common"; import {Request,Response,NextFunction} from "express"; import * as ob from "@onebro/oba-common"; export const OBCustomMiddlewares:GetCustomHandlers = m => ({ apiGuard:{ after:"cors", func:m => async (req:Request,res:Response,next:NextFunction) => { //ob.log(req.url.split("/").slice(1)); const apis = m.vars.consumers; if(apis){ const name = req.url.split("/").slice(1)[0]; const api = apis[name]; if(api){ const id = req.headers["oba-client-id"] as string||null; const key = req.headers["oba-client-key"] as string||null; const valid = api && id && key?id === api.id && key === api.key:false; //ob.log(name,api,id,key,valid); if(!(id && key)) return next(m.e.missing(401,"API credentials not provided")); if(!valid) return next(m.e.invalid(401,"API credentials invalid")); } } return next();}}, appShake:{ after:"session", func:() => async (req:Request,res:Response,next:NextFunction) => { /* if(req.cookies._caOB){ let randomNumber = Math.random().toString(); randomNumber = randomNumber.substring(2,randomNumber.length); res.cookie("_baOB",randomNumber,{maxAge:900000,httpOnly:true});} else if(req.cookies._aB){ let randomNumber = Math.random().toString(); randomNumber = randomNumber.substring(2,randomNumber.length); res.cookie("_bcOB",randomNumber,{maxAge:900000,httpOnly:true});} else { */ let randomNumber = Math.random().toString(); randomNumber = randomNumber.substring(2,randomNumber.length); res.cookie("_bc_0",randomNumber,{maxAge:900000,httpOnly:true}); return next();}}, reqCounter:{ after:"session", func:m => async (req:Request,res:Response,next:NextFunction) => { const within1Min = req.session.lastReq?((Date.now() - req.session.lastReq)/1000) <= 60:true; if(within1Min) req.session.visits = (req.session.visits||0)+1; else req.session.visits = 1; //req.session.reqsPerMin = (req.session.visits/60);} //if(req.session.visits > 12) console.warn("whoa what is going on?"); req.session.lastReq = Date.now(); return next();}}, }); /* showOrigin:{ before:"cors", func:m => async (req:Request,res:Response,next:NextFunction) => { ob.log(req.headers["origin"]); return next();}}, ipData:{ after:"session", func:m => async (req:Request,res:Response,next:NextFunction) => { if(req.session && !req.session.ipdata){ const apikey = m.vars.providers["ip-data"]; const ip = /127.0.0/.test(req.ip)?"":req.ip; const url = `https://api.ipdata.co${ip?"/"+ip:""}?api-key=${apikey}`; const opts:SendReqOpts = {url}; req.session.ipdata = await sendreq(opts);} return next();}}, addUserToLocals:{ after:"session", func:m => async (req:Request,res:Response,next:NextFunction) => { res.locals.user = req.appuser; return next();} */