clavarium 0.2.62-beta.1 → 0.2.63-beta.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.
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/server/accounts.mjs +14 -5
- package/server/oauth.mjs +16 -0
- package/web/app.js +15 -3
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.63-beta.1",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "clavarium",
|
|
9
|
-
"version": "0.2.
|
|
9
|
+
"version": "0.2.63-beta.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/accounts.mjs
CHANGED
|
@@ -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
|
|
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
|
-
|
|
6221
|
-
|
|
6222
|
-
|
|
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();
|