clavarium 0.2.84-next.1 → 0.2.86-next.1

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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.84-next.1",
3
+ "version": "0.2.86-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.84-next.1",
9
+ "version": "0.2.86-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
@@ -14,7 +14,7 @@
14
14
  "clavarium-binder": "0.1.8-next.1",
15
15
  "clavarium-docket": "0.1.1",
16
16
  "clavarium-parley": "0.17.11-next.1",
17
- "clavarium-tether": "0.1.0-next.14",
17
+ "clavarium-tether": "0.1.0-next.16",
18
18
  "mermaid": "11.17.2",
19
19
  "node-pty": "^1.1.0",
20
20
  "pdfjs-dist": "6.4.299",
@@ -755,9 +755,9 @@
755
755
  }
756
756
  },
757
757
  "node_modules/clavarium-tether": {
758
- "version": "0.1.0-next.14",
759
- "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.14.tgz",
760
- "integrity": "sha512-QNa5FmgxVi1b2jW0MCMY0hkkqAIl8QDF1qHluwW8j2QbA+h5kwJ1bRW5haT2GomHlX3xxcbah0uYpuSoSZd6dw==",
758
+ "version": "0.1.0-next.16",
759
+ "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.16.tgz",
760
+ "integrity": "sha512-OluUIv32Ot5oh+PbrcDBT/MnklJ8+kUjQQbtvbhQuEnBO1tnXWaBNIA2TLpEyWysu7hFe2jCehm19ljsZrvy7w==",
761
761
  "license": "MIT",
762
762
  "bin": {
763
763
  "tether": "bin/cli.mjs",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.84-next.1",
3
+ "version": "0.2.86-next.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
@@ -47,7 +47,7 @@
47
47
  "clavarium-binder": "0.1.8-next.1",
48
48
  "clavarium-docket": "0.1.1",
49
49
  "clavarium-parley": "0.17.11-next.1",
50
- "clavarium-tether": "0.1.0-next.14",
50
+ "clavarium-tether": "0.1.0-next.16",
51
51
  "mermaid": "11.17.2",
52
52
  "node-pty": "^1.1.0",
53
53
  "pdfjs-dist": "6.4.299",
@@ -22,7 +22,7 @@
22
22
  */
23
23
  import { Refusal } from './conversations.mjs';
24
24
  import { createHash } from 'node:crypto';
25
- import { consentUrl, exchangeCode, extractCode, thunderbird, scopeFor, pkcePair, newState, PROVIDERS } from './oauth.mjs';
25
+ import { consentUrl, exchangeCode, extractCode, thunderbird, scopeFor, kindsGranted, pkcePair, newState, PROVIDERS } from './oauth.mjs';
26
26
 
27
27
  /* The loopback the provider sends the code back to; nothing listens there. The
28
28
  person's browser lands on a page that will not load, and they copy the whole
@@ -103,17 +103,26 @@ export class Accounts {
103
103
  let code;
104
104
  try { code = extractCode(redirected, { state }); } catch (e) { throw new Refusal(400, e.message); }
105
105
  const { clientId, clientSecret } = thunderbird(p.provider);
106
- let refreshToken;
107
- try { ({ refreshToken } = await exchangeCode(p.provider, { clientId, clientSecret, redirectUri: redirectFor(p.provider), code, codeVerifier: p.verifier, fetch: this.#fetch })); }
106
+ let refreshToken, grantedScope;
107
+ try { ({ refreshToken, scope: grantedScope } = await exchangeCode(p.provider, { clientId, clientSecret, redirectUri: redirectFor(p.provider), code, codeVerifier: p.verifier, fetch: this.#fetch })); }
108
108
  catch (e) { throw new Refusal(502, `The connection could not be completed: ${e.message}`); }
109
109
  if (!refreshToken) throw new Refusal(502, 'The account returned no lasting token; allow offline access and try again.');
110
+ /* What was actually allowed. A provider may grant fewer kinds than asked
111
+ (the person unticks one); the token's scope says so, so we make a target
112
+ only for a kind that was really granted, and tell the caller which were
113
+ denied -- no guessing. If the scope is unreadable (matched nothing) we
114
+ trust what was asked, as before. */
115
+ const matched = kindsGranted(p.provider, grantedScope);
116
+ const useScope = matched.length > 0;
117
+ const authorized = useScope ? p.kinds.filter((k) => matched.includes(k)) : p.kinds;
118
+ const denied = useScope ? p.kinds.filter((k) => !matched.includes(k)) : [];
110
119
  /* The token is kept host-only: it can never be attached to a conversation,
111
120
  so it is never delivered to a machine -- the keeper reveals it by name. */
112
121
  const name = secretName(p.provider, p.user);
113
122
  this.#secrets.set({ name, value: refreshToken, hostOnly: true });
114
123
  const label = PROVIDERS[p.provider].label;
115
124
  const made = [];
116
- for (const k of p.kinds) {
125
+ for (const k of authorized) {
117
126
  const spec = KIND[k];
118
127
  /* Pin the client that minted the token onto the target, so the keeper
119
128
  refreshes with the same one -- a refresh token is bound to its client,
@@ -128,7 +137,7 @@ export class Accounts {
128
137
  });
129
138
  made.push({ id: target?.id ?? targetId(k, p.provider, p.user), title: target?.title, kind: spec.kind });
130
139
  }
131
- return { provider: p.provider, user: p.user, targets: made };
140
+ return { provider: p.provider, user: p.user, targets: made, authorized, denied };
132
141
  }
133
142
 
134
143
  #sweep() { const now = Date.now(); for (const [k, v] of this.#pending) if (now - v.at > PENDING_MS) this.#pending.delete(k); }
package/server/oauth.mjs CHANGED
@@ -138,6 +138,22 @@ export function scopeFor(name, kinds = ['mail']) {
138
138
  return [...new Set(parts)].join(' ');
139
139
  }
140
140
 
141
+ /* Which kinds a granted scope string actually covers. A provider may grant fewer
142
+ scopes than asked (the person unticks one on the consent screen); the token's
143
+ `scope` field says what was allowed, so we never have to guess. A kind counts as
144
+ granted when the first (primary) scope token of its request appears in the
145
+ granted string, matched case-insensitively -- providers echo scopes back in
146
+ their own casing and URL form (Microsoft returns
147
+ `https://graph.microsoft.com/Calendars.ReadWrite` for a requested
148
+ `calendars.readwrite`). Returns [] when the scope is unknown, so a caller can
149
+ fall back to trusting what it asked for. */
150
+ export function kindsGranted(name, granted = '') {
151
+ const p = provider(name);
152
+ const g = String(granted).toLowerCase();
153
+ if (!g) return [];
154
+ return Object.keys(p.scopes).filter((k) => g.includes(p.scopes[k].split(/\s+/)[0].toLowerCase()));
155
+ }
156
+
141
157
  /* The URL the person's browser opens to sign in and allow. `redirectUri` is the
142
158
  loopback the provider sends the code back to (http://127.0.0.1:PORT). */
143
159
  export function consentUrl(name, { clientId, redirectUri, scope, state, codeChallenge, loginHint } = {}) {
package/web/app.js CHANGED
@@ -6217,9 +6217,21 @@
6217
6217
  const btn = $('connexion-connect'); const was = btn.textContent; btn.disabled = true; btn.textContent = 'Connecting...';
6218
6218
  try {
6219
6219
  const made = await post(`${API}/accounts/connect`, { redirected: $('connexion-code').value.trim() });
6220
- if (made?.targets?.[0]) assetOpen = `connexion:${made.targets[0].id}`;
6221
- assetsAdding = '';
6222
- $('connexion-new').reset(); setConnexionProvider('google');
6220
+ const denied = Array.isArray(made?.denied) ? made.denied : [];
6221
+ if (denied.length) {
6222
+ /* A partial grant: say plainly what was allowed and what was not,
6223
+ from the token itself, and keep the form so they can allow the rest. */
6224
+ const word = (k) => ({ mail: 'mail', calendar: 'calendar', contacts: 'contacts' }[k] || k);
6225
+ const ok = (made.authorized || []).map(word);
6226
+ const no = denied.map(word);
6227
+ $('connexion-error').textContent = `${ok.length ? `Connected for ${ok.join(', ')}. ` : ''}Not allowed: ${no.join(', ')} — connect again and allow ${no.length > 1 ? 'them' : 'it'} on the provider's screen.`;
6228
+ $('connexion-error').hidden = false;
6229
+ $('connexion-code').value = ''; $('connexion-paste').hidden = true;
6230
+ } else {
6231
+ if (made?.targets?.[0]) assetOpen = `connexion:${made.targets[0].id}`;
6232
+ assetsAdding = '';
6233
+ $('connexion-new').reset(); setConnexionProvider('google');
6234
+ }
6223
6235
  } catch (error) { $('connexion-error').textContent = error.message; $('connexion-error').hidden = false; }
6224
6236
  btn.disabled = false; btn.textContent = was;
6225
6237
  await paintAssets();