aboutsummaryrefslogtreecommitdiffstats
path: root/src/middleware.ts
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-11-07 22:13:49 +0200
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-11-07 22:13:49 +0200
commit7991ad93470cf42b9c4c96477fc920bbb8899eee (patch)
tree2e066c5ffd4895f2bafede1f92cb1cdc08557b4d /src/middleware.ts
parent2c5c47e3c5343419a6efa618b9d6549394da2e9f (diff)
Prepare for move to DO
Diffstat (limited to 'src/middleware.ts')
-rw-r--r--src/middleware.ts47
1 files changed, 6 insertions, 41 deletions
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<void>): Promise<void> => {
const userId = ctx.message?.from.id;
@@ -19,12 +17,7 @@ const tgAuth = async (ctx: Context, next: () => Promise<void>): Promise<void> =>
}
};
-const verifyGoogleJWT = async (req: Request, res: Response, next: () => void): Promise<void> => {
- if (config.nodeEnv !== "production") {
- next();
- return;
- }
-
+const httpHeaderAuth = async (req: Request, res: Response, next: () => void): Promise<void> => {
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<string, string> = 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,
};