@hazem3500/palette 0.9.110 → 0.9.111

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
@@ -1,4 +1,5 @@
1
1
  import { timingSafeEqual } from "node:crypto";
2
+ import { authOptions } from "./proxy-auth";
2
3
  import { check, string } from "./validation";
3
4
 
4
5
  // Personal access tokens let a bot act as its owner with a narrower grant.
@@ -136,6 +137,10 @@ export async function createToken(
136
137
  userId: string,
137
138
  data: Record<string, unknown>
138
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.");
143
+ }
139
144
  const name = string(data.name, 60, "token name").trim();
140
145
  check(name.length > 0, 400, "Name your token.");
141
146
  let expiresAt: number | null = null;
@@ -285,6 +290,15 @@ export async function authenticateToken(
285
290
  row.revoked_at === null &&
286
291
  (row.expires_at === null || row.expires_at > now);
287
292
  check(valid, 401, "This access token is invalid, expired, or revoked.");
293
+ if (authOptions(env).proxy) {
294
+ 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,
298
+ 403,
299
+ "Proxy access tokens require explicit projects and a lifetime of 1–365 days."
300
+ );
301
+ }
288
302
  const user = await env.DB.prepare("SELECT * FROM users WHERE id=?")
289
303
  .bind(row.user_id)
290
304
  .first<TokenUser>();