mirror of https://github.com/Budibase/budibase.git
17 changed files with 197 additions and 54 deletions
@ -0,0 +1,3 @@ |
|||
module.exports = { |
|||
user: require("./src/cache/user"), |
|||
} |
|||
@ -0,0 +1 @@ |
|||
module.exports = require("./src/security/sessions") |
|||
@ -0,0 +1,22 @@ |
|||
const { getDB } = require("../db") |
|||
const { StaticDatabases } = require("../db/utils") |
|||
const redis = require("../redis/authRedis") |
|||
|
|||
const EXPIRY_SECONDS = 3600 |
|||
|
|||
|
|||
exports.getUser = async userId => { |
|||
const client = await redis.getUserClient() |
|||
// try cache
|
|||
let user = await client.get(userId) |
|||
if (!user) { |
|||
user = await getDB(StaticDatabases.GLOBAL.name).get(userId) |
|||
client.store(userId, user, EXPIRY_SECONDS) |
|||
} |
|||
return user |
|||
} |
|||
|
|||
exports.invalidateUser = async userId => { |
|||
const client = await redis.getUserClient() |
|||
await client.delete(userId) |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
const { Client, utils } = require("./index") |
|||
|
|||
let userClient, sessionClient |
|||
|
|||
async function init() { |
|||
userClient = await new Client(utils.Databases.USER_CACHE).init() |
|||
sessionClient = await new Client(utils.Databases.SESSIONS).init() |
|||
} |
|||
|
|||
process.on("exit", async () => { |
|||
if (userClient) await userClient.finish() |
|||
if (sessionClient) await sessionClient.finish() |
|||
}) |
|||
|
|||
module.exports = { |
|||
getUserClient: async () => { |
|||
if (!userClient) { |
|||
await init() |
|||
} |
|||
return userClient |
|||
}, |
|||
getSessionClient: async () => { |
|||
if (!sessionClient) { |
|||
await init() |
|||
} |
|||
return sessionClient |
|||
}, |
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
const redis = require("../redis/authRedis") |
|||
|
|||
const EXPIRY_SECONDS = 86400 |
|||
|
|||
async function getSessionsForUser(userId) { |
|||
const client = await redis.getSessionClient() |
|||
return client.scan(userId) |
|||
} |
|||
|
|||
function makeSessionID(userId, sessionId) { |
|||
return `${userId}/${sessionId}` |
|||
} |
|||
|
|||
exports.createASession = async (userId, sessionId, token) => { |
|||
const client = await redis.getSessionClient() |
|||
await client.store(makeSessionID(userId, sessionId), token, EXPIRY_SECONDS) |
|||
} |
|||
|
|||
exports.invalidateSessions = async (userId, sessionId = null) => { |
|||
let sessions = [] |
|||
if (sessionId) { |
|||
sessions.push({ key: makeSessionID(userId, sessionId) }) |
|||
} else { |
|||
sessions = await getSessionsForUser(userId) |
|||
} |
|||
const client = await redis.getSessionClient() |
|||
const promises = [] |
|||
for (let session of sessions) { |
|||
promises.push(client.delete(session.key)) |
|||
} |
|||
await Promise.all(promises) |
|||
} |
|||
|
|||
exports.updateSessionTTL = async (userId, sessionId) => { |
|||
const client = await redis.getSessionClient() |
|||
await client.setExpiry(makeSessionID(userId, sessionId), EXPIRY_SECONDS) |
|||
} |
|||
|
|||
exports.endSession = async (userId, sessionId) => { |
|||
const client = await redis.getSessionClient() |
|||
await client.delete(makeSessionID(userId, sessionId)) |
|||
} |
|||
|
|||
exports.getSession = async (userId, sessionId) => { |
|||
try { |
|||
const client = await redis.getSessionClient() |
|||
return client.get(makeSessionID(userId, sessionId)) |
|||
} catch (err) { |
|||
// if can't get session don't error, just don't return anything
|
|||
return null |
|||
} |
|||
} |
|||
|
|||
exports.getAllSessions = async () => { |
|||
const client = await redis.getSessionClient() |
|||
return client.scan() |
|||
} |
|||
Loading…
Reference in new issue