@getcirrus/pds 0.12.0 → 0.13.0

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/dist/index.js CHANGED
@@ -10,7 +10,7 @@ import { Hono } from "hono";
10
10
  import { cors } from "hono/cors";
11
11
  import { isDid, isHandle, isNsid, isRecordKey } from "@atcute/lexicons/syntax";
12
12
  import { SignJWT, base64url, errors, jwtVerify } from "jose";
13
- import { compare, compare as compare$1 } from "bcryptjs";
13
+ import { compare, compare as compare$1, hash } from "bcryptjs";
14
14
  import { ATProtoOAuthProvider } from "@getcirrus/oauth-provider";
15
15
  import { generateAuthenticationOptions, generateRegistrationOptions, verifyAuthenticationResponse, verifyRegistrationResponse } from "@simplewebauthn/server";
16
16
  import { CompositeDidDocumentResolver, PlcDidDocumentResolver, WebDidDocumentResolver } from "@atcute/identity-resolver";
@@ -194,6 +194,13 @@ var SqliteRepoStorage = class extends ReadableBlockstore {
194
194
  expires_at INTEGER NOT NULL,
195
195
  name TEXT
196
196
  );
197
+
198
+ -- App passwords (AT Protocol com.atproto.server.createAppPassword)
199
+ CREATE TABLE IF NOT EXISTS app_passwords (
200
+ name TEXT PRIMARY KEY,
201
+ password_hash TEXT NOT NULL,
202
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
203
+ );
197
204
  `);
198
205
  try {
199
206
  this.sql.exec("ALTER TABLE repo_state ADD COLUMN email TEXT");
@@ -518,6 +525,39 @@ var SqliteRepoStorage = class extends ReadableBlockstore {
518
525
  cleanupPasskeyTokens() {
519
526
  this.sql.exec("DELETE FROM passkey_tokens WHERE expires_at < ?", Date.now());
520
527
  }
528
+ /**
529
+ * Save an app password (store bcrypt hash, not plaintext).
530
+ */
531
+ saveAppPassword(name, passwordHash) {
532
+ this.sql.exec(`INSERT INTO app_passwords (name, password_hash) VALUES (?, ?)`, name, passwordHash);
533
+ }
534
+ /**
535
+ * List all app passwords (names and creation dates only — never return hashes).
536
+ */
537
+ listAppPasswords() {
538
+ return this.sql.exec(`SELECT name, created_at FROM app_passwords ORDER BY created_at DESC`).toArray().map((row) => ({
539
+ name: row.name,
540
+ createdAt: row.created_at
541
+ }));
542
+ }
543
+ /**
544
+ * Delete an app password by name.
545
+ */
546
+ deleteAppPassword(name) {
547
+ const before = this.sql.exec("SELECT COUNT(*) as c FROM app_passwords").one();
548
+ this.sql.exec("DELETE FROM app_passwords WHERE name = ?", name);
549
+ const after = this.sql.exec("SELECT COUNT(*) as c FROM app_passwords").one();
550
+ return before.c > after.c;
551
+ }
552
+ /**
553
+ * Get all app password hashes for verification during login.
554
+ */
555
+ getAppPasswordHashes() {
556
+ return this.sql.exec(`SELECT name, password_hash FROM app_passwords`).toArray().map((row) => ({
557
+ name: row.name,
558
+ passwordHash: row.password_hash
559
+ }));
560
+ }
521
561
  };
522
562
 
523
563
  //#endregion
@@ -1872,6 +1912,22 @@ var AccountDurableObject = class extends DurableObject {
1872
1912
  async rpcConsumeWebAuthnChallenge(challenge) {
1873
1913
  return (await this.getOAuthStorage()).consumeWebAuthnChallenge(challenge);
1874
1914
  }
1915
+ /** Save an app password (bcrypt hash) */
1916
+ async rpcSaveAppPassword(name, passwordHash) {
1917
+ (await this.getStorage()).saveAppPassword(name, passwordHash);
1918
+ }
1919
+ /** List all app passwords (names and dates only) */
1920
+ async rpcListAppPasswords() {
1921
+ return (await this.getStorage()).listAppPasswords();
1922
+ }
1923
+ /** Delete an app password by name */
1924
+ async rpcDeleteAppPassword(name) {
1925
+ return (await this.getStorage()).deleteAppPassword(name);
1926
+ }
1927
+ /** Get all app password hashes for login verification */
1928
+ async rpcGetAppPasswordHashes() {
1929
+ return (await this.getStorage()).getAppPasswordHashes();
1930
+ }
1875
1931
  /**
1876
1932
  * HTTP fetch handler for WebSocket upgrades and streaming responses.
1877
1933
  * Used instead of RPC when the response can't be serialized (WebSocket)
@@ -3360,6 +3416,26 @@ async function listMissingBlobs(c, accountDO) {
3360
3416
 
3361
3417
  //#endregion
3362
3418
  //#region src/xrpc/server.ts
3419
+ /**
3420
+ * Generate an AT Protocol app password in the format xxxx-xxxx-xxxx-xxxx.
3421
+ * Uses crypto.getRandomValues for secure randomness.
3422
+ */
3423
+ function generateAppPassword() {
3424
+ const chars = "abcdefghijklmnopqrstuvwxyz";
3425
+ const bytes = new Uint8Array(16);
3426
+ crypto.getRandomValues(bytes);
3427
+ const groups = [];
3428
+ for (let g = 0; g < 4; g++) {
3429
+ let segment = "";
3430
+ for (let i = 0; i < 4; i++) segment += chars[bytes[g * 4 + i] % 26];
3431
+ groups.push(segment);
3432
+ }
3433
+ return groups.join("-");
3434
+ }
3435
+ /** Check if a string looks like an app password (xxxx-xxxx-xxxx-xxxx). */
3436
+ function isAppPassword(password) {
3437
+ return /^[a-z]{4}-[a-z]{4}-[a-z]{4}-[a-z]{4}$/.test(password);
3438
+ }
3363
3439
  async function describeServer(c) {
3364
3440
  return c.json({
3365
3441
  did: c.env.DID,
@@ -3368,7 +3444,8 @@ async function describeServer(c) {
3368
3444
  });
3369
3445
  }
3370
3446
  /**
3371
- * Create a new session (login)
3447
+ * Create a new session (login).
3448
+ * Accepts either the account password or an app password.
3372
3449
  */
3373
3450
  async function createSession(c, accountDO) {
3374
3451
  const { identifier, password } = await c.req.json();
@@ -3380,7 +3457,18 @@ async function createSession(c, accountDO) {
3380
3457
  error: "AuthenticationRequired",
3381
3458
  message: "Invalid identifier or password"
3382
3459
  }, 401);
3383
- if (!await compare$1(password, c.env.PASSWORD_HASH)) return c.json({
3460
+ if (isAppPassword(password)) {
3461
+ const appPasswords = await accountDO.rpcGetAppPasswordHashes();
3462
+ let matched = false;
3463
+ for (const ap of appPasswords) if (await compare$1(password, ap.passwordHash)) {
3464
+ matched = true;
3465
+ break;
3466
+ }
3467
+ if (!matched) return c.json({
3468
+ error: "AuthenticationRequired",
3469
+ message: "Invalid identifier or password"
3470
+ }, 401);
3471
+ } else if (!await compare$1(password, c.env.PASSWORD_HASH)) return c.json({
3384
3472
  error: "AuthenticationRequired",
3385
3473
  message: "Invalid identifier or password"
3386
3474
  }, 401);
@@ -3627,6 +3715,57 @@ async function resetMigration(c, accountDO) {
3627
3715
  }, 500);
3628
3716
  }
3629
3717
  }
3718
+ /**
3719
+ * Create an app password.
3720
+ * com.atproto.server.createAppPassword
3721
+ */
3722
+ async function createAppPassword(c, accountDO) {
3723
+ const body = await c.req.json();
3724
+ if (!body.name || body.name.trim().length === 0) return c.json({
3725
+ error: "InvalidRequest",
3726
+ message: "Missing required field: name"
3727
+ }, 400);
3728
+ const name = body.name.trim();
3729
+ if ((await accountDO.rpcListAppPasswords()).some((p) => p.name === name)) return c.json({
3730
+ error: "DuplicateName",
3731
+ message: `App password with name "${name}" already exists`
3732
+ }, 400);
3733
+ const password = generateAppPassword();
3734
+ const passwordHash = await hash(password, 10);
3735
+ await accountDO.rpcSaveAppPassword(name, passwordHash);
3736
+ return c.json({
3737
+ name,
3738
+ password,
3739
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
3740
+ });
3741
+ }
3742
+ /**
3743
+ * List app passwords (names and dates, never the passwords themselves).
3744
+ * com.atproto.server.listAppPasswords
3745
+ */
3746
+ async function listAppPasswords(c, accountDO) {
3747
+ const passwords = await accountDO.rpcListAppPasswords();
3748
+ return c.json({ passwords: passwords.map((p) => ({
3749
+ name: p.name,
3750
+ createdAt: p.createdAt
3751
+ })) });
3752
+ }
3753
+ /**
3754
+ * Revoke an app password by name.
3755
+ * com.atproto.server.revokeAppPassword
3756
+ */
3757
+ async function revokeAppPassword(c, accountDO) {
3758
+ const body = await c.req.json();
3759
+ if (!body.name) return c.json({
3760
+ error: "InvalidRequest",
3761
+ message: "Missing required field: name"
3762
+ }, 400);
3763
+ if (!await accountDO.rpcDeleteAppPassword(body.name)) return c.json({
3764
+ error: "InvalidRequest",
3765
+ message: `App password "${body.name}" not found`
3766
+ }, 400);
3767
+ return c.json({});
3768
+ }
3630
3769
 
3631
3770
  //#endregion
3632
3771
  //#region src/migration-token.ts
@@ -4230,7 +4369,7 @@ function renderDashboard(config) {
4230
4369
 
4231
4370
  //#endregion
4232
4371
  //#region package.json
4233
- var version = "0.12.0";
4372
+ var version = "0.13.0";
4234
4373
 
4235
4374
  //#endregion
4236
4375
  //#region src/index.ts
@@ -4417,6 +4556,9 @@ app.post("/xrpc/com.atproto.server.createSession", (c) => createSession(c, getAc
4417
4556
  app.post("/xrpc/com.atproto.server.refreshSession", (c) => refreshSession(c, getAccountDO(c.env)));
4418
4557
  app.get("/xrpc/com.atproto.server.getSession", (c) => getSession(c, getAccountDO(c.env)));
4419
4558
  app.post("/xrpc/com.atproto.server.deleteSession", deleteSession);
4559
+ app.post("/xrpc/com.atproto.server.createAppPassword", requireAuth, (c) => createAppPassword(c, getAccountDO(c.env)));
4560
+ app.get("/xrpc/com.atproto.server.listAppPasswords", requireAuth, (c) => listAppPasswords(c, getAccountDO(c.env)));
4561
+ app.post("/xrpc/com.atproto.server.revokeAppPassword", requireAuth, (c) => revokeAppPassword(c, getAccountDO(c.env)));
4420
4562
  app.get("/xrpc/com.atproto.server.checkAccountStatus", requireAuth, (c) => checkAccountStatus(c, getAccountDO(c.env)));
4421
4563
  app.post("/xrpc/com.atproto.server.activateAccount", requireAuth, (c) => activateAccount(c, getAccountDO(c.env)));
4422
4564
  app.post("/xrpc/com.atproto.server.deactivateAccount", requireAuth, (c) => deactivateAccount(c, getAccountDO(c.env)));