From 91e847126c832efa25ab0bf8d6f3b7e537378fa7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 16 Jan 2023 23:40:25 -0800 Subject: shuffle things around --- lib/passport.js | 79 --------------------------------------------------------- 1 file changed, 79 deletions(-) delete mode 100644 lib/passport.js (limited to 'lib/passport.js') diff --git a/lib/passport.js b/lib/passport.js deleted file mode 100644 index 25dc0103..00000000 --- a/lib/passport.js +++ /dev/null @@ -1,79 +0,0 @@ -import app from "$app"; -import jwt from "passport-jwt"; -import fastifyPassport from "@fastify/passport"; -import fastifySecureSession from "@fastify/secure-session"; -import config from "$config"; -import fs from "fs"; -import path from "path"; -import { err } from "$lib/logging"; -import { g } from "$lib/redis"; - -app.register(fastifySecureSession, { - key: Buffer.from(config.jwt, "hex") -}); - -app.register(fastifyPassport.initialize()); -app.register(fastifyPassport.secureSession()); - -function cookieExtractor(req) { - if (req && req.cookies) { - return req.cookies["token"]; - } - return null; -} - -fastifyPassport.use( - "jwt", - new jwt.Strategy( - { - jwtFromRequest: jwt.ExtractJwt.fromExtractors([ - jwt.ExtractJwt.fromAuthHeaderAsBearerToken(), - cookieExtractor - ]), - secretOrKey: config.jwt - }, - async (payload, next) => { - try { - let { id } = payload; - let user = await g(`user:${id}`); - user.haspin = !!user.pin; - next(null, user); - } catch (e) { - console.log("payload", payload); - if (!e.message.includes("locked")) { - console.log(e); - err("problem logging in", e); - } - next(null, { username: "guest" }); - } - } - ) -); - -export const optionalAuth = { - preValidation: fastifyPassport.authenticate( - "jwt", - { session: false }, - function(req, res, err, user, info) { - req.user = user; - } - ) -}; - -export const adminAuth = { - preValidation: fastifyPassport.authenticate( - "jwt", - { session: false }, - function(req, res, err, user, info) { - if (!(user && user.admin)) return res.code(401).send("Unauthorized"); - req.user = user; - } - ) -}; - -export const auth = { - preValidation: fastifyPassport.authenticate("jwt", { - authInfo: false, - session: false - }) -}; -- cgit v1.2.3