Skip to content

Commit

Permalink
Merge pull request #25 from afaneca/develop
Browse files Browse the repository at this point in the history
1.2.1
  • Loading branch information
afaneca authored Nov 7, 2023
2 parents 60ae113 + cec1ed4 commit 4fd0d7c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"db:seed": "prisma db seed"
},
"name": "myfin-api",
"version": "1.2.0",
"version": "1.2.1",
"description": "NodeJS API for Myfin",
"main": "src/server.js",
"devDependencies": {
Expand Down
82 changes: 40 additions & 42 deletions src/utils/sessionManager.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { prisma } from "../config/prisma.js";
import {prisma} from "../config/prisma.js";
import DateTimeUtils from "./DateTimeUtils.js";
import APIError from "../errorHandling/apiError.js";
import { generateUuid } from "./CryptoUtils.js";
import {generateUuid} from "./CryptoUtils.js";
import Logger from "./Logger.js";

const User = prisma.users;

const updateUserSessionKeyValue = async (username: string, newSessionKey: string, mobile = false) => {
const sessionKeyAttr = mobile ? 'sessionkey_mobile' : 'sessionkey';
if (!mobile) {
await User.update({
where: { username },
data: { [sessionKeyAttr]: newSessionKey },
});
}
const sessionKeyAttr = mobile ? 'sessionkey_mobile' : 'sessionkey';
if (!mobile) {
await User.update({
where: {username},
data: {[sessionKeyAttr]: newSessionKey},
});
}
};

const updateUserTrustlimitValue = async (username: string, newTrustLimit: number, mobile = false) => {
const trustLimitAttr = mobile ? 'trustlimit_mobile' : 'trustlimit';
if (!mobile) {
const trustLimitAttr = mobile ? 'trustlimit_mobile' : 'trustlimit';
await User.update({
where: { username },
data: { [trustLimitAttr]: newTrustLimit },
where: {username},
data: {[trustLimitAttr]: newTrustLimit},
});
}
};

/**
Expand All @@ -32,47 +30,47 @@ const updateUserTrustlimitValue = async (username: string, newTrustLimit: number
* @param mobile
*/
const extendUserSession = (username: string, mobile = false) => {
const renewTimeInSeconds = mobile ? 30 * 24 * 60 * 60 /* 30 days */ : 30 * 60; /* 30 minutes */
const newTrustLimit = DateTimeUtils.getCurrentUnixTimestamp() + renewTimeInSeconds;
updateUserTrustlimitValue(username, newTrustLimit, mobile);
return newTrustLimit;
const renewTimeInSeconds = mobile ? 30 * 24 * 60 * 60 /* 30 days */ : 30 * 60; /* 30 minutes */
const newTrustLimit = DateTimeUtils.getCurrentUnixTimestamp() + renewTimeInSeconds;
updateUserTrustlimitValue(username, newTrustLimit, mobile);
return newTrustLimit;
};

const checkIfUserExists = async (username: string, key: string) => {
const whereCondition = {
username: username,
OR: [{ sessionkey: key }, { sessionkey_mobile: key }],
};
const whereCondition = {
username: username,
OR: [{sessionkey: key}, {sessionkey_mobile: key}],
};

return User.findUnique({
where: whereCondition,
}).catch(() => null);
return User.findUnique({
where: whereCondition,
}).catch(() => null);
};

const checkIfTrustLimitHasExpired = (trustlimit: number) => {
const currentUnixTime = DateTimeUtils.getCurrentUnixTimestamp();
return currentUnixTime >= trustlimit;
const currentUnixTime = DateTimeUtils.getCurrentUnixTimestamp();
return currentUnixTime >= trustlimit;
};

const generateNewSessionKeyForUser = async (username: string, mobile = false) => {
const newKey = generateUuid();
await updateUserSessionKeyValue(username, newKey, mobile);
const newTrustlimit = extendUserSession(username, mobile);
return { sessionkey: newKey, trustlimit: newTrustlimit };
const newKey = generateUuid();
await updateUserSessionKeyValue(username, newKey, mobile);
const newTrustlimit = extendUserSession(username, mobile);
return {sessionkey: newKey, trustlimit: newTrustlimit};
};

const checkIfSessionKeyIsValid = async (key: string, username: string, renewTrustLimit = true, mobile = false) => {
const userData = await checkIfUserExists(username, key);
if (userData) {
// User exists, check if trustlimit has expired
if (checkIfTrustLimitHasExpired(mobile ? userData.trustlimit_mobile : userData.trustlimit)) {
throw APIError.notAuthorized('Session is not valid.');
const userData = await checkIfUserExists(username, key);
if (userData) {
// User exists, check if trustlimit has expired
if (checkIfTrustLimitHasExpired(mobile ? userData.trustlimit_mobile : userData.trustlimit)) {
throw APIError.notAuthorized('Session is not valid.');
}
if (renewTrustLimit) extendUserSession(username, mobile);
return true;
}
if (renewTrustLimit) extendUserSession(username, mobile);
return true;
}
Logger.addLog('USER AND/OR SESSION NOT FOUND');
throw APIError.notAuthorized();
Logger.addLog('USER AND/OR SESSION NOT FOUND');
throw APIError.notAuthorized();
};

export default { checkIfSessionKeyIsValid, generateNewSessionKeyForUser };
export default {checkIfSessionKeyIsValid, generateNewSessionKeyForUser};

0 comments on commit 4fd0d7c

Please sign in to comment.