From 7991ad93470cf42b9c4c96477fc920bbb8899eee Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 7 Nov 2021 22:13:49 +0200 Subject: Prepare for move to DO --- src/middleware.ts | 47 ++++++----------------------------------------- 1 file changed, 6 insertions(+), 41 deletions(-) (limited to 'src/middleware.ts') diff --git a/src/middleware.ts b/src/middleware.ts index 46d6e00..98d5688 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,10 +1,8 @@ import { Request, Response } from "@tinyhttp/app"; import { Context } from "telegraf"; -import got from "got"; -import jwt from "jsonwebtoken"; +import config from "./config"; import { getPermittedUsers } from "./db"; -import config from "./config"; const tgAuth = async (ctx: Context, next: () => Promise): Promise => { const userId = ctx.message?.from.id; @@ -19,12 +17,7 @@ const tgAuth = async (ctx: Context, next: () => Promise): Promise => } }; -const verifyGoogleJWT = async (req: Request, res: Response, next: () => void): Promise => { - if (config.nodeEnv !== "production") { - next(); - return; - } - +const httpHeaderAuth = async (req: Request, res: Response, next: () => void): Promise => { const authHeader = req.headers.authorization || null; if (!authHeader) { res.sendStatus(401); @@ -32,43 +25,15 @@ const verifyGoogleJWT = async (req: Request, res: Response, next: () => void): P } const token = authHeader.split(" ")[1]; - if (!token) { + if (!token || token !== config.authToken) { res.sendStatus(401); return; } - const decoded = jwt.decode(token, { complete: true }); - if (!decoded) { - res.sendStatus(401); - return; - } - - const kid: string = decoded.header.kid; - - const response = await got("https://www.googleapis.com/oauth2/v1/certs", { json: true }); - const googleCerts: Record = response.body; - - const cert = googleCerts[kid]; - if (!cert) { - console.error("KID not found in google certificates"); - res.sendStatus(500); - return; - } - - try { - jwt.verify(token, cert); - } catch (err) { - res.sendStatus(403); - return; - } - next(); -}; +} -export const tgMiddleware = { +export { tgAuth, -}; - -export const httpMiddleware = { - verifyGoogleJWT, + httpHeaderAuth, }; -- cgit v1.3