diff options
| author | Adam Soltys <[email protected]> | 2023-02-06 16:48:13 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-02-06 16:48:13 -0800 |
| commit | 9c667055c6367e1b2e239686e72f52995679a47e (patch) | |
| tree | 0b1d4aee51ec2d4e92d08a9d1404ba4e2ecd462a /lib/passport.js | |
| parent | 15294844dbc8e73b4d1e92008a7eebc6b86b9ec5 (diff) | |
| parent | 6f5602d112b68adb7452abf3047f20c32e7725b6 (diff) | |
| download | coinos-server-9c667055c6367e1b2e239686e72f52995679a47e.tar.gz coinos-server-9c667055c6367e1b2e239686e72f52995679a47e.zip | |
Merge branch 'master' into test
Diffstat (limited to 'lib/passport.js')
| -rw-r--r-- | lib/passport.js | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/lib/passport.js b/lib/passport.js deleted file mode 100644 index ac2949a5..00000000 --- a/lib/passport.js +++ /dev/null @@ -1,84 +0,0 @@ -import app from "$app"; -import db from "$db"; -import jwt from "passport-jwt"; -import { Op } from "@sequelize/core"; -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"; - -app.register(fastifySecureSession, { - key: config.jwt -}); - -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 { username } = payload; - let user = await db.User.findOne({ - where: { - username: { [Op.or]: [username, username.replace(/ /g, "")] } - }, - include: { model: db.Account, as: "account" } - }); - if (user.locked) throw new Error("account is locked"); - next(null, user); - } catch (e) { - 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 - }) -}; |
