pulse-hub 3.5.0 → 3.6.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/CHANGELOG.md +6 -0
- package/backend/.env.example +52 -52
- package/backend/agent.js +372 -372
- package/backend/cli.js +23 -1
- package/backend/emails/env-backup.html +52 -52
- package/backend/emails/invite.html +53 -53
- package/backend/lib/agent-rpc.js +72 -72
- package/backend/lib/async-route.js +17 -17
- package/backend/lib/authz/bootstrap.js +35 -35
- package/backend/lib/authz/catalog.js +291 -291
- package/backend/lib/authz/delegation.js +104 -104
- package/backend/lib/authz/effective.js +104 -104
- package/backend/lib/authz/engine.js +251 -251
- package/backend/lib/authz/index.js +134 -134
- package/backend/lib/authz/migrate.js +871 -871
- package/backend/lib/authz/mutate.js +87 -87
- package/backend/lib/authz/preview.js +327 -327
- package/backend/lib/authz/scope.js +282 -282
- package/backend/lib/env-mask.js +131 -131
- package/backend/lib/invite-helpers.js +83 -83
- package/backend/lib/invites.js +160 -160
- package/backend/lib/logs.js +244 -244
- package/backend/lib/metrics.js +249 -249
- package/backend/lib/relay-state.js +564 -564
- package/backend/lib/session.js +48 -48
- package/backend/middleware/authz.js +65 -65
- package/backend/public/assets/favicon.svg +6 -6
- package/backend/public/assets/index.js +1 -1
- package/backend/public/index.html +23 -24
- package/backend/routes/access.js +123 -123
- package/backend/routes/audit.js +41 -41
- package/backend/routes/auth.js +207 -207
- package/backend/routes/invites-public.js +91 -91
- package/backend/routes/invites.js +121 -121
- package/backend/routes/logs.js +68 -68
- package/backend/routes/metrics.js +76 -76
- package/backend/routes/policies.js +240 -240
- package/backend/routes/teams.js +221 -221
- package/backend/routes/users.js +100 -100
- package/backend/seed-admins.js +68 -68
- package/backend/server.js +106 -106
- package/backend/ws/agent-relay.js +159 -159
- package/package.json +1 -1
package/backend/lib/invites.js
CHANGED
|
@@ -1,160 +1,160 @@
|
|
|
1
|
-
// Invite lifecycle — mirrors the existing agent-token pattern in lib/instance-registry.js
|
|
2
|
-
// (registerInstance/verifyInstanceToken): a random token is returned to the caller
|
|
3
|
-
// exactly once and only ever stored scrypt-hashed, never in plaintext or logged. See
|
|
4
|
-
// .claude/plans/sqlite-oauth-invites.md, "Phase 3".
|
|
5
|
-
const crypto = require('crypto');
|
|
6
|
-
const db = require('./db');
|
|
7
|
-
const { hashSecret, timingSafeStringsEqual } = require('./crypto-helpers');
|
|
8
|
-
|
|
9
|
-
const MIN_EXPIRY_HOURS = 1;
|
|
10
|
-
const MAX_EXPIRY_HOURS = 7 * 24; // "1 week max" — the inviting user picks within this range
|
|
11
|
-
const DEFAULT_EXPIRY_HOURS = MAX_EXPIRY_HOURS;
|
|
12
|
-
|
|
13
|
-
function clampExpiryHours(hours) {
|
|
14
|
-
const n = Number(hours);
|
|
15
|
-
if (!Number.isFinite(n)) return DEFAULT_EXPIRY_HOURS;
|
|
16
|
-
return Math.min(MAX_EXPIRY_HOURS, Math.max(MIN_EXPIRY_HOURS, Math.round(n)));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// `teamIds` are the teams the invitee joins on accept (routes/auth.js applies them). The
|
|
20
|
-
// caller (routes/invites.js) has already checked the inviter may delegate everything those
|
|
21
|
-
// teams grant; this function trusts its caller, same as every other writer in this file.
|
|
22
|
-
function createInvite(email, invitedByUserId, expiresInHours, teamIds = []) {
|
|
23
|
-
const id = crypto.randomBytes(12).toString('hex');
|
|
24
|
-
const token = crypto.randomBytes(24).toString('hex');
|
|
25
|
-
const salt = crypto.randomBytes(16).toString('hex');
|
|
26
|
-
const now = Date.now();
|
|
27
|
-
const expiresAt = now + clampExpiryHours(expiresInHours) * 60 * 60 * 1000;
|
|
28
|
-
db.db
|
|
29
|
-
.prepare(
|
|
30
|
-
`INSERT INTO invites
|
|
31
|
-
(id, email, token_salt, token_hash, invited_by_user_id, status, team_ids, created_at, expires_at)
|
|
32
|
-
VALUES (?, ?, ?, ?, ?, 'pending', ?, ?, ?)`,
|
|
33
|
-
)
|
|
34
|
-
.run(
|
|
35
|
-
id,
|
|
36
|
-
email,
|
|
37
|
-
salt,
|
|
38
|
-
hashSecret(token, salt),
|
|
39
|
-
invitedByUserId,
|
|
40
|
-
JSON.stringify(Array.isArray(teamIds) ? teamIds : []),
|
|
41
|
-
now,
|
|
42
|
-
expiresAt,
|
|
43
|
-
);
|
|
44
|
-
return { id, token, expiresAt };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function parseTeamIds(raw) {
|
|
48
|
-
try {
|
|
49
|
-
const v = JSON.parse(raw || '[]');
|
|
50
|
-
return Array.isArray(v) ? v.map(String) : [];
|
|
51
|
-
} catch (e) {
|
|
52
|
-
return [];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function getInviteById(id) {
|
|
57
|
-
return db.db.prepare('SELECT * FROM invites WHERE id = ?').get(id);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Valid only while pending and not expired — an accepted/revoked/expired invite always
|
|
61
|
-
// fails verification, regardless of whether the token itself would still hash-match.
|
|
62
|
-
function verifyInviteToken(inviteId, token) {
|
|
63
|
-
const invite = getInviteById(inviteId);
|
|
64
|
-
if (!invite) return null;
|
|
65
|
-
if (invite.status !== 'pending') return null;
|
|
66
|
-
if (Date.now() >= invite.expires_at) return null;
|
|
67
|
-
if (!timingSafeStringsEqual(hashSecret(token || '', invite.token_salt), invite.token_hash))
|
|
68
|
-
return null;
|
|
69
|
-
return invite;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Deliberately ignores expires_at, unlike verifyInviteToken above — this backs the
|
|
73
|
-
// "trouble with this link?" button in the invite email, which needs to keep working
|
|
74
|
-
// past expiry (an expired link is exactly the case it exists to handle). Still requires
|
|
75
|
-
// status 'pending': an already-accepted or revoked invite has nothing left to resend.
|
|
76
|
-
function verifyPendingInviteTokenIgnoringExpiry(inviteId, token) {
|
|
77
|
-
const invite = getInviteById(inviteId);
|
|
78
|
-
if (!invite) return null;
|
|
79
|
-
if (invite.status !== 'pending') return null;
|
|
80
|
-
if (!timingSafeStringsEqual(hashSecret(token || '', invite.token_salt), invite.token_hash))
|
|
81
|
-
return null;
|
|
82
|
-
return invite;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Called on every successful sign-in (see routes/auth.js's /auth/google/callback), not just
|
|
86
|
-
// the "this invite is what created the account" path — someone can also end up with a
|
|
87
|
-
// working account while a pending invite for their email still exists (e.g. they were
|
|
88
|
-
// separately seeded via seed-admins.js, or already had an account some other way, and
|
|
89
|
-
// happened to sign in through a plain login rather than clicking their invite link).
|
|
90
|
-
// Without this, that invite would sit 'pending' in the Team tab forever, even though
|
|
91
|
-
// they've clearly already joined. Scoped to `status = 'pending'` so revoked/already-
|
|
92
|
-
// accepted rows are untouched.
|
|
93
|
-
function getPendingInvitesForEmail(email) {
|
|
94
|
-
if (!email) return [];
|
|
95
|
-
return db.db
|
|
96
|
-
.prepare(`SELECT * FROM invites WHERE status = 'pending' AND LOWER(email) = LOWER(?)`)
|
|
97
|
-
.all(email);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function autoAcceptPendingInvitesForEmail(email) {
|
|
101
|
-
if (!email) return;
|
|
102
|
-
db.db
|
|
103
|
-
.prepare(
|
|
104
|
-
`UPDATE invites SET status = 'accepted', accepted_at = ? WHERE status = 'pending' AND LOWER(email) = LOWER(?)`,
|
|
105
|
-
)
|
|
106
|
-
.run(Date.now(), email);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function revokeInvite(inviteId) {
|
|
110
|
-
db.db.prepare(`UPDATE invites SET status = 'revoked' WHERE id = ?`).run(inviteId);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Idempotent by design — clicking the email's "trouble with this link?" button twice
|
|
114
|
-
// just keeps the latest timestamp, no separate request log.
|
|
115
|
-
function markResendRequested(inviteId) {
|
|
116
|
-
db.db
|
|
117
|
-
.prepare(`UPDATE invites SET resend_requested_at = ? WHERE id = ?`)
|
|
118
|
-
.run(Date.now(), inviteId);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function listPendingInvites() {
|
|
122
|
-
return db.db
|
|
123
|
-
.prepare(
|
|
124
|
-
`SELECT id, email, invited_by_user_id, team_ids, created_at, expires_at, resend_requested_at
|
|
125
|
-
FROM invites WHERE status = 'pending' ORDER BY created_at DESC`,
|
|
126
|
-
)
|
|
127
|
-
.all();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// One-time-per-boot cleanup for invites that got stuck 'pending' before
|
|
131
|
-
// autoAcceptPendingInvitesForEmail existed above (or for any other reason a now-linked
|
|
132
|
-
// user's invite was never marked accepted) — anyone who already signed in successfully
|
|
133
|
-
// clearly doesn't need their invite anymore. Cheap (nothing to do once caught up) and
|
|
134
|
-
// idempotent, so this just runs on every hub startup rather than needing a one-time
|
|
135
|
-
// migration flag like lib/db.js's schema migrations use.
|
|
136
|
-
db.db
|
|
137
|
-
.prepare(
|
|
138
|
-
`
|
|
139
|
-
UPDATE invites SET status = 'accepted', accepted_at = ?
|
|
140
|
-
WHERE status = 'pending' AND EXISTS (
|
|
141
|
-
SELECT 1 FROM users WHERE users.google_id IS NOT NULL AND LOWER(users.email) = LOWER(invites.email)
|
|
142
|
-
)
|
|
143
|
-
`,
|
|
144
|
-
)
|
|
145
|
-
.run(Date.now());
|
|
146
|
-
|
|
147
|
-
module.exports = {
|
|
148
|
-
MIN_EXPIRY_HOURS,
|
|
149
|
-
MAX_EXPIRY_HOURS,
|
|
150
|
-
createInvite,
|
|
151
|
-
parseTeamIds,
|
|
152
|
-
getInviteById,
|
|
153
|
-
verifyInviteToken,
|
|
154
|
-
verifyPendingInviteTokenIgnoringExpiry,
|
|
155
|
-
getPendingInvitesForEmail,
|
|
156
|
-
autoAcceptPendingInvitesForEmail,
|
|
157
|
-
revokeInvite,
|
|
158
|
-
markResendRequested,
|
|
159
|
-
listPendingInvites,
|
|
160
|
-
};
|
|
1
|
+
// Invite lifecycle — mirrors the existing agent-token pattern in lib/instance-registry.js
|
|
2
|
+
// (registerInstance/verifyInstanceToken): a random token is returned to the caller
|
|
3
|
+
// exactly once and only ever stored scrypt-hashed, never in plaintext or logged. See
|
|
4
|
+
// .claude/plans/sqlite-oauth-invites.md, "Phase 3".
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
const db = require('./db');
|
|
7
|
+
const { hashSecret, timingSafeStringsEqual } = require('./crypto-helpers');
|
|
8
|
+
|
|
9
|
+
const MIN_EXPIRY_HOURS = 1;
|
|
10
|
+
const MAX_EXPIRY_HOURS = 7 * 24; // "1 week max" — the inviting user picks within this range
|
|
11
|
+
const DEFAULT_EXPIRY_HOURS = MAX_EXPIRY_HOURS;
|
|
12
|
+
|
|
13
|
+
function clampExpiryHours(hours) {
|
|
14
|
+
const n = Number(hours);
|
|
15
|
+
if (!Number.isFinite(n)) return DEFAULT_EXPIRY_HOURS;
|
|
16
|
+
return Math.min(MAX_EXPIRY_HOURS, Math.max(MIN_EXPIRY_HOURS, Math.round(n)));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// `teamIds` are the teams the invitee joins on accept (routes/auth.js applies them). The
|
|
20
|
+
// caller (routes/invites.js) has already checked the inviter may delegate everything those
|
|
21
|
+
// teams grant; this function trusts its caller, same as every other writer in this file.
|
|
22
|
+
function createInvite(email, invitedByUserId, expiresInHours, teamIds = []) {
|
|
23
|
+
const id = crypto.randomBytes(12).toString('hex');
|
|
24
|
+
const token = crypto.randomBytes(24).toString('hex');
|
|
25
|
+
const salt = crypto.randomBytes(16).toString('hex');
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
const expiresAt = now + clampExpiryHours(expiresInHours) * 60 * 60 * 1000;
|
|
28
|
+
db.db
|
|
29
|
+
.prepare(
|
|
30
|
+
`INSERT INTO invites
|
|
31
|
+
(id, email, token_salt, token_hash, invited_by_user_id, status, team_ids, created_at, expires_at)
|
|
32
|
+
VALUES (?, ?, ?, ?, ?, 'pending', ?, ?, ?)`,
|
|
33
|
+
)
|
|
34
|
+
.run(
|
|
35
|
+
id,
|
|
36
|
+
email,
|
|
37
|
+
salt,
|
|
38
|
+
hashSecret(token, salt),
|
|
39
|
+
invitedByUserId,
|
|
40
|
+
JSON.stringify(Array.isArray(teamIds) ? teamIds : []),
|
|
41
|
+
now,
|
|
42
|
+
expiresAt,
|
|
43
|
+
);
|
|
44
|
+
return { id, token, expiresAt };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function parseTeamIds(raw) {
|
|
48
|
+
try {
|
|
49
|
+
const v = JSON.parse(raw || '[]');
|
|
50
|
+
return Array.isArray(v) ? v.map(String) : [];
|
|
51
|
+
} catch (e) {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getInviteById(id) {
|
|
57
|
+
return db.db.prepare('SELECT * FROM invites WHERE id = ?').get(id);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Valid only while pending and not expired — an accepted/revoked/expired invite always
|
|
61
|
+
// fails verification, regardless of whether the token itself would still hash-match.
|
|
62
|
+
function verifyInviteToken(inviteId, token) {
|
|
63
|
+
const invite = getInviteById(inviteId);
|
|
64
|
+
if (!invite) return null;
|
|
65
|
+
if (invite.status !== 'pending') return null;
|
|
66
|
+
if (Date.now() >= invite.expires_at) return null;
|
|
67
|
+
if (!timingSafeStringsEqual(hashSecret(token || '', invite.token_salt), invite.token_hash))
|
|
68
|
+
return null;
|
|
69
|
+
return invite;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Deliberately ignores expires_at, unlike verifyInviteToken above — this backs the
|
|
73
|
+
// "trouble with this link?" button in the invite email, which needs to keep working
|
|
74
|
+
// past expiry (an expired link is exactly the case it exists to handle). Still requires
|
|
75
|
+
// status 'pending': an already-accepted or revoked invite has nothing left to resend.
|
|
76
|
+
function verifyPendingInviteTokenIgnoringExpiry(inviteId, token) {
|
|
77
|
+
const invite = getInviteById(inviteId);
|
|
78
|
+
if (!invite) return null;
|
|
79
|
+
if (invite.status !== 'pending') return null;
|
|
80
|
+
if (!timingSafeStringsEqual(hashSecret(token || '', invite.token_salt), invite.token_hash))
|
|
81
|
+
return null;
|
|
82
|
+
return invite;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Called on every successful sign-in (see routes/auth.js's /auth/google/callback), not just
|
|
86
|
+
// the "this invite is what created the account" path — someone can also end up with a
|
|
87
|
+
// working account while a pending invite for their email still exists (e.g. they were
|
|
88
|
+
// separately seeded via seed-admins.js, or already had an account some other way, and
|
|
89
|
+
// happened to sign in through a plain login rather than clicking their invite link).
|
|
90
|
+
// Without this, that invite would sit 'pending' in the Team tab forever, even though
|
|
91
|
+
// they've clearly already joined. Scoped to `status = 'pending'` so revoked/already-
|
|
92
|
+
// accepted rows are untouched.
|
|
93
|
+
function getPendingInvitesForEmail(email) {
|
|
94
|
+
if (!email) return [];
|
|
95
|
+
return db.db
|
|
96
|
+
.prepare(`SELECT * FROM invites WHERE status = 'pending' AND LOWER(email) = LOWER(?)`)
|
|
97
|
+
.all(email);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function autoAcceptPendingInvitesForEmail(email) {
|
|
101
|
+
if (!email) return;
|
|
102
|
+
db.db
|
|
103
|
+
.prepare(
|
|
104
|
+
`UPDATE invites SET status = 'accepted', accepted_at = ? WHERE status = 'pending' AND LOWER(email) = LOWER(?)`,
|
|
105
|
+
)
|
|
106
|
+
.run(Date.now(), email);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function revokeInvite(inviteId) {
|
|
110
|
+
db.db.prepare(`UPDATE invites SET status = 'revoked' WHERE id = ?`).run(inviteId);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Idempotent by design — clicking the email's "trouble with this link?" button twice
|
|
114
|
+
// just keeps the latest timestamp, no separate request log.
|
|
115
|
+
function markResendRequested(inviteId) {
|
|
116
|
+
db.db
|
|
117
|
+
.prepare(`UPDATE invites SET resend_requested_at = ? WHERE id = ?`)
|
|
118
|
+
.run(Date.now(), inviteId);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function listPendingInvites() {
|
|
122
|
+
return db.db
|
|
123
|
+
.prepare(
|
|
124
|
+
`SELECT id, email, invited_by_user_id, team_ids, created_at, expires_at, resend_requested_at
|
|
125
|
+
FROM invites WHERE status = 'pending' ORDER BY created_at DESC`,
|
|
126
|
+
)
|
|
127
|
+
.all();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// One-time-per-boot cleanup for invites that got stuck 'pending' before
|
|
131
|
+
// autoAcceptPendingInvitesForEmail existed above (or for any other reason a now-linked
|
|
132
|
+
// user's invite was never marked accepted) — anyone who already signed in successfully
|
|
133
|
+
// clearly doesn't need their invite anymore. Cheap (nothing to do once caught up) and
|
|
134
|
+
// idempotent, so this just runs on every hub startup rather than needing a one-time
|
|
135
|
+
// migration flag like lib/db.js's schema migrations use.
|
|
136
|
+
db.db
|
|
137
|
+
.prepare(
|
|
138
|
+
`
|
|
139
|
+
UPDATE invites SET status = 'accepted', accepted_at = ?
|
|
140
|
+
WHERE status = 'pending' AND EXISTS (
|
|
141
|
+
SELECT 1 FROM users WHERE users.google_id IS NOT NULL AND LOWER(users.email) = LOWER(invites.email)
|
|
142
|
+
)
|
|
143
|
+
`,
|
|
144
|
+
)
|
|
145
|
+
.run(Date.now());
|
|
146
|
+
|
|
147
|
+
module.exports = {
|
|
148
|
+
MIN_EXPIRY_HOURS,
|
|
149
|
+
MAX_EXPIRY_HOURS,
|
|
150
|
+
createInvite,
|
|
151
|
+
parseTeamIds,
|
|
152
|
+
getInviteById,
|
|
153
|
+
verifyInviteToken,
|
|
154
|
+
verifyPendingInviteTokenIgnoringExpiry,
|
|
155
|
+
getPendingInvitesForEmail,
|
|
156
|
+
autoAcceptPendingInvitesForEmail,
|
|
157
|
+
revokeInvite,
|
|
158
|
+
markResendRequested,
|
|
159
|
+
listPendingInvites,
|
|
160
|
+
};
|