@celljs/ta-relay 4.17.0 → 4.19.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/esm/common/index.d.ts +4 -0
- package/esm/common/index.js +4 -0
- package/esm/common/relay-account-client.d.ts +138 -0
- package/esm/common/relay-account-client.js +338 -0
- package/esm/common/relay-account.d.ts +137 -0
- package/esm/common/relay-account.js +122 -0
- package/esm/common/relay-accounts.d.ts +107 -0
- package/esm/common/relay-accounts.js +393 -0
- package/esm/common/relay-faces.d.ts +1 -1
- package/esm/common/relay-faces.js +56 -1
- package/esm/common/relay-files.d.ts +14 -3
- package/esm/common/relay-files.js +41 -16
- package/esm/common/relay-friend-sends.d.ts +64 -0
- package/esm/common/relay-friend-sends.js +126 -0
- package/esm/common/relay-group-invites.d.ts +76 -0
- package/esm/common/relay-group-invites.js +163 -0
- package/esm/common/relay-hubs-protocol.d.ts +3 -1
- package/esm/common/relay-hubs.d.ts +19 -0
- package/esm/common/relay-hubs.js +58 -0
- package/esm/common/relay-open-in-ta.d.ts +11 -0
- package/esm/common/relay-open-in-ta.js +34 -0
- package/esm/common/relay-protocol.d.ts +2 -2
- package/esm/common/relay-protocol.js +5 -2
- package/esm/guest/guest-i18n.d.ts +1 -0
- package/esm/guest/guest-i18n.js +45 -41
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +2 -0
- package/esm/guest/guest.js +7 -2
- package/esm/join/join-i18n.d.ts +1 -0
- package/esm/join/join-i18n.js +4 -0
- package/esm/join/join.js +4 -1
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/relay-files-host.d.ts +2 -0
- package/esm/node/relay-files-host.js +24 -5
- package/esm/node/relay-server-accounts.d.ts +16 -0
- package/esm/node/relay-server-accounts.js +54 -0
- package/esm/node/relay-server.d.ts +5 -0
- package/esm/node/relay-server.js +7 -0
- package/esm/node/relay-store.d.ts +3 -1
- package/esm/node/relay-store.js +1 -0
- package/esm/viewer/viewer-i18n.d.ts +1 -0
- package/esm/viewer/viewer-i18n.js +4 -0
- package/esm/viewer/viewer-style.d.ts +1 -1
- package/esm/viewer/viewer-style.js +2 -0
- package/esm/viewer/viewer.js +3 -1
- package/lib/common/index.d.ts +4 -0
- package/lib/common/index.js +4 -0
- package/lib/common/relay-account-client.d.ts +139 -0
- package/lib/common/relay-account-client.js +343 -0
- package/lib/common/relay-account.d.ts +138 -0
- package/lib/common/relay-account.js +125 -0
- package/lib/common/relay-accounts.d.ts +108 -0
- package/lib/common/relay-accounts.js +401 -0
- package/lib/common/relay-faces.d.ts +1 -1
- package/lib/common/relay-faces.js +56 -1
- package/lib/common/relay-files.d.ts +14 -3
- package/lib/common/relay-files.js +42 -17
- package/lib/common/relay-friend-sends.d.ts +65 -0
- package/lib/common/relay-friend-sends.js +130 -0
- package/lib/common/relay-group-invites.d.ts +77 -0
- package/lib/common/relay-group-invites.js +167 -0
- package/lib/common/relay-hubs-protocol.d.ts +3 -1
- package/lib/common/relay-hubs.d.ts +19 -0
- package/lib/common/relay-hubs.js +58 -0
- package/lib/common/relay-open-in-ta.d.ts +12 -0
- package/lib/common/relay-open-in-ta.js +37 -0
- package/lib/common/relay-protocol.d.ts +2 -2
- package/lib/common/relay-protocol.js +5 -2
- package/lib/guest/guest-i18n.d.ts +1 -0
- package/lib/guest/guest-i18n.js +45 -41
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +2 -0
- package/lib/guest/guest.js +7 -2
- package/lib/join/join-i18n.d.ts +1 -0
- package/lib/join/join-i18n.js +4 -0
- package/lib/join/join.js +4 -1
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/relay-files-host.d.ts +2 -0
- package/lib/node/relay-files-host.js +23 -4
- package/lib/node/relay-server-accounts.d.ts +17 -0
- package/lib/node/relay-server-accounts.js +58 -0
- package/lib/node/relay-server.d.ts +5 -0
- package/lib/node/relay-server.js +7 -0
- package/lib/node/relay-store.d.ts +3 -1
- package/lib/node/relay-store.js +1 -0
- package/lib/viewer/viewer-i18n.d.ts +1 -0
- package/lib/viewer/viewer-i18n.js +4 -0
- package/lib/viewer/viewer-style.d.ts +1 -1
- package/lib/viewer/viewer-style.js +2 -0
- package/lib/viewer/viewer.js +3 -1
- package/package.json +3 -3
- package/viewer/join.html +12 -3
- package/viewer/page.html +18 -9
- package/viewer/room.html +19 -8
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccountAgent = exports.AccountService = exports.AccountCallError = void 0;
|
|
4
|
+
const relay_account_1 = require("./relay-account");
|
|
5
|
+
const relay_identity_1 = require("./relay-identity");
|
|
6
|
+
/** The service said no: `status` 401 is a token no longer good, 403 this device removed. */
|
|
7
|
+
class AccountCallError extends Error {
|
|
8
|
+
constructor(status, message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.status = status;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.AccountCallError = AccountCallError;
|
|
14
|
+
/** The account service of a relay or a gateway, over fetch: the same in ta serve and a page. */
|
|
15
|
+
var AccountService;
|
|
16
|
+
(function (AccountService) {
|
|
17
|
+
/** How long the service is waited for: one out of reach (a company's network, away from it) answers nothing at all. */
|
|
18
|
+
AccountService.TIMEOUT_MS = 8000;
|
|
19
|
+
const wait = () => AbortSignal.timeout(AccountService.TIMEOUT_MS);
|
|
20
|
+
async function answer(response) {
|
|
21
|
+
var _a;
|
|
22
|
+
const body = (await response.json().catch(() => ({})));
|
|
23
|
+
if (!response.ok) {
|
|
24
|
+
throw new AccountCallError(response.status, (_a = body.error) !== null && _a !== void 0 ? _a : `The account service answered ${response.status}.`);
|
|
25
|
+
}
|
|
26
|
+
return body;
|
|
27
|
+
}
|
|
28
|
+
function base(service) {
|
|
29
|
+
return service.replace(/\/+$/, '') + relay_account_1.RELAY_ACCOUNT_PATH;
|
|
30
|
+
}
|
|
31
|
+
AccountService.base = base;
|
|
32
|
+
async function providers(service) {
|
|
33
|
+
return answer(await fetch(`${base(service)}providers`, { signal: wait() }));
|
|
34
|
+
}
|
|
35
|
+
AccountService.providers = providers;
|
|
36
|
+
async function start(service, provider) {
|
|
37
|
+
return answer(await fetch(`${base(service)}sign-in`, {
|
|
38
|
+
method: 'POST',
|
|
39
|
+
headers: { 'content-type': 'application/json' },
|
|
40
|
+
body: JSON.stringify({ provider }),
|
|
41
|
+
signal: wait(),
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
AccountService.start = start;
|
|
45
|
+
async function poll(service, provider, handle) {
|
|
46
|
+
return answer(await fetch(`${base(service)}sign-in/poll`, {
|
|
47
|
+
method: 'POST',
|
|
48
|
+
headers: { 'content-type': 'application/json' },
|
|
49
|
+
body: JSON.stringify({ provider, handle }),
|
|
50
|
+
signal: wait(),
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
AccountService.poll = poll;
|
|
54
|
+
/**
|
|
55
|
+
* The ways to sign in to an account: those of the relay (GitHub), and those of the gateways of deployments (a
|
|
56
|
+
* company's accounts), whose own sign-in gives the token; a gateway without the account service is left out.
|
|
57
|
+
*/
|
|
58
|
+
async function ways(relay, gateways = []) {
|
|
59
|
+
const of = async (gateway) => {
|
|
60
|
+
const base = gateway.replace(/\/+$/, '');
|
|
61
|
+
const [accounts, signIn] = await Promise.all([
|
|
62
|
+
fetch(`${AccountService.base(base)}providers`, { signal: wait() }),
|
|
63
|
+
fetch(`${base}/sign-in`, { signal: wait() }),
|
|
64
|
+
]);
|
|
65
|
+
if (!accounts.ok || !signIn.ok) {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
const { providers } = (await signIn.json());
|
|
69
|
+
return (providers !== null && providers !== void 0 ? providers : []).map(({ id, name }) => ({ id, name, service: base, gateway: true }));
|
|
70
|
+
};
|
|
71
|
+
const found = await Promise.all([
|
|
72
|
+
providers(relay).then(list => list.map(({ id, name }) => ({ id, name, service: relay.replace(/\/+$/, '') }))),
|
|
73
|
+
...[...new Set(gateways.filter(Boolean))].map(gateway => of(gateway)),
|
|
74
|
+
].map(one => one.catch(() => [])));
|
|
75
|
+
return found.flat();
|
|
76
|
+
}
|
|
77
|
+
AccountService.ways = ways;
|
|
78
|
+
/** Begins signing in by `way`: the code the user enters on the page of `url`. */
|
|
79
|
+
async function begin(way, deviceName) {
|
|
80
|
+
if (!way.gateway) {
|
|
81
|
+
return start(way.service, way.id);
|
|
82
|
+
}
|
|
83
|
+
const given = await answer(await fetch(`${way.service}/sign-in/device`, { method: 'POST', body: JSON.stringify({ name: deviceName }), signal: wait() }));
|
|
84
|
+
return {
|
|
85
|
+
handle: given.device,
|
|
86
|
+
code: given.code,
|
|
87
|
+
url: `${way.service}/sign-in/${encodeURIComponent(way.id)}?device=${encodeURIComponent(given.code)}`,
|
|
88
|
+
interval: 2,
|
|
89
|
+
expiresIn: Math.max(60, Math.round((given.expiresAt - Date.now()) / 1000)),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
AccountService.begin = begin;
|
|
93
|
+
/** One look at a sign-in begun by `way`. */
|
|
94
|
+
async function check(way, handle) {
|
|
95
|
+
if (!way.gateway) {
|
|
96
|
+
return poll(way.service, way.id, handle);
|
|
97
|
+
}
|
|
98
|
+
const state = await answer(await fetch(`${way.service}/sign-in/device/poll`, { method: 'POST', body: JSON.stringify({ device: handle }), signal: wait() }));
|
|
99
|
+
if (state.status === 'done' && state.token && state.account) {
|
|
100
|
+
return { status: 'done', token: state.token, account: { id: state.account, name: state.account } };
|
|
101
|
+
}
|
|
102
|
+
return state.status === 'expired' ? { status: 'failed', error: 'The sign-in ran out of time: begin it again.' } : { status: 'pending' };
|
|
103
|
+
}
|
|
104
|
+
AccountService.check = check;
|
|
105
|
+
/** The calls of an account, with a token of the service (or of the gateway it is part of). */
|
|
106
|
+
function calls(service, token) {
|
|
107
|
+
const call = async (op, args) => (await answer(await fetch(`${base(service)}call`, {
|
|
108
|
+
method: 'POST',
|
|
109
|
+
headers: { 'content-type': 'application/json', authorization: `Bearer ${token}` },
|
|
110
|
+
body: JSON.stringify({ op, args }),
|
|
111
|
+
signal: wait(),
|
|
112
|
+
}))).value;
|
|
113
|
+
return Object.fromEntries(relay_account_1.ACCOUNT_CALLS.map(op => [op, (...args) => call(op, args)]));
|
|
114
|
+
}
|
|
115
|
+
AccountService.calls = calls;
|
|
116
|
+
})(AccountService || (exports.AccountService = AccountService = {}));
|
|
117
|
+
/**
|
|
118
|
+
* A device of an account (docs/design/ta-account.md 4): says it is there, keeps whom it trusts, lets others in, makes and
|
|
119
|
+
* takes ways into computers through sealed mail. The same in ta serve and a page; what is particular comes in hooks.
|
|
120
|
+
*/
|
|
121
|
+
class AccountAgent {
|
|
122
|
+
constructor(kept, hooks, calls) {
|
|
123
|
+
this.kept = kept;
|
|
124
|
+
this.hooks = hooks;
|
|
125
|
+
/** What was told last, by `<from>|<topic>`: a mail is kept until replaced, told once each time it changes. */
|
|
126
|
+
this.heard = new Map();
|
|
127
|
+
this.calls = calls !== null && calls !== void 0 ? calls : AccountService.calls(kept.service, kept.token);
|
|
128
|
+
}
|
|
129
|
+
/** A new device of `account`: its own keys, trusting itself alone. */
|
|
130
|
+
static async create(service, token, account, kind, name) {
|
|
131
|
+
const self = await relay_identity_1.RelayIdentityUtil.create(name);
|
|
132
|
+
return { service: service.replace(/\/+$/, ''), token, account, kind, self, circle: relay_account_1.AccountCircle.alone(self), pairings: {} };
|
|
133
|
+
}
|
|
134
|
+
get state() {
|
|
135
|
+
return this.kept;
|
|
136
|
+
}
|
|
137
|
+
async save(next) {
|
|
138
|
+
this.kept = next;
|
|
139
|
+
await this.hooks.save(next);
|
|
140
|
+
}
|
|
141
|
+
async guard(run) {
|
|
142
|
+
var _a, _b;
|
|
143
|
+
try {
|
|
144
|
+
return await run();
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error instanceof AccountCallError && (error.status === 401 || error.status === 403)) {
|
|
148
|
+
await ((_b = (_a = this.hooks).signedOut) === null || _b === void 0 ? void 0 : _b.call(_a, error.status === 401 ? 'token' : 'removed'));
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** One round: be in the roster, trust whom to, let the trusted in to this computer, take ways into others. */
|
|
154
|
+
async sync() {
|
|
155
|
+
return this.guard(async () => {
|
|
156
|
+
var _a, _b, _c, _d;
|
|
157
|
+
const { self } = this.kept;
|
|
158
|
+
const name = this.hooks.name();
|
|
159
|
+
const host = (_b = (_a = this.hooks).host) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
160
|
+
let roster = await this.calls.roster(self.id);
|
|
161
|
+
const mine = roster.devices.find(one => one.id === self.id);
|
|
162
|
+
if (mine === null || mine === void 0 ? void 0 : mine.removed) {
|
|
163
|
+
throw new AccountCallError(403, 'This device was removed from the account.');
|
|
164
|
+
}
|
|
165
|
+
if (!mine || mine.name !== name || ((_c = mine.host) === null || _c === void 0 ? void 0 : _c.relay) !== (host === null || host === void 0 ? void 0 : host.relay) || ((_d = mine.host) === null || _d === void 0 ? void 0 : _d.id) !== (host === null || host === void 0 ? void 0 : host.id)) {
|
|
166
|
+
roster = await this.calls.join({ id: self.id, kind: this.kept.kind, name, sign: self.sign, box: self.box, ...(host ? { host } : {}) });
|
|
167
|
+
}
|
|
168
|
+
this.roster = roster;
|
|
169
|
+
await this.save({ ...this.kept, account: roster.account });
|
|
170
|
+
const me = roster.devices.find(one => one.id === self.id);
|
|
171
|
+
if ((me === null || me === void 0 ? void 0 : me.endorsement) && Object.keys(this.kept.circle.trusted).length === 1) {
|
|
172
|
+
await this.joinCircle(roster, me);
|
|
173
|
+
}
|
|
174
|
+
const circle = await relay_account_1.AccountCircle.update(this.kept.circle, roster);
|
|
175
|
+
if (JSON.stringify(circle) !== JSON.stringify(this.kept.circle)) {
|
|
176
|
+
await this.save({ ...this.kept, circle });
|
|
177
|
+
}
|
|
178
|
+
if (me && relay_account_1.AccountCircle.admitted(me)) {
|
|
179
|
+
await this.ways(roster);
|
|
180
|
+
}
|
|
181
|
+
return this.view();
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/** Let in by `me.endorsement.by`: whom it trusts came sealed, signed by it; the device it let in trusts them too. */
|
|
185
|
+
async joinCircle(roster, me) {
|
|
186
|
+
const by = roster.devices.find(one => { var _a; return one.id === ((_a = me.endorsement) === null || _a === void 0 ? void 0 : _a.by); });
|
|
187
|
+
if (!by || !me.endorsement || !(await relay_identity_1.RelayIdentityUtil.verify(by.sign, relay_account_1.AccountCircle.text(roster.account.id, me), me.endorsement.signature))) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const mail = (await this.calls.mail(this.kept.self.id)).find(one => one.from === by.id && one.kind === 'circle');
|
|
191
|
+
const sent = mail ? await relay_account_1.AccountCircle.open(this.kept.self, by, mail) : undefined;
|
|
192
|
+
if (!(sent === null || sent === void 0 ? void 0 : sent.trusted)) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const trusted = { ...sent.trusted, [by.id]: { sign: by.sign, box: by.box }, ...this.kept.circle.trusted };
|
|
196
|
+
await this.save({ ...this.kept, circle: { trusted, removed: this.kept.circle.removed } });
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* The trusted devices in the roster, with the keys this device trusts them by: the service may list other keys under
|
|
200
|
+
* a trusted id, and mail is sealed to and checked against what was trusted, never what the service says now.
|
|
201
|
+
*/
|
|
202
|
+
trusted(roster) {
|
|
203
|
+
const { circle, self } = this.kept;
|
|
204
|
+
return roster.devices
|
|
205
|
+
.filter(one => one.id !== self.id && relay_account_1.AccountCircle.live(one) && circle.trusted[one.id])
|
|
206
|
+
.map(one => ({ ...one, sign: circle.trusted[one.id].sign, box: circle.trusted[one.id].box }));
|
|
207
|
+
}
|
|
208
|
+
/** Ways in: made for the trusted (a computer), dropped for the rest; taken from trusted computers. */
|
|
209
|
+
async ways(roster) {
|
|
210
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
211
|
+
const trusted = this.trusted(roster);
|
|
212
|
+
const ids = new Set(trusted.map(one => one.id));
|
|
213
|
+
if (this.hooks.pair && this.hooks.paired) {
|
|
214
|
+
const paired = new Set(this.hooks.paired());
|
|
215
|
+
for (const id of paired) {
|
|
216
|
+
if (!ids.has(id)) {
|
|
217
|
+
await ((_b = (_a = this.hooks).unpair) === null || _b === void 0 ? void 0 : _b.call(_a, id));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
for (const device of trusted) {
|
|
221
|
+
if (!paired.has(device.id)) {
|
|
222
|
+
const link = await this.hooks.pair(device);
|
|
223
|
+
await this.calls.send(await relay_account_1.AccountCircle.seal(this.kept.self, device, 'pairing', { link }));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const mail = await this.calls.mail(this.kept.self.id);
|
|
228
|
+
const pairings = {};
|
|
229
|
+
for (const one of mail) {
|
|
230
|
+
const from = trusted.find(device => device.id === one.from);
|
|
231
|
+
if (!from || (one.kind !== 'pairing' && one.kind !== 'package')) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (one.kind === 'package') {
|
|
235
|
+
const key = `${one.from}|${one.topic}`;
|
|
236
|
+
if (this.heard.get(key) !== one.sealed) {
|
|
237
|
+
const opened = await relay_account_1.AccountCircle.open(this.kept.self, from, one);
|
|
238
|
+
if (opened && one.topic) {
|
|
239
|
+
this.heard.set(key, one.sealed);
|
|
240
|
+
await ((_d = (_c = this.hooks).told) === null || _d === void 0 ? void 0 : _d.call(_c, from, one.topic, opened.value));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const opened = await relay_account_1.AccountCircle.open(this.kept.self, from, one);
|
|
246
|
+
if (typeof (opened === null || opened === void 0 ? void 0 : opened.link) !== 'string') {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
pairings[from.id] = opened.link;
|
|
250
|
+
if (this.kept.pairings[from.id] !== opened.link) {
|
|
251
|
+
await ((_f = (_e = this.hooks).pairing) === null || _f === void 0 ? void 0 : _f.call(_e, from, opened.link));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (const id of Object.keys(this.kept.pairings)) {
|
|
255
|
+
if (!pairings[id]) {
|
|
256
|
+
await ((_h = (_g = this.hooks).unpaired) === null || _h === void 0 ? void 0 : _h.call(_g, id));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (JSON.stringify(pairings) !== JSON.stringify(this.kept.pairings)) {
|
|
260
|
+
await this.save({ ...this.kept, pairings });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async view(now = Date.now()) {
|
|
264
|
+
var _a;
|
|
265
|
+
const roster = (_a = this.roster) !== null && _a !== void 0 ? _a : (await this.guard(() => this.calls.roster(this.kept.self.id)));
|
|
266
|
+
const { self, circle } = this.kept;
|
|
267
|
+
const me = roster.devices.find(one => one.id === self.id);
|
|
268
|
+
const devices = [];
|
|
269
|
+
for (const device of roster.devices) {
|
|
270
|
+
if (!relay_account_1.AccountCircle.live(device)) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const waiting = relay_account_1.AccountCircle.waiting(device);
|
|
274
|
+
const trusted = !!circle.trusted[device.id];
|
|
275
|
+
if (!trusted && !waiting) {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
devices.push({
|
|
279
|
+
id: device.id,
|
|
280
|
+
kind: device.kind,
|
|
281
|
+
name: device.name,
|
|
282
|
+
self: device.id === self.id,
|
|
283
|
+
trusted,
|
|
284
|
+
waiting,
|
|
285
|
+
...(waiting ? { code: await relay_account_1.AccountCircle.code(device) } : {}),
|
|
286
|
+
online: device.id === self.id || relay_account_1.AccountCircle.online(device, now),
|
|
287
|
+
seenAt: device.seenAt,
|
|
288
|
+
addedAt: device.addedAt,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
account: roster.account,
|
|
293
|
+
service: this.kept.service,
|
|
294
|
+
self: self.id,
|
|
295
|
+
waiting: !me || relay_account_1.AccountCircle.waiting(me) || (!!me.endorsement && Object.keys(circle.trusted).length === 1),
|
|
296
|
+
code: await relay_account_1.AccountCircle.code(self),
|
|
297
|
+
devices,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
/** The devices of the account this one trusts, itself aside, as the service last listed them. */
|
|
301
|
+
devices() {
|
|
302
|
+
return this.roster ? this.trusted(this.roster) : [];
|
|
303
|
+
}
|
|
304
|
+
/** Says `value` to a trusted device of the account, sealed for it: a package on it hears it by `topic`. */
|
|
305
|
+
async tell(to, topic, value) {
|
|
306
|
+
const device = this.devices().find(one => one.id === to);
|
|
307
|
+
if (!device) {
|
|
308
|
+
throw new AccountCallError(404, 'No such device trusted in the account.');
|
|
309
|
+
}
|
|
310
|
+
await this.guard(async () => this.calls.send(await relay_account_1.AccountCircle.seal(this.kept.self, device, 'package', { value }, topic)));
|
|
311
|
+
}
|
|
312
|
+
/** Lets a waiting device in: signs it, and sends it whom this device trusts. */
|
|
313
|
+
async approve(id) {
|
|
314
|
+
return this.guard(async () => {
|
|
315
|
+
const roster = await this.calls.roster(this.kept.self.id);
|
|
316
|
+
const device = roster.devices.find(one => one.id === id);
|
|
317
|
+
if (!device || !relay_account_1.AccountCircle.waiting(device)) {
|
|
318
|
+
throw new AccountCallError(409, 'This device is not waiting to be let in.');
|
|
319
|
+
}
|
|
320
|
+
const signature = await relay_account_1.AccountCircle.endorse(this.kept.self, roster.account.id, device);
|
|
321
|
+
await this.calls.endorse(id, this.kept.self.id, signature);
|
|
322
|
+
await this.calls.send(await relay_account_1.AccountCircle.seal(this.kept.self, device, 'circle', { trusted: this.kept.circle.trusted }));
|
|
323
|
+
return this.sync();
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
/** Removes a device (one waiting that is not the user's, or one lost): every device drops it. */
|
|
327
|
+
async remove(id) {
|
|
328
|
+
return this.guard(async () => {
|
|
329
|
+
await this.calls.remove(id, this.kept.self.id, await relay_account_1.AccountCircle.remove(this.kept.self, this.kept.account.id, id));
|
|
330
|
+
return this.sync();
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/** Leaves the account: removed from it, its token dropped. */
|
|
334
|
+
async signOut() {
|
|
335
|
+
const { self, account } = this.kept;
|
|
336
|
+
await relay_account_1.AccountCircle.remove(self, account.id, self.id)
|
|
337
|
+
.then(signature => this.calls.remove(self.id, self.id, signature))
|
|
338
|
+
.catch(() => undefined);
|
|
339
|
+
await this.calls.signOut().catch(() => undefined);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
exports.AccountAgent = AccountAgent;
|
|
343
|
+
//# sourceMappingURL=relay-account-client.js.map
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type RelayIdentity } from './relay-identity';
|
|
2
|
+
/** The account service of a relay or a gateway (docs/design/ta-account.md): the devices of one user, and their mail. */
|
|
3
|
+
export declare const RELAY_ACCOUNT_PATH = "/v1/account/";
|
|
4
|
+
/** Devices of an account at most, removed ones aside: `RELAY_ACCOUNT_MAX_DEVICES` of the service changes it. */
|
|
5
|
+
export declare const RELAY_ACCOUNT_MAX_DEVICES = 20;
|
|
6
|
+
/** A device seen within this is online: computers say they are there every minute. */
|
|
7
|
+
export declare const RELAY_ACCOUNT_ONLINE_MS: number;
|
|
8
|
+
export type AccountDeviceKind = 'computer' | 'phone' | 'browser';
|
|
9
|
+
export interface AccountName {
|
|
10
|
+
/** `github:<id>`, `iam:<user>`: the provider and who the user is there. */
|
|
11
|
+
id: string;
|
|
12
|
+
/** What the user is called there, to show. */
|
|
13
|
+
name: string;
|
|
14
|
+
}
|
|
15
|
+
/** One device of the user, as the service keeps it: public keys only. */
|
|
16
|
+
export interface AccountDevice {
|
|
17
|
+
/** RelayIdentityUtil.id of its keys: its own keys, not the identity others know the user by. */
|
|
18
|
+
id: string;
|
|
19
|
+
kind: AccountDeviceKind;
|
|
20
|
+
name: string;
|
|
21
|
+
/** ECDSA P-256, raw base64url. */
|
|
22
|
+
sign: string;
|
|
23
|
+
/** ECDH P-256, raw base64url. */
|
|
24
|
+
box: string;
|
|
25
|
+
/** A computer: the relay it hangs on and its host id there. */
|
|
26
|
+
host?: {
|
|
27
|
+
relay: string;
|
|
28
|
+
id: string;
|
|
29
|
+
};
|
|
30
|
+
/** The first device of the account: in without anyone letting it in. */
|
|
31
|
+
root?: true;
|
|
32
|
+
/** Let in by a device of the account: its id and signature of AccountCircle.text. */
|
|
33
|
+
endorsement?: {
|
|
34
|
+
by: string;
|
|
35
|
+
signature: string;
|
|
36
|
+
};
|
|
37
|
+
/** Removed: by which device, with its signature of AccountCircle.removal, when. Kept, so that what it signed can still be told apart. */
|
|
38
|
+
removed?: {
|
|
39
|
+
by?: string;
|
|
40
|
+
signature?: string;
|
|
41
|
+
at: number;
|
|
42
|
+
};
|
|
43
|
+
addedAt: number;
|
|
44
|
+
seenAt: number;
|
|
45
|
+
}
|
|
46
|
+
export interface AccountRoster {
|
|
47
|
+
account: AccountName;
|
|
48
|
+
devices: AccountDevice[];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* What a device is sent by another, sealed for it alone and signed by the sender: whom it trusts (`circle`), a way into a
|
|
52
|
+
* computer (`pairing`), or what a package of Ta says to the same package on another computer (`package`, by `topic`).
|
|
53
|
+
*/
|
|
54
|
+
export type AccountMailKind = 'circle' | 'pairing' | 'package';
|
|
55
|
+
export interface AccountMail {
|
|
56
|
+
from: string;
|
|
57
|
+
to: string;
|
|
58
|
+
kind: AccountMailKind;
|
|
59
|
+
/** Of a `package` mail: the package and what about, one mail kept of each. */
|
|
60
|
+
topic?: string;
|
|
61
|
+
sealed: string;
|
|
62
|
+
signature: string;
|
|
63
|
+
at: number;
|
|
64
|
+
}
|
|
65
|
+
/** What a device asks of its account, with its token (RelayAccounts). */
|
|
66
|
+
export interface AccountCalls {
|
|
67
|
+
roster(device?: string): Promise<AccountRoster>;
|
|
68
|
+
/** Adds the device, or says it is there again (its name, its host). */
|
|
69
|
+
join(device: Pick<AccountDevice, 'id' | 'kind' | 'name' | 'sign' | 'box' | 'host'>): Promise<AccountRoster>;
|
|
70
|
+
endorse(id: string, by: string, signature: string): Promise<AccountRoster>;
|
|
71
|
+
/** Removes `id`, said by `by` (a device let in, or `id` itself) with its signature of AccountCircle.removal. */
|
|
72
|
+
remove(id: string, by: string, signature: string): Promise<AccountRoster>;
|
|
73
|
+
send(mail: Omit<AccountMail, 'at'>): Promise<void>;
|
|
74
|
+
mail(to: string): Promise<AccountMail[]>;
|
|
75
|
+
signOut(): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
export type AccountCallName = keyof AccountCalls;
|
|
78
|
+
export declare const ACCOUNT_CALLS: readonly AccountCallName[];
|
|
79
|
+
/** A sign-in begun: open `url`, enter `code`; `handle` is what the device polls with. */
|
|
80
|
+
export interface AccountSignInStart {
|
|
81
|
+
handle: string;
|
|
82
|
+
code: string;
|
|
83
|
+
url: string;
|
|
84
|
+
interval: number;
|
|
85
|
+
expiresIn: number;
|
|
86
|
+
}
|
|
87
|
+
export type AccountSignInPoll = {
|
|
88
|
+
status: 'pending';
|
|
89
|
+
} | {
|
|
90
|
+
status: 'done';
|
|
91
|
+
token: string;
|
|
92
|
+
account: AccountName;
|
|
93
|
+
} | {
|
|
94
|
+
status: 'failed';
|
|
95
|
+
error: string;
|
|
96
|
+
};
|
|
97
|
+
/** What a device keeps of the devices it trusts (docs/design/ta-account.md 5). */
|
|
98
|
+
export interface AccountCircleState {
|
|
99
|
+
trusted: Record<string, {
|
|
100
|
+
sign: string;
|
|
101
|
+
box: string;
|
|
102
|
+
}>;
|
|
103
|
+
/** Removed once: never trusted again, whatever the service says later. */
|
|
104
|
+
removed: string[];
|
|
105
|
+
}
|
|
106
|
+
/** The trust of the devices of an account, kept by each device: the service holds keys, never says who to trust. */
|
|
107
|
+
export declare namespace AccountCircle {
|
|
108
|
+
/** What a device signs to let another in. */
|
|
109
|
+
function text(account: string, device: Pick<AccountDevice, 'id' | 'kind' | 'sign' | 'box'>): string;
|
|
110
|
+
/** Six digits both devices show, the user checks they are the same: they come from the keys of the new one. */
|
|
111
|
+
function code(device: Pick<AccountDevice, 'sign' | 'box'>): Promise<string>;
|
|
112
|
+
function live(device: AccountDevice): boolean;
|
|
113
|
+
/** In, by the service: the first one, or let in by someone. Who trusts it is each device's own. */
|
|
114
|
+
function admitted(device: AccountDevice): boolean;
|
|
115
|
+
/** Waiting for a device of the account to let it in. */
|
|
116
|
+
function waiting(device: AccountDevice): boolean;
|
|
117
|
+
/** A device trusting itself alone: the first one, or one not let in yet. */
|
|
118
|
+
function alone(self: Pick<AccountDevice, 'id' | 'sign' | 'box'>): AccountCircleState;
|
|
119
|
+
/**
|
|
120
|
+
* Who to trust now: those trusted before and not removed, and those a trusted device let in with a good signature,
|
|
121
|
+
* until no more. A device the service adds without a signature of one of them is never trusted.
|
|
122
|
+
*/
|
|
123
|
+
function update(state: AccountCircleState, roster: AccountRoster): Promise<AccountCircleState>;
|
|
124
|
+
function endorse(self: RelayIdentity, account: string, device: AccountDevice): Promise<string>;
|
|
125
|
+
/** What a device signs to remove one of the account, itself or another. */
|
|
126
|
+
function removal(account: string, id: string): string;
|
|
127
|
+
function remove(self: RelayIdentity, account: string, id: string): Promise<string>;
|
|
128
|
+
/** `value` sealed for `to` and signed by `self`: the service carries it, never reads it. */
|
|
129
|
+
function seal(self: RelayIdentity, to: Pick<AccountDevice, 'id' | 'box'>, kind: AccountMailKind, value: unknown, topic?: string): Promise<Omit<AccountMail, 'at'>>;
|
|
130
|
+
/** What `mail` holds, when it is for `self` and signed by `sender`; undefined otherwise. */
|
|
131
|
+
function open<T>(self: RelayIdentity, sender: {
|
|
132
|
+
sign: string;
|
|
133
|
+
box: string;
|
|
134
|
+
}, mail: AccountMail): Promise<T | undefined>;
|
|
135
|
+
/** Whether a device is online, from when it was last there. */
|
|
136
|
+
function online(device: AccountDevice, now?: number): boolean;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=relay-account.d.ts.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccountCircle = exports.ACCOUNT_CALLS = exports.RELAY_ACCOUNT_ONLINE_MS = exports.RELAY_ACCOUNT_MAX_DEVICES = exports.RELAY_ACCOUNT_PATH = void 0;
|
|
4
|
+
const relay_bytes_1 = require("./relay-bytes");
|
|
5
|
+
const relay_identity_1 = require("./relay-identity");
|
|
6
|
+
/** The account service of a relay or a gateway (docs/design/ta-account.md): the devices of one user, and their mail. */
|
|
7
|
+
exports.RELAY_ACCOUNT_PATH = '/v1/account/';
|
|
8
|
+
/** Devices of an account at most, removed ones aside: `RELAY_ACCOUNT_MAX_DEVICES` of the service changes it. */
|
|
9
|
+
exports.RELAY_ACCOUNT_MAX_DEVICES = 20;
|
|
10
|
+
/** A device seen within this is online: computers say they are there every minute. */
|
|
11
|
+
exports.RELAY_ACCOUNT_ONLINE_MS = 3 * 60000;
|
|
12
|
+
exports.ACCOUNT_CALLS = ['roster', 'join', 'endorse', 'remove', 'send', 'mail', 'signOut'];
|
|
13
|
+
/** The trust of the devices of an account, kept by each device: the service holds keys, never says who to trust. */
|
|
14
|
+
var AccountCircle;
|
|
15
|
+
(function (AccountCircle) {
|
|
16
|
+
/** What a device signs to let another in. */
|
|
17
|
+
function text(account, device) {
|
|
18
|
+
return ['ta-account-device', account, device.id, device.kind, device.sign, device.box].join('\n');
|
|
19
|
+
}
|
|
20
|
+
AccountCircle.text = text;
|
|
21
|
+
/** Six digits both devices show, the user checks they are the same: they come from the keys of the new one. */
|
|
22
|
+
async function code(device) {
|
|
23
|
+
const digest = await relay_identity_1.RelayKeys.sha256(relay_bytes_1.RelayBytes.concat(relay_bytes_1.RelayBytes.fromBase64(device.sign), relay_bytes_1.RelayBytes.fromBase64(device.box)));
|
|
24
|
+
const number = ((digest[0] << 24) | (digest[1] << 16) | (digest[2] << 8) | digest[3]) >>> 0;
|
|
25
|
+
return String(number % 1000000).padStart(6, '0');
|
|
26
|
+
}
|
|
27
|
+
AccountCircle.code = code;
|
|
28
|
+
function live(device) {
|
|
29
|
+
return !device.removed;
|
|
30
|
+
}
|
|
31
|
+
AccountCircle.live = live;
|
|
32
|
+
/** In, by the service: the first one, or let in by someone. Who trusts it is each device's own. */
|
|
33
|
+
function admitted(device) {
|
|
34
|
+
return live(device) && (!!device.root || !!device.endorsement);
|
|
35
|
+
}
|
|
36
|
+
AccountCircle.admitted = admitted;
|
|
37
|
+
/** Waiting for a device of the account to let it in. */
|
|
38
|
+
function waiting(device) {
|
|
39
|
+
return live(device) && !device.root && !device.endorsement;
|
|
40
|
+
}
|
|
41
|
+
AccountCircle.waiting = waiting;
|
|
42
|
+
/** A device trusting itself alone: the first one, or one not let in yet. */
|
|
43
|
+
function alone(self) {
|
|
44
|
+
return { trusted: { [self.id]: { sign: self.sign, box: self.box } }, removed: [] };
|
|
45
|
+
}
|
|
46
|
+
AccountCircle.alone = alone;
|
|
47
|
+
/**
|
|
48
|
+
* Who to trust now: those trusted before and not removed, and those a trusted device let in with a good signature,
|
|
49
|
+
* until no more. A device the service adds without a signature of one of them is never trusted.
|
|
50
|
+
*/
|
|
51
|
+
async function update(state, roster) {
|
|
52
|
+
const removed = new Set(state.removed);
|
|
53
|
+
for (const device of roster.devices) {
|
|
54
|
+
if (device.removed && state.trusted[device.id]) {
|
|
55
|
+
removed.add(device.id);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const trusted = {};
|
|
59
|
+
for (const [id, keys] of Object.entries(state.trusted)) {
|
|
60
|
+
if (!removed.has(id)) {
|
|
61
|
+
trusted[id] = keys;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (let added = true; added;) {
|
|
65
|
+
added = false;
|
|
66
|
+
for (const device of roster.devices) {
|
|
67
|
+
if (trusted[device.id] || removed.has(device.id) || !live(device) || !device.endorsement || !trusted[device.endorsement.by]) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (await relay_identity_1.RelayIdentityUtil.verify(trusted[device.endorsement.by].sign, text(roster.account.id, device), device.endorsement.signature)) {
|
|
71
|
+
trusted[device.id] = { sign: device.sign, box: device.box };
|
|
72
|
+
added = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { trusted, removed: [...removed] };
|
|
77
|
+
}
|
|
78
|
+
AccountCircle.update = update;
|
|
79
|
+
function endorse(self, account, device) {
|
|
80
|
+
return relay_identity_1.RelayIdentityUtil.sign(self, text(account, device));
|
|
81
|
+
}
|
|
82
|
+
AccountCircle.endorse = endorse;
|
|
83
|
+
/** What a device signs to remove one of the account, itself or another. */
|
|
84
|
+
function removal(account, id) {
|
|
85
|
+
return ['ta-account-remove', account, id].join('\n');
|
|
86
|
+
}
|
|
87
|
+
AccountCircle.removal = removal;
|
|
88
|
+
function remove(self, account, id) {
|
|
89
|
+
return relay_identity_1.RelayIdentityUtil.sign(self, removal(account, id));
|
|
90
|
+
}
|
|
91
|
+
AccountCircle.remove = remove;
|
|
92
|
+
function mailText(mail) {
|
|
93
|
+
var _a;
|
|
94
|
+
return ['ta-account-mail', mail.from, mail.to, mail.kind, (_a = mail.topic) !== null && _a !== void 0 ? _a : '', mail.sealed].join('\n');
|
|
95
|
+
}
|
|
96
|
+
async function key(self, otherBox, from, to) {
|
|
97
|
+
return relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(self, otherBox, `${from}>${to}`, 'ta-account-mail'));
|
|
98
|
+
}
|
|
99
|
+
/** `value` sealed for `to` and signed by `self`: the service carries it, never reads it. */
|
|
100
|
+
async function seal(self, to, kind, value, topic) {
|
|
101
|
+
const sealed = await relay_identity_1.RelayKeys.seal(await key(self, to.box, self.id, to.id), JSON.stringify(value));
|
|
102
|
+
const mail = { from: self.id, to: to.id, kind, ...(topic ? { topic } : {}), sealed };
|
|
103
|
+
return { ...mail, signature: await relay_identity_1.RelayIdentityUtil.sign(self, mailText(mail)) };
|
|
104
|
+
}
|
|
105
|
+
AccountCircle.seal = seal;
|
|
106
|
+
/** What `mail` holds, when it is for `self` and signed by `sender`; undefined otherwise. */
|
|
107
|
+
async function open(self, sender, mail) {
|
|
108
|
+
if (mail.to !== self.id || !(await relay_identity_1.RelayIdentityUtil.verify(sender.sign, mailText(mail), mail.signature))) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
return JSON.parse(await relay_identity_1.RelayKeys.open(await key(self, sender.box, mail.from, mail.to), mail.sealed));
|
|
113
|
+
}
|
|
114
|
+
catch (_a) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
AccountCircle.open = open;
|
|
119
|
+
/** Whether a device is online, from when it was last there. */
|
|
120
|
+
function online(device, now = Date.now()) {
|
|
121
|
+
return live(device) && now - device.seenAt < exports.RELAY_ACCOUNT_ONLINE_MS;
|
|
122
|
+
}
|
|
123
|
+
AccountCircle.online = online;
|
|
124
|
+
})(AccountCircle || (exports.AccountCircle = AccountCircle = {}));
|
|
125
|
+
//# sourceMappingURL=relay-account.js.map
|