fix line endings
This commit is contained in:
parent
b261b4fc4b
commit
4bcacd4d7c
3 changed files with 103 additions and 101 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -8,3 +8,5 @@ npm-debug.log*
|
|||
|
||||
# Mac OS X
|
||||
.DS_Store
|
||||
|
||||
.vscode
|
66
db.js
66
db.js
|
@ -1,33 +1,33 @@
|
|||
const sqlite3 = require("sqlite3");
|
||||
const mkdirp = require("mkdirp");
|
||||
const crypto = require("crypto");
|
||||
|
||||
mkdirp.sync("./var/db");
|
||||
|
||||
let db = new sqlite3.Database("./var/db/media.db");
|
||||
|
||||
db.serialize(function() {
|
||||
// create the database schema for the todos app
|
||||
db.run("CREATE TABLE IF NOT EXISTS users ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
username TEXT UNIQUE, \
|
||||
hashed_password BLOB, \
|
||||
salt BLOB \
|
||||
)");
|
||||
|
||||
db.run("CREATE TABLE IF NOT EXISTS media ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
path TEXT NOT NULL, \
|
||||
expire INTEGER \
|
||||
)");
|
||||
|
||||
// create an initial user (username: alice, password: letmein)
|
||||
var salt = crypto.randomBytes(16);
|
||||
db.run("INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)", [
|
||||
"admin",
|
||||
crypto.pbkdf2Sync(process.env.EBPASS || "changeme", salt, 310000, 32, "sha256"),
|
||||
salt
|
||||
]);
|
||||
});
|
||||
|
||||
module.exports = db;
|
||||
const sqlite3 = require("sqlite3");
|
||||
const mkdirp = require("mkdirp");
|
||||
const crypto = require("crypto");
|
||||
|
||||
mkdirp.sync("./var/db");
|
||||
|
||||
let db = new sqlite3.Database("./var/db/media.db");
|
||||
|
||||
db.serialize(function() {
|
||||
// create the database schema for the todos app
|
||||
db.run("CREATE TABLE IF NOT EXISTS users ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
username TEXT UNIQUE, \
|
||||
hashed_password BLOB, \
|
||||
salt BLOB \
|
||||
)");
|
||||
|
||||
db.run("CREATE TABLE IF NOT EXISTS media ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
path TEXT NOT NULL, \
|
||||
expire INTEGER \
|
||||
)");
|
||||
|
||||
// create an initial user (username: alice, password: letmein)
|
||||
var salt = crypto.randomBytes(16);
|
||||
db.run("INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)", [
|
||||
"admin",
|
||||
crypto.pbkdf2Sync(process.env.EBPASS || "changeme", salt, 310000, 32, "sha256"),
|
||||
salt
|
||||
]);
|
||||
});
|
||||
|
||||
module.exports = db;
|
||||
|
|
136
routes/auth.js
136
routes/auth.js
|
@ -1,68 +1,68 @@
|
|||
let crypto = require("crypto");
|
||||
let express = require("express");
|
||||
let passport = require("passport");
|
||||
let LocalStrategy = require("passport-local");
|
||||
|
||||
let db = require("../db");
|
||||
|
||||
let router = express.Router();
|
||||
|
||||
passport.use(new LocalStrategy(function verify(username, password, cb) {
|
||||
db.get("SELECT * FROM users WHERE username = ?", [username], function(err, row) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!row) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password."
|
||||
});
|
||||
}
|
||||
|
||||
crypto.pbkdf2(password, row.salt, 310000, 32, "sha256", function(err, hashedPassword) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password."
|
||||
});
|
||||
}
|
||||
return cb(null, row);
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
passport.serializeUser(function(user, cb) {
|
||||
process.nextTick(function() {
|
||||
cb(null, {
|
||||
id: user.id,
|
||||
username: user.username
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(user, cb) {
|
||||
process.nextTick(function() {
|
||||
return cb(null, user);
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/login", function(req, res) {
|
||||
res.render("login");
|
||||
});
|
||||
|
||||
router.post("/login/password", passport.authenticate("local", {
|
||||
successRedirect: "/",
|
||||
failureRedirect: "/login"
|
||||
}));
|
||||
|
||||
router.post("/logout", function(req, res, next) {
|
||||
req.logout(function(err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.redirect("/");
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
let crypto = require("crypto");
|
||||
let express = require("express");
|
||||
let passport = require("passport");
|
||||
let LocalStrategy = require("passport-local");
|
||||
|
||||
let db = require("../db");
|
||||
|
||||
let router = express.Router();
|
||||
|
||||
passport.use(new LocalStrategy(function verify(username, password, cb) {
|
||||
db.get("SELECT * FROM users WHERE username = ?", [username], function(err, row) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!row) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password."
|
||||
});
|
||||
}
|
||||
|
||||
crypto.pbkdf2(password, row.salt, 310000, 32, "sha256", function(err, hashedPassword) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password."
|
||||
});
|
||||
}
|
||||
return cb(null, row);
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
passport.serializeUser(function(user, cb) {
|
||||
process.nextTick(function() {
|
||||
cb(null, {
|
||||
id: user.id,
|
||||
username: user.username
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(user, cb) {
|
||||
process.nextTick(function() {
|
||||
return cb(null, user);
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/login", function(req, res) {
|
||||
res.render("login");
|
||||
});
|
||||
|
||||
router.post("/login/password", passport.authenticate("local", {
|
||||
successRedirect: "/",
|
||||
failureRedirect: "/login"
|
||||
}));
|
||||
|
||||
router.post("/logout", function(req, res, next) {
|
||||
req.logout(function(err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.redirect("/");
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue