minimal passport
This commit is contained in:
@@ -31,9 +31,12 @@
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.18.2",
|
||||
"express-fileupload": "^1.4.2",
|
||||
"express-session": "^1.18.0",
|
||||
"joi": "^17.11.0",
|
||||
"jsqr": "^1.4.0",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"passport": "^0.7.0",
|
||||
"passport-local": "^1.0.0",
|
||||
"pdf-lib": "^1.17.1",
|
||||
"rollup-plugin-copy": "^3.5.0",
|
||||
"rollup-plugin-dynamic-import-variables": "^1.1.0",
|
||||
@@ -48,6 +51,8 @@
|
||||
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
|
||||
"@rollup/plugin-run": "^3.0.2",
|
||||
"@rollup/plugin-typescript": "^11.1.6",
|
||||
"@types/express-session": "^1.18.0",
|
||||
"@types/passport-local": "^1.0.38",
|
||||
"copyfiles": "^2.4.1",
|
||||
"pkgroll": "^2.0.1",
|
||||
"rimraf": "^5.0.5",
|
||||
|
||||
31
server-node/src/auth/passport-config.ts
Normal file
31
server-node/src/auth/passport-config.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import LocalStrategy from "passport-local";
|
||||
import * as User from "./user/user-controller";
|
||||
|
||||
export function initialize(passport: typeof import("passport")) {
|
||||
passport.use("local", new LocalStrategy.Strategy(
|
||||
function(username, password, done) {
|
||||
User.findOne({ username: username }, function (err, user) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
if (!user) {
|
||||
return done(null, false);
|
||||
}
|
||||
if (!User.verifyPassword(user, password)) {
|
||||
return done(null, false);
|
||||
}
|
||||
return done(null, user);
|
||||
});
|
||||
}
|
||||
));
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user.id)
|
||||
});
|
||||
|
||||
passport.deserializeUser((id: number, done) => {
|
||||
User.findOne({id: id}, function (err, user) {
|
||||
done(err, user);
|
||||
});
|
||||
});
|
||||
}
|
||||
16
server-node/src/auth/user/user-controller.ts
Normal file
16
server-node/src/auth/user/user-controller.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { User } from "./user-model";
|
||||
|
||||
export function findOne(params: {id?: number, username?: string}, cb: (err: Error | null, user: User) => void): undefined {
|
||||
//TODO: replace with db connection.
|
||||
cb(null, {
|
||||
id: 1,
|
||||
username: "test",
|
||||
mail: "test@test.com",
|
||||
accessControlList: []
|
||||
});
|
||||
}
|
||||
|
||||
export function verifyPassword(user: User, password: string) {
|
||||
//TODO: replace with db connection.
|
||||
return password == "test";
|
||||
}
|
||||
6
server-node/src/auth/user/user-model.ts
Normal file
6
server-node/src/auth/user/user-model.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface User {
|
||||
id: number,
|
||||
username: string,
|
||||
mail: string,
|
||||
accessControlList: string[],
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Translation
|
||||
* translation
|
||||
*/
|
||||
|
||||
import i18next from "i18next";
|
||||
@@ -7,7 +7,7 @@ import resourcesToBackend from "i18next-resources-to-backend";
|
||||
|
||||
i18next.use(resourcesToBackend((language: string, namespace: string) => import(`../../shared-operations/public/locales/${namespace}/${language}.json`)))
|
||||
.init({
|
||||
debug: true,
|
||||
debug: false,
|
||||
ns: ["common"], // Preload this namespace, no need to add the others, they will load once their module is loaded
|
||||
defaultNS: "common",
|
||||
fallbackLng: "en",
|
||||
@@ -25,16 +25,42 @@ console.log("Available Modules: ", listOperatorNames());
|
||||
* jobs
|
||||
*/
|
||||
|
||||
import "./jobs";
|
||||
import "./jobs/jobs-controller";
|
||||
|
||||
/*
|
||||
* API
|
||||
* EXPRESS
|
||||
*/
|
||||
|
||||
import express from "express";
|
||||
const app = express();
|
||||
const PORT = 8000;
|
||||
|
||||
/*
|
||||
* auth
|
||||
*/
|
||||
|
||||
import passport from "passport";
|
||||
import session from "express-session";
|
||||
import { initialize } from "./auth/passport-config";
|
||||
import auth from "./routes/auth/auth-controller";
|
||||
|
||||
app.use(session({
|
||||
secret: process.env.SESSION_SECRET || "default-secret",
|
||||
resave: false,
|
||||
saveUninitialized: false
|
||||
}));
|
||||
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
initialize(passport);
|
||||
|
||||
app.use("/auth", auth);
|
||||
|
||||
/*
|
||||
* api
|
||||
*/
|
||||
|
||||
import api from "./routes/api/api-controller";
|
||||
app.use("/api", api);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import { getOperatorByName } from "@stirling-pdf/shared-operations/src/workflow/
|
||||
import { Operator } from "@stirling-pdf/shared-operations/src/functions";
|
||||
|
||||
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
|
||||
import { respondWithPdfFiles } from "../../utils/endpoint-utils";
|
||||
import { respondWithPdfFiles } from "../../utils/response-utils";
|
||||
import { Action } from "@stirling-pdf/shared-operations/declarations/Action";
|
||||
import { JoiPDFFileSchema } from "@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi";
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ const upload = multer();
|
||||
|
||||
import { traverseOperations } from "@stirling-pdf/shared-operations/src/workflow/traverseOperations";
|
||||
import { PdfFile, RepresentationType } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
|
||||
import { respondWithPdfFiles } from "../../utils/endpoint-utils";
|
||||
import { respondWithPdfFiles } from "../../utils/response-utils";
|
||||
import { JoiPDFFileSchema } from "@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi";
|
||||
|
||||
interface Workflow {
|
||||
|
||||
12
server-node/src/routes/auth/auth-controller.ts
Normal file
12
server-node/src/routes/auth/auth-controller.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import express, { Request, Response } from "express";
|
||||
|
||||
import login from "./login-controller";
|
||||
import logout from "./logout-controller";
|
||||
import register from "./register-controller";
|
||||
import status from "./status-controller";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use("/", [login, logout, register, status]);
|
||||
|
||||
export default router;
|
||||
16
server-node/src/routes/auth/login-controller.ts
Normal file
16
server-node/src/routes/auth/login-controller.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import express from "express";
|
||||
const router = express.Router();
|
||||
|
||||
import passport from "passport";
|
||||
|
||||
router.post("/login", passport.authenticate(['local'], {
|
||||
successRedirect: '/auth/status',
|
||||
failureRedirect: '/auth/login/failure'
|
||||
}));
|
||||
|
||||
router.post('/login/password', passport.authenticate('local', {
|
||||
successRedirect: '/auth/status',
|
||||
failureRedirect: '/auth/login/failure'
|
||||
}));
|
||||
|
||||
export default router;
|
||||
11
server-node/src/routes/auth/logout-controller.ts
Normal file
11
server-node/src/routes/auth/logout-controller.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import express, { Request, Response } from "express";
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/logout', function(req, res, next) {
|
||||
req.logout(function(err) {
|
||||
if (err) { return next(err); }
|
||||
res.redirect('/');
|
||||
});
|
||||
});
|
||||
|
||||
export default router;
|
||||
8
server-node/src/routes/auth/register-controller.ts
Normal file
8
server-node/src/routes/auth/register-controller.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import express, { Request, Response } from "express";
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/register', async function(req: Request, res: Response) {
|
||||
//TODO: Register new user
|
||||
});
|
||||
|
||||
export default router;
|
||||
8
server-node/src/routes/auth/status-controller.ts
Normal file
8
server-node/src/routes/auth/status-controller.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import express, { Request, Response } from "express";
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/status', async function(req: Request, res: Response) {
|
||||
res.json({user: req.user})
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user