This repository was archived by the owner on Jul 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaccess.js
More file actions
249 lines (216 loc) · 7.07 KB
/
Copy pathaccess.js
File metadata and controls
249 lines (216 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { hash } from "@felix/argon2";
import { connectDB } from "./db.js";
import { verifyToken } from "./keys.js";
import { log } from "./logging.js";
const db = connectDB();
const MASTER_USER_SETTING = "master_user_id";
const DEFAULT_MASTER_CODE_FILE = ".master-code";
export const PERMISSIONS = Object.freeze({
MANAGE_PERMISSIONS: "moderation.manage_permissions",
BAN_USERS: "moderation.ban_users",
KICK_USERS: "moderation.kick_users",
DELETE_HOME_POSTS: "moderation.delete_home_posts",
SEND_INBOX_MESSAGES: "moderation.send_inbox_messages",
});
export const ALL_PERMISSIONS = Object.freeze(Object.values(PERMISSIONS));
export class AccessError extends Error {
constructor(message, status = 403, code = "ACCESS_DENIED", details) {
super(message);
this.name = "AccessError";
this.status = status;
this.code = code;
this.details = details;
}
}
let masterCodeDigest;
let masterUserId;
function randomHex(byteLength = 32) {
return Array.from(crypto.getRandomValues(new Uint8Array(byteLength)))
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
}
async function digest(value) {
return new Uint8Array(
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(value)),
);
}
function constantTimeEqual(left, right) {
if (!left || !right || left.length !== right.length) return false;
let difference = 0;
for (let index = 0; index < left.length; index++) {
difference |= left[index] ^ right[index];
}
return difference === 0;
}
async function loadMasterCode() {
const codeFile = Deno.env.get("MASTER_CODE_FILE") ||
DEFAULT_MASTER_CODE_FILE;
let code;
try {
code = (await Deno.readTextFile(codeFile)).trim();
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) throw error;
}
if (!code || code.length < 32) {
code = randomHex();
await Deno.writeTextFile(codeFile, `${code}\n`, { mode: 0o600 });
}
try {
await Deno.chmod(codeFile, 0o600);
} catch {
// Windows does not apply POSIX file modes. The file remains git-ignored.
}
masterCodeDigest = await digest(code);
log(`MASTER LOGIN CODE (${codeFile}): ${code}`, "yellow");
}
async function ensureMasterUser() {
const setting = db.prepare(
`SELECT setting_value FROM server_settings WHERE setting_key = ?`,
).get(MASTER_USER_SETTING);
if (setting?.setting_value) {
const user = db.prepare(
`SELECT uuid FROM users WHERE uuid = ?`,
).get(setting.setting_value);
if (user) {
masterUserId = user.uuid;
return;
}
}
let username = "maelink";
while (db.prepare(`SELECT 1 FROM users WHERE username = ?`).get(username)) {
username = `System_${randomHex(3)}`;
}
const uuid = crypto.randomUUID();
const unusablePassword = await hash(randomHex(48));
const passwordHash = typeof unusablePassword === "string"
? unusablePassword
: new TextDecoder().decode(unusablePassword);
db.prepare(
`INSERT INTO users (uuid, username, password, pfp, bio, auth_version)
VALUES (?, ?, ?, ?, ?, 0)`,
).run(uuid, username, passwordHash, null, "Server master account");
db.prepare(
`INSERT INTO server_settings (setting_key, setting_value) VALUES (?, ?)
ON CONFLICT(setting_key) DO UPDATE SET setting_value = excluded.setting_value`,
).run(MASTER_USER_SETTING, uuid);
masterUserId = uuid;
}
export async function initAccessControl() {
await loadMasterCode();
await ensureMasterUser();
}
export function isMasterUser(userId) {
return Boolean(masterUserId) && userId === masterUserId;
}
export function getMasterUser() {
if (!masterUserId) throw new Error("Access control is not initialized");
return db.prepare(
`SELECT uuid, username, pfp, bio, auth_version FROM users WHERE uuid = ?`,
).get(masterUserId);
}
export async function verifyMasterCode(code) {
if (typeof code !== "string" || !masterCodeDigest) return false;
return constantTimeEqual(await digest(code.trim()), masterCodeDigest);
}
export function getUserByIdentifier(identifier) {
if (typeof identifier !== "string" || !identifier.trim()) return null;
return db.prepare(
`SELECT uuid, username, pfp, bio, auth_version
FROM users WHERE uuid = ? OR username = ?`,
).get(identifier.trim(), identifier.trim()) ?? null;
}
export function getActiveBan(userId) {
const row = db.prepare(
`SELECT userID, reason, CAST(untilTs AS TEXT) as untilTsText,
createdBy, CAST(ts AS TEXT) as tsText
FROM global_bans WHERE userID = ?`,
).get(userId);
if (!row) return null;
const ban = {
userID: row.userID,
reason: row.reason,
untilTs: row.untilTsText === null ? null : Number(row.untilTsText),
createdBy: row.createdBy,
ts: Number(row.tsText),
};
if (ban.untilTs !== null && Number(ban.untilTs) <= Date.now()) {
db.prepare(`DELETE FROM global_bans WHERE userID = ?`).run(userId);
return null;
}
return ban;
}
export function assertUserAllowed(userId) {
const ban = getActiveBan(userId);
if (ban) {
throw new AccessError("This account is banned", 403, "ACCOUNT_BANNED", {
reason: ban.reason,
untilTs: ban.untilTs,
});
}
}
export function listPermissions(userId) {
if (isMasterUser(userId)) return [...ALL_PERMISSIONS];
return db.prepare(
`SELECT permission FROM user_permissions WHERE userID = ? ORDER BY permission`,
).all(userId).map((row) => row.permission);
}
export async function authenticateToken(token, options = {}) {
if (!token) {
throw new AccessError("Authentication required", 401, "AUTH_REQUIRED");
}
let payload;
try {
payload = await verifyToken(token);
} catch {
throw new AccessError(
"Invalid or expired token",
401,
"INVALID_TOKEN",
);
}
if (options.requiredType && payload.type !== options.requiredType) {
throw new AccessError("Wrong token type", 401, "INVALID_TOKEN_TYPE");
}
const user = db.prepare(
`SELECT uuid, username, auth_version FROM users WHERE uuid = ?`,
).get(payload.uuid);
if (!user) {
throw new AccessError("Account no longer exists", 401, "ACCOUNT_MISSING");
}
assertUserAllowed(user.uuid);
const tokenVersion = Number(payload.authVersion ?? 0);
if (tokenVersion !== Number(user.auth_version ?? 0)) {
throw new AccessError(
"Session has been revoked; log in again",
401,
"SESSION_REVOKED",
);
}
return {
...payload,
uuid: user.uuid,
username: user.username,
isMaster: isMasterUser(user.uuid),
permissions: listPermissions(user.uuid),
};
}
export async function requirePermission(token, permission) {
const principal = await authenticateToken(token, { requiredType: "access" });
if (!principal.isMaster && !principal.permissions.includes(permission)) {
throw new AccessError(
`Missing permission: ${permission}`,
403,
"MISSING_PERMISSION",
{ permission },
);
}
return principal;
}
export function revokeSessions(userId) {
const user = db.prepare(`SELECT uuid FROM users WHERE uuid = ?`).get(userId);
if (!user) return false;
db.prepare(
`UPDATE users SET auth_version = COALESCE(auth_version, 0) + 1 WHERE uuid = ?`,
).run(userId);
return true;
}