@hazem3500/palette 0.9.113 → 0.9.115

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/server/tokens.ts CHANGED
@@ -99,9 +99,9 @@ export const pruneAudit = (db: D1Database, now: number) =>
99
99
  db.prepare("DELETE FROM audit_log WHERE at<?").bind(now - AUDIT_RETENTION);
100
100
 
101
101
  /** Projects an account holder may limit a token to: owned or joined. */
102
- export async function accountProjects(env: Env, userId: string) {
102
+ export async function accountProjects(env: Env, userId: string, activeMembership = false) {
103
103
  const rows = await env.DB.prepare(
104
- "SELECT id AS project FROM workspaces WHERE owner_id=? UNION SELECT project FROM project_members WHERE user_id=? UNION SELECT project FROM project_owners WHERE user_id=?"
104
+ `SELECT id AS project FROM workspaces WHERE owner_id=? UNION SELECT project FROM ${activeMembership ? "project_access" : "project_members"} WHERE user_id=? UNION SELECT project FROM project_owners WHERE user_id=?`
105
105
  )
106
106
  .bind(userId, userId, userId)
107
107
  .all<{ project: string }>();
@@ -137,9 +137,10 @@ export async function createToken(
137
137
  userId: string,
138
138
  data: Record<string, unknown>
139
139
  ) {
140
- if (authOptions(env).proxy) {
141
- check(data.expiresInDays !== undefined && data.expiresInDays !== null, 400, "Choose an expiry between 1 and 365 days.");
142
- check(Array.isArray(data.projects) && data.projects.length > 0, 400, "Choose explicit projects for this token.");
140
+ const auth = authOptions(env);
141
+ if (auth.proxy) {
142
+ check(auth.proxyTokenNoExpiry || (data.expiresInDays !== undefined && data.expiresInDays !== null), 400, "Choose an expiry between 1 and 365 days.");
143
+ check(auth.proxyTokenAllProjects || (Array.isArray(data.projects) && data.projects.length > 0), 400, "Choose explicit projects for this token.");
143
144
  }
144
145
  const name = string(data.name, 60, "token name").trim();
145
146
  check(name.length > 0, 400, "Name your token.");
@@ -290,13 +291,14 @@ export async function authenticateToken(
290
291
  row.revoked_at === null &&
291
292
  (row.expires_at === null || row.expires_at > now);
292
293
  check(valid, 401, "This access token is invalid, expired, or revoked.");
293
- if (authOptions(env).proxy) {
294
+ const auth = authOptions(env);
295
+ if (auth.proxy) {
294
296
  check(
295
- row.expires_at !== null && row.expires_at > row.created_at &&
296
- row.expires_at - row.created_at <= MAX_LIFETIME_DAYS * 86400000 &&
297
- (list(row.projects)?.length ?? 0) > 0,
297
+ (row.expires_at === null ? auth.proxyTokenNoExpiry :
298
+ row.expires_at > row.created_at && row.expires_at - row.created_at <= MAX_LIFETIME_DAYS * 86400000) &&
299
+ (list(row.projects) === null ? auth.proxyTokenAllProjects : (list(row.projects)?.length ?? 0) > 0),
298
300
  403,
299
- "Proxy access tokens require explicit projects and a lifetime of 1–365 days."
301
+ "This access token exceeds the proxy token policy."
300
302
  );
301
303
  }
302
304
  const user = await env.DB.prepare("SELECT * FROM users WHERE id=?")
@@ -321,7 +323,7 @@ export async function authenticateToken(
321
323
  );
322
324
  const projects = list(row.projects);
323
325
  check(
324
- !projects || projects.includes(project),
326
+ projects ? projects.includes(project) : !auth.proxy || (await accountProjects(env, row.user_id, true)).includes(project),
325
327
  403,
326
328
  "This token is not allowed for this project."
327
329
  );
package/src/types.ts CHANGED
@@ -285,6 +285,7 @@ export interface CommentsOptions {
285
285
  * Single-copy simulated viewport (with mode "iframe"): turning it on reloads this page as a
286
286
  * shell for Palette, so the app runs once, in the frame. Requires the app's entry
287
287
  * to skip mounting when `isShell(project)` from `@hazem3500/palette/shell` is true.
288
+ * Cancellation or failure keeps the normal page; a second app is never loaded as fallback.
288
289
  */
289
290
  shell?: boolean;
290
291
  };