chore: restart the repository with a clean history
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
This commit is contained in:
438 files changed
+45070
No files matched your search
@@ -0,0 +1,75 @@
|
||||
const { Op } = require("sequelize");
|
||||
const Session = require("../models/Session");
|
||||
const Account = require("../models/Account");
|
||||
|
||||
const COOKIE_NAME = "frislit-admin";
|
||||
const TTL = 7 * 24 * 60 * 60 * 1000;
|
||||
const TOUCH_AFTER = 60_000;
|
||||
|
||||
class SessionStore {
|
||||
constructor() {
|
||||
this.sweeper = setInterval(() => this.sweep(), 60 * 60 * 1000);
|
||||
this.sweeper.unref();
|
||||
}
|
||||
|
||||
async create(accountId, meta = {}) {
|
||||
const session = await Session.create({ accountId, ip: meta.ip || null, userAgent: meta.userAgent || null });
|
||||
return session.get ? session.get("token") : session.token;
|
||||
}
|
||||
|
||||
async get(token) {
|
||||
if (!token) return null;
|
||||
const session = await Session.findOne({ where: { token: String(token) } });
|
||||
if (!session) return null;
|
||||
|
||||
if (Date.now() - new Date(session.lastSeen).getTime() > TTL) {
|
||||
await Session.destroy({ where: { id: session.id } });
|
||||
return null;
|
||||
}
|
||||
|
||||
const account = await Account.findByPk(session.accountId);
|
||||
if (!account) {
|
||||
await Session.destroy({ where: { id: session.id } });
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Date.now() - new Date(session.lastSeen).getTime() > TOUCH_AFTER) {
|
||||
await Session.update({ lastSeen: new Date() }, { where: { id: session.id } });
|
||||
}
|
||||
|
||||
return { id: session.id, account, username: account.username, role: account.role, createdAt: session.createdAt };
|
||||
}
|
||||
|
||||
remove(token) {
|
||||
return Session.destroy({ where: { token: String(token || "") } });
|
||||
}
|
||||
|
||||
forAccount(accountId) {
|
||||
return Session.destroy({ where: { accountId } });
|
||||
}
|
||||
|
||||
sweep() {
|
||||
return Session.destroy({ where: { lastSeen: { [Op.lt]: new Date(Date.now() - TTL) } } });
|
||||
}
|
||||
}
|
||||
|
||||
const parseCookies = header => {
|
||||
const out = [];
|
||||
for (const part of String(header || "").split(";")) {
|
||||
const index = part.indexOf("=");
|
||||
if (index === -1) continue;
|
||||
const name = part.slice(0, index).trim();
|
||||
if (name) out.push({ name, value: part.slice(index + 1).trim() });
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const tokenFromRequest = req => parseCookies(req.headers.cookie).find(cookie => cookie.name === COOKIE_NAME)?.value || null;
|
||||
|
||||
const sessionCookie = (token, secure) =>
|
||||
`${COOKIE_NAME}=${token}; Path=/@frislit; HttpOnly; SameSite=Lax; Max-Age=${TTL / 1000}${secure ? "; Secure" : ""}`;
|
||||
|
||||
const clearSessionCookie = secure =>
|
||||
`${COOKIE_NAME}=; Path=/@frislit; HttpOnly; SameSite=Lax; Max-Age=0${secure ? "; Secure" : ""}`;
|
||||
|
||||
module.exports = { SessionStore, tokenFromRequest, sessionCookie, clearSessionCookie };
|
||||
Reference in new issue
Block a user