aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-01-13 15:46:53 -0800
committerAdam Soltys <[email protected]>2021-01-13 16:11:11 -0800
commit5cd96cc027be811116b147e0614aca39ce164c6b (patch)
treea9d7d9096bc4ea5bb7049500571e9641b50177d7 /lib
parenteb860d78b836d810f7c29d5587e17321adf20bef (diff)
downloadcoinos-server-5cd96cc027be811116b147e0614aca39ce164c6b.tar.gz
coinos-server-5cd96cc027be811116b147e0614aca39ce164c6b.zip
account locking mechanism
Diffstat (limited to 'lib')
-rw-r--r--lib/passport.js15
-rw-r--r--lib/send.js1
-rw-r--r--lib/sockets.js7
3 files changed, 17 insertions, 6 deletions
diff --git a/lib/passport.js b/lib/passport.js
index b26b445e..70e002b9 100644
--- a/lib/passport.js
+++ b/lib/passport.js
@@ -4,7 +4,7 @@ const jwt = require("passport-jwt");
function cookieExtractor(req) {
if (req && req.cookies) {
return req.cookies["token"];
- }
+ }
return null;
}
@@ -20,9 +20,14 @@ passport = Passport.use(
async (payload, next) => {
const { username } = payload;
try {
- next(null, await db.User.findOne({ where: { username }, include: { model: db.Account, as: 'account' } }));
+ let user = await db.User.findOne({
+ where: { username },
+ include: { model: db.Account, as: "account" }
+ });
+ if (user.locked) throw new Error("account is locked");
+ next(null, user);
} catch (e) {
- l.error("error finding user for session", e);
+ l.error("problem logging in", e);
next(null, { username: "guest" });
}
}
@@ -31,8 +36,8 @@ passport = Passport.use(
auth = passport.authenticate("jwt", { session: false });
-optionalAuth = function (req, res, next) {
- passport.authenticate("jwt", { session: false }, function (err, user, info) {
+optionalAuth = function(req, res, next) {
+ passport.authenticate("jwt", { session: false }, function(err, user, info) {
req.user = user;
next();
})(req, res, next);
diff --git a/lib/send.js b/lib/send.js
index 232d6054..b4fc9ac9 100644
--- a/lib/send.js
+++ b/lib/send.js
@@ -65,6 +65,7 @@ send = async (amount, memo, hash, user) => {
total = parseInt(total);
fee = parseInt(fee);
+ await new Promise((resolve) => setTimeout(resolve, 5000));
account.balance -= total;
await account.save({ transaction });
diff --git a/lib/sockets.js b/lib/sockets.js
index 1ed18610..25f8aed8 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -106,6 +106,7 @@ wss.on("connection", function(ws, req) {
names[id] = username;
let user = await getUser(username);
+ if (user.locked) throw new Error("Account is locked");
let payments = await user.getPayments({
where: {
account_id: user.account_id
@@ -131,9 +132,13 @@ wss.on("connection", function(ws, req) {
);
} catch (e) {
l.error("caught socket error", e.message);
+ if (e.message.includes("locked")) {
+ ws.send(JSON.stringify({ type: "locked" }));
+ ws.send(JSON.stringify({ type: "logout" }));
+ }
if (e.message.includes("not found"))
ws.send(JSON.stringify({ type: "logout" }));
- setTimeout(ws.close, 1000);
+ setTimeout(() => ws && ws.close(), 1000);
}
break;
case "token":