aboutsummaryrefslogtreecommitdiffstats
path: root/static/punks/29.webp
diff options
context:
space:
mode:
Diffstat (limited to 'static/punks/29.webp')
0 files changed, 0 insertions, 0 deletions
f='#n5'>5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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.status(401).send("Unauthorized");
      req.user = user;
    }
  )
};

export const auth = {
  preValidation: fastifyPassport.authenticate("jwt", {
    authInfo: false,
    session: false
  })
};