@celljs/ta-relay 4.18.0 → 4.20.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 +3 -0
- package/esm/common/index.js +3 -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-hubs-connection.d.ts +2 -0
- package/esm/common/relay-hubs-connection.js +3 -0
- package/esm/common/relay-hubs-identity.d.ts +24 -2
- package/esm/common/relay-hubs-identity.js +81 -0
- package/esm/common/relay-identity.d.ts +7 -0
- package/esm/common/relay-identity.js +8 -0
- package/esm/common/relay-protocol.d.ts +7 -0
- package/esm/common/relay-protocol.js +7 -0
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- 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/lib/common/index.d.ts +3 -0
- package/lib/common/index.js +3 -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-hubs-connection.d.ts +2 -0
- package/lib/common/relay-hubs-connection.js +3 -0
- package/lib/common/relay-hubs-identity.d.ts +24 -2
- package/lib/common/relay-hubs-identity.js +81 -0
- package/lib/common/relay-identity.d.ts +7 -0
- package/lib/common/relay-identity.js +8 -0
- package/lib/common/relay-protocol.d.ts +7 -0
- package/lib/common/relay-protocol.js +8 -1
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- 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/package.json +3 -3
- package/viewer/join.html +6 -6
- package/viewer/page.html +12 -12
- package/viewer/room.html +13 -13
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type AccountCallName, type AccountDevice, type AccountMail, type AccountName, type AccountRoster, type AccountSignInPoll, type AccountSignInStart } from './relay-account';
|
|
2
|
+
import type { RelayHubStorage } from './relay-hub';
|
|
3
|
+
/** Refused, with the HTTP status it answers with. */
|
|
4
|
+
export declare class RelayAccountError extends Error {
|
|
5
|
+
readonly status: number;
|
|
6
|
+
constructor(status: number, message: string);
|
|
7
|
+
}
|
|
8
|
+
/** What the handler asks the book of one account: a call of a device, or a token to issue once a sign-in is done. */
|
|
9
|
+
export type RelayAccountBookRequest = {
|
|
10
|
+
op: 'issue';
|
|
11
|
+
account: AccountName;
|
|
12
|
+
hash: string;
|
|
13
|
+
} | {
|
|
14
|
+
op: AccountCallName;
|
|
15
|
+
args: unknown[];
|
|
16
|
+
hash?: string;
|
|
17
|
+
account?: AccountName;
|
|
18
|
+
max?: number;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The devices and mail of one account, in the storage a Durable Object or `ta relay` gives it: public keys, sealed
|
|
22
|
+
* mail and hashes of tokens only (docs/design/ta-account.md 3).
|
|
23
|
+
*/
|
|
24
|
+
export declare class RelayAccountBook {
|
|
25
|
+
protected readonly storage: RelayHubStorage;
|
|
26
|
+
constructor(storage: RelayHubStorage);
|
|
27
|
+
handle(request: RelayAccountBookRequest, now?: number): Promise<unknown>;
|
|
28
|
+
protected issue(account: AccountName, hash: string, now: number): Promise<void>;
|
|
29
|
+
/** Whether anyone signed in to this account: a book of a forged token holds nothing. */
|
|
30
|
+
signedIn(): Promise<boolean>;
|
|
31
|
+
protected account(): Promise<AccountName>;
|
|
32
|
+
protected devices(): Promise<AccountDevice[]>;
|
|
33
|
+
protected device(id: string): Promise<AccountDevice>;
|
|
34
|
+
protected roster(seen: unknown, now: number): Promise<AccountRoster>;
|
|
35
|
+
protected join(input: AccountDevice, max: number, now: number): Promise<AccountRoster>;
|
|
36
|
+
protected endorse(id: string, by: string, signature: string, now: number): Promise<AccountRoster>;
|
|
37
|
+
/** By a device let in, or the device itself, with its signature: a token alone removes nothing. */
|
|
38
|
+
protected remove(id: string, by: string, signature: string, now: number): Promise<AccountRoster>;
|
|
39
|
+
protected send(mail: AccountMail, now: number): Promise<void>;
|
|
40
|
+
protected mail(to: string): Promise<AccountMail[]>;
|
|
41
|
+
}
|
|
42
|
+
/** A way to sign in to accounts of a relay, as the device flow of RFC 8628: the relay asks, the user says yes elsewhere. */
|
|
43
|
+
export interface RelayAccountProvider {
|
|
44
|
+
id: string;
|
|
45
|
+
name: string;
|
|
46
|
+
start(): Promise<AccountSignInStart>;
|
|
47
|
+
poll(handle: string): Promise<AccountName | 'pending' | 'slowDown'>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* GitHub's device flow, for Ta's own OAuth app: a client id is public and the flow needs no secret. The device code is
|
|
51
|
+
* the handle: the device that began the sign-in holds it, as RFC 8628 has it; the relay keeps nothing of it.
|
|
52
|
+
*/
|
|
53
|
+
export declare class RelayGitHubAccounts implements RelayAccountProvider {
|
|
54
|
+
protected readonly clientId: string;
|
|
55
|
+
protected readonly site: string;
|
|
56
|
+
protected readonly api: string;
|
|
57
|
+
readonly id = "github";
|
|
58
|
+
readonly name = "GitHub";
|
|
59
|
+
static readonly CLIENT_ID = "Ov23lip46sRQDlDM1xt7";
|
|
60
|
+
constructor(clientId?: string, site?: string, api?: string);
|
|
61
|
+
protected post<T>(path: string, form: Record<string, string>): Promise<T>;
|
|
62
|
+
start(): Promise<AccountSignInStart>;
|
|
63
|
+
poll(handle: string): Promise<AccountName | 'pending' | 'slowDown'>;
|
|
64
|
+
}
|
|
65
|
+
export interface RelayAccountsOptions {
|
|
66
|
+
/** The book of an account, where its requests go one after another. */
|
|
67
|
+
book(account: string): {
|
|
68
|
+
handle(request: RelayAccountBookRequest): Promise<unknown>;
|
|
69
|
+
};
|
|
70
|
+
providers: RelayAccountProvider[];
|
|
71
|
+
/**
|
|
72
|
+
* A gateway: who the bearer is, by a token of its own sign-in. Without it the service issues tokens of its own, once
|
|
73
|
+
* a provider says who signed in.
|
|
74
|
+
*/
|
|
75
|
+
authenticate?(token: string): Promise<AccountName | undefined>;
|
|
76
|
+
maxDevices?: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The account service over HTTP, the same in a Worker, `ta relay` and a gateway: `/v1/account/sign-in`,
|
|
80
|
+
* `/v1/account/sign-in/poll`, `/v1/account/providers` and `/v1/account/call`. Tokens go in a header, never a
|
|
81
|
+
* cookie, so any page may call it.
|
|
82
|
+
*/
|
|
83
|
+
export declare class RelayAccounts {
|
|
84
|
+
protected readonly options: RelayAccountsOptions;
|
|
85
|
+
constructor(options: RelayAccountsOptions);
|
|
86
|
+
static json(value: unknown, status?: number): Response;
|
|
87
|
+
/** `<account id, base64url>.<secret>`: the account it is of, and what only its holder knows. */
|
|
88
|
+
static token(account: string): string;
|
|
89
|
+
static accountOf(token: string): string | undefined;
|
|
90
|
+
handle(request: Request): Promise<Response>;
|
|
91
|
+
protected provider(id: unknown): RelayAccountProvider;
|
|
92
|
+
protected poll(id: unknown, handle: unknown): Promise<AccountSignInPoll>;
|
|
93
|
+
protected call(authorization: string, op: unknown, args: unknown): Promise<unknown>;
|
|
94
|
+
}
|
|
95
|
+
/** The books of a `ta relay` or a gateway, in one process: the requests of one account one after another. */
|
|
96
|
+
export declare class RelayAccountBooks {
|
|
97
|
+
protected readonly storage: (account: string) => RelayHubStorage;
|
|
98
|
+
/** Any token names an account: a book that holds none once its requests are done is not kept. */
|
|
99
|
+
protected readonly books: Map<string, {
|
|
100
|
+
book: RelayAccountBook;
|
|
101
|
+
chain: Promise<unknown>;
|
|
102
|
+
}>;
|
|
103
|
+
constructor(storage: (account: string) => RelayHubStorage);
|
|
104
|
+
book(account: string): {
|
|
105
|
+
handle(request: RelayAccountBookRequest): Promise<unknown>;
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=relay-accounts.d.ts.map
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayAccountBooks = exports.RelayAccounts = exports.RelayGitHubAccounts = exports.RelayAccountBook = exports.RelayAccountError = void 0;
|
|
4
|
+
const relay_account_1 = require("./relay-account");
|
|
5
|
+
const relay_bytes_1 = require("./relay-bytes");
|
|
6
|
+
const relay_identity_1 = require("./relay-identity");
|
|
7
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
8
|
+
/** Refused, with the HTTP status it answers with. */
|
|
9
|
+
class RelayAccountError extends Error {
|
|
10
|
+
constructor(status, message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.status = status;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.RelayAccountError = RelayAccountError;
|
|
16
|
+
const MAIL_MAX = 16 * 1024;
|
|
17
|
+
const TOKENS_MAX = 50;
|
|
18
|
+
const REMOVED_MAX = 100;
|
|
19
|
+
const SEEN_EVERY_MS = 30000;
|
|
20
|
+
const KINDS = new Set(['computer', 'phone', 'browser']);
|
|
21
|
+
const MAIL_KINDS = new Set(['circle', 'pairing', 'package']);
|
|
22
|
+
const TOPIC = /^[@\w./:-]{1,120}$/;
|
|
23
|
+
/**
|
|
24
|
+
* The devices and mail of one account, in the storage a Durable Object or `ta relay` gives it: public keys, sealed
|
|
25
|
+
* mail and hashes of tokens only (docs/design/ta-account.md 3).
|
|
26
|
+
*/
|
|
27
|
+
class RelayAccountBook {
|
|
28
|
+
constructor(storage) {
|
|
29
|
+
this.storage = storage;
|
|
30
|
+
}
|
|
31
|
+
async handle(request, now = Date.now()) {
|
|
32
|
+
var _a, _b, _c;
|
|
33
|
+
if (request.op === 'issue') {
|
|
34
|
+
return this.issue(request.account, request.hash, now);
|
|
35
|
+
}
|
|
36
|
+
if (request.account) {
|
|
37
|
+
const kept = await this.storage.get('account');
|
|
38
|
+
if (!kept || kept.name !== request.account.name) {
|
|
39
|
+
await this.storage.put('account', request.account);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (!request.hash || !(await this.storage.get(`token:${request.hash}`))) {
|
|
43
|
+
throw new RelayAccountError(401, 'Sign in again: this token is no longer of the account.');
|
|
44
|
+
}
|
|
45
|
+
const args = Array.isArray(request.args) ? request.args : [];
|
|
46
|
+
switch (request.op) {
|
|
47
|
+
case 'roster':
|
|
48
|
+
return this.roster(args[0], now);
|
|
49
|
+
case 'join':
|
|
50
|
+
return this.join(args[0], (_a = request.max) !== null && _a !== void 0 ? _a : relay_account_1.RELAY_ACCOUNT_MAX_DEVICES, now);
|
|
51
|
+
case 'endorse':
|
|
52
|
+
return this.endorse(String(args[0]), String(args[1]), String(args[2]), now);
|
|
53
|
+
case 'remove':
|
|
54
|
+
return this.remove(String(args[0]), String((_b = args[1]) !== null && _b !== void 0 ? _b : ''), String((_c = args[2]) !== null && _c !== void 0 ? _c : ''), now);
|
|
55
|
+
case 'send':
|
|
56
|
+
return this.send(args[0], now);
|
|
57
|
+
case 'mail':
|
|
58
|
+
return this.mail(String(args[0]));
|
|
59
|
+
case 'signOut':
|
|
60
|
+
if (request.hash) {
|
|
61
|
+
await this.storage.delete(`token:${request.hash}`);
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
default:
|
|
65
|
+
throw new RelayAccountError(400, 'No such call.');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async issue(account, hash, now) {
|
|
69
|
+
await this.storage.put('account', account);
|
|
70
|
+
const tokens = [...(await this.storage.list({ prefix: 'token:' }))].sort(([, a], [, b]) => a.at - b.at);
|
|
71
|
+
for (const [key] of tokens.slice(0, Math.max(0, tokens.length - TOKENS_MAX + 1))) {
|
|
72
|
+
await this.storage.delete(key);
|
|
73
|
+
}
|
|
74
|
+
await this.storage.put(`token:${hash}`, { at: now });
|
|
75
|
+
}
|
|
76
|
+
/** Whether anyone signed in to this account: a book of a forged token holds nothing. */
|
|
77
|
+
async signedIn() {
|
|
78
|
+
return !!(await this.storage.get('account'));
|
|
79
|
+
}
|
|
80
|
+
async account() {
|
|
81
|
+
const account = await this.storage.get('account');
|
|
82
|
+
if (!account) {
|
|
83
|
+
throw new RelayAccountError(401, 'Sign in first.');
|
|
84
|
+
}
|
|
85
|
+
return account;
|
|
86
|
+
}
|
|
87
|
+
async devices() {
|
|
88
|
+
return [...(await this.storage.list({ prefix: 'device:' })).values()].sort((a, b) => a.addedAt - b.addedAt);
|
|
89
|
+
}
|
|
90
|
+
async device(id) {
|
|
91
|
+
const device = typeof id === 'string' && relay_protocol_1.RELAY_ID.test(id) ? await this.storage.get(`device:${id}`) : undefined;
|
|
92
|
+
if (!device) {
|
|
93
|
+
throw new RelayAccountError(404, 'No such device in the account.');
|
|
94
|
+
}
|
|
95
|
+
return device;
|
|
96
|
+
}
|
|
97
|
+
async roster(seen, now) {
|
|
98
|
+
if (typeof seen === 'string' && relay_protocol_1.RELAY_ID.test(seen)) {
|
|
99
|
+
const device = await this.storage.get(`device:${seen}`);
|
|
100
|
+
if (device && relay_account_1.AccountCircle.live(device) && now - device.seenAt > SEEN_EVERY_MS) {
|
|
101
|
+
await this.storage.put(`device:${seen}`, { ...device, seenAt: now });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { account: await this.account(), devices: await this.devices() };
|
|
105
|
+
}
|
|
106
|
+
async join(input, max, now) {
|
|
107
|
+
const name = typeof (input === null || input === void 0 ? void 0 : input.name) === 'string' ? input.name.trim().slice(0, 64) : '';
|
|
108
|
+
if (!input || !KINDS.has(input.kind) || !name || !(await relay_identity_1.RelayIdentityUtil.check({ id: input.id, name, sign: input.sign, box: input.box }))) {
|
|
109
|
+
throw new RelayAccountError(400, 'Not a device: its id, keys, kind or name.');
|
|
110
|
+
}
|
|
111
|
+
const host = input.host && typeof input.host.relay === 'string' && /^https?:\/\/[^\s]{1,200}$/.test(input.host.relay) && relay_protocol_1.RELAY_ID.test(input.host.id)
|
|
112
|
+
? { relay: input.host.relay.replace(/\/+$/, ''), id: input.host.id }
|
|
113
|
+
: undefined;
|
|
114
|
+
const kept = await this.storage.get(`device:${input.id}`);
|
|
115
|
+
if (kept === null || kept === void 0 ? void 0 : kept.removed) {
|
|
116
|
+
throw new RelayAccountError(403, 'This device was removed from the account: it joins again with new keys.');
|
|
117
|
+
}
|
|
118
|
+
const devices = await this.devices();
|
|
119
|
+
let device;
|
|
120
|
+
if (kept) {
|
|
121
|
+
const { host: _host, ...rest } = kept;
|
|
122
|
+
device = { ...rest, name, ...(host ? { host } : {}), seenAt: now };
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
if (devices.filter(relay_account_1.AccountCircle.live).length >= max) {
|
|
126
|
+
throw new RelayAccountError(409, `An account has ${max} devices at most: remove one first.`);
|
|
127
|
+
}
|
|
128
|
+
const first = !devices.some(relay_account_1.AccountCircle.admitted);
|
|
129
|
+
device = {
|
|
130
|
+
id: input.id,
|
|
131
|
+
kind: input.kind,
|
|
132
|
+
name,
|
|
133
|
+
sign: input.sign,
|
|
134
|
+
box: input.box,
|
|
135
|
+
...(host ? { host } : {}),
|
|
136
|
+
...(first ? { root: true } : {}),
|
|
137
|
+
addedAt: now,
|
|
138
|
+
seenAt: now,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
await this.storage.put(`device:${device.id}`, device);
|
|
142
|
+
return this.roster(undefined, now);
|
|
143
|
+
}
|
|
144
|
+
async endorse(id, by, signature, now) {
|
|
145
|
+
const [device, endorser, account] = [await this.device(id), await this.device(by), await this.account()];
|
|
146
|
+
if (!relay_account_1.AccountCircle.waiting(device)) {
|
|
147
|
+
throw new RelayAccountError(409, 'This device is not waiting to be let in.');
|
|
148
|
+
}
|
|
149
|
+
if (!relay_account_1.AccountCircle.admitted(endorser) || !(await relay_identity_1.RelayIdentityUtil.verify(endorser.sign, relay_account_1.AccountCircle.text(account.id, device), signature))) {
|
|
150
|
+
throw new RelayAccountError(403, 'Only a device of the account lets another in, with its signature.');
|
|
151
|
+
}
|
|
152
|
+
await this.storage.put(`device:${id}`, { ...device, endorsement: { by, signature } });
|
|
153
|
+
return this.roster(undefined, now);
|
|
154
|
+
}
|
|
155
|
+
/** By a device let in, or the device itself, with its signature: a token alone removes nothing. */
|
|
156
|
+
async remove(id, by, signature, now) {
|
|
157
|
+
const device = await this.device(id);
|
|
158
|
+
if (device.removed) {
|
|
159
|
+
return this.roster(undefined, now);
|
|
160
|
+
}
|
|
161
|
+
const remover = by && relay_protocol_1.RELAY_ID.test(by) ? await this.storage.get(`device:${by}`) : undefined;
|
|
162
|
+
const may = remover && (by === id ? relay_account_1.AccountCircle.live(remover) : relay_account_1.AccountCircle.admitted(remover));
|
|
163
|
+
if (!may || !(await relay_identity_1.RelayIdentityUtil.verify(remover.sign, relay_account_1.AccountCircle.removal((await this.account()).id, id), signature))) {
|
|
164
|
+
throw new RelayAccountError(403, 'Only a device of the account removes another, with its signature.');
|
|
165
|
+
}
|
|
166
|
+
await this.storage.put(`device:${id}`, { ...device, removed: { by, signature, at: now } });
|
|
167
|
+
for (const key of (await this.storage.list({ prefix: 'mail:' })).keys()) {
|
|
168
|
+
const [, to, from] = key.split(':');
|
|
169
|
+
if (to === id || from === id) {
|
|
170
|
+
await this.storage.delete(key);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const removed = (await this.devices()).filter(one => one.removed).sort((a, b) => { var _a, _b; var _c, _d; return ((_c = (_a = b.removed) === null || _a === void 0 ? void 0 : _a.at) !== null && _c !== void 0 ? _c : 0) - ((_d = (_b = a.removed) === null || _b === void 0 ? void 0 : _b.at) !== null && _d !== void 0 ? _d : 0); });
|
|
174
|
+
for (const old of removed.slice(REMOVED_MAX)) {
|
|
175
|
+
await this.storage.delete(`device:${old.id}`);
|
|
176
|
+
}
|
|
177
|
+
return this.roster(undefined, now);
|
|
178
|
+
}
|
|
179
|
+
async send(mail, now) {
|
|
180
|
+
if (!mail ||
|
|
181
|
+
!MAIL_KINDS.has(mail.kind) ||
|
|
182
|
+
typeof mail.sealed !== 'string' ||
|
|
183
|
+
typeof mail.signature !== 'string' ||
|
|
184
|
+
mail.sealed.length > MAIL_MAX ||
|
|
185
|
+
(mail.kind === 'package') !== (typeof mail.topic === 'string' && TOPIC.test(mail.topic))) {
|
|
186
|
+
throw new RelayAccountError(400, 'Not a mail.');
|
|
187
|
+
}
|
|
188
|
+
const [from, to] = [await this.device(mail.from), await this.device(mail.to)];
|
|
189
|
+
if (!relay_account_1.AccountCircle.live(from) || !relay_account_1.AccountCircle.live(to)) {
|
|
190
|
+
throw new RelayAccountError(409, 'Mail goes between devices of the account.');
|
|
191
|
+
}
|
|
192
|
+
const { from: f, to: t, kind, topic, sealed, signature } = mail;
|
|
193
|
+
await this.storage.put(`mail:${t}:${f}:${kind}${topic ? `:${topic}` : ''}`, {
|
|
194
|
+
from: f,
|
|
195
|
+
to: t,
|
|
196
|
+
kind,
|
|
197
|
+
...(topic ? { topic } : {}),
|
|
198
|
+
sealed,
|
|
199
|
+
signature,
|
|
200
|
+
at: now,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
async mail(to) {
|
|
204
|
+
await this.device(to);
|
|
205
|
+
return [...(await this.storage.list({ prefix: `mail:${to}:` })).values()];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.RelayAccountBook = RelayAccountBook;
|
|
209
|
+
/**
|
|
210
|
+
* GitHub's device flow, for Ta's own OAuth app: a client id is public and the flow needs no secret. The device code is
|
|
211
|
+
* the handle: the device that began the sign-in holds it, as RFC 8628 has it; the relay keeps nothing of it.
|
|
212
|
+
*/
|
|
213
|
+
class RelayGitHubAccounts {
|
|
214
|
+
constructor(clientId = RelayGitHubAccounts.CLIENT_ID, site = 'https://github.com', api = 'https://api.github.com') {
|
|
215
|
+
this.clientId = clientId;
|
|
216
|
+
this.site = site;
|
|
217
|
+
this.api = api;
|
|
218
|
+
this.id = 'github';
|
|
219
|
+
this.name = 'GitHub';
|
|
220
|
+
}
|
|
221
|
+
async post(path, form) {
|
|
222
|
+
const response = await fetch(this.site + path, {
|
|
223
|
+
method: 'POST',
|
|
224
|
+
headers: { accept: 'application/json', 'content-type': 'application/x-www-form-urlencoded', 'user-agent': 'Ta' },
|
|
225
|
+
body: new URLSearchParams(form).toString(),
|
|
226
|
+
});
|
|
227
|
+
return (await response.json());
|
|
228
|
+
}
|
|
229
|
+
async start() {
|
|
230
|
+
var _a, _b, _c;
|
|
231
|
+
const answer = await this.post('/login/device/code', { client_id: this.clientId, scope: 'read:user' });
|
|
232
|
+
if (!answer.device_code || !answer.user_code || !answer.verification_uri) {
|
|
233
|
+
throw new RelayAccountError(502, `GitHub: ${(_a = answer.error_description) !== null && _a !== void 0 ? _a : 'no code'}`);
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
handle: answer.device_code,
|
|
237
|
+
code: answer.user_code,
|
|
238
|
+
url: answer.verification_uri,
|
|
239
|
+
interval: Number((_b = answer.interval) !== null && _b !== void 0 ? _b : 5),
|
|
240
|
+
expiresIn: Number((_c = answer.expires_in) !== null && _c !== void 0 ? _c : 900),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async poll(handle) {
|
|
244
|
+
var _a, _b, _c, _d;
|
|
245
|
+
const token = await this.post('/login/oauth/access_token', {
|
|
246
|
+
client_id: this.clientId,
|
|
247
|
+
device_code: handle,
|
|
248
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
|
|
249
|
+
});
|
|
250
|
+
if (token.error === 'authorization_pending') {
|
|
251
|
+
return 'pending';
|
|
252
|
+
}
|
|
253
|
+
if (token.error === 'slow_down') {
|
|
254
|
+
return 'slowDown';
|
|
255
|
+
}
|
|
256
|
+
if (!token.access_token) {
|
|
257
|
+
throw new RelayAccountError(400, `GitHub: ${(_b = (_a = token.error_description) !== null && _a !== void 0 ? _a : token.error) !== null && _b !== void 0 ? _b : 'no access token'}`);
|
|
258
|
+
}
|
|
259
|
+
const user = (await (await fetch(`${this.api}/user`, { headers: { accept: 'application/json', authorization: `Bearer ${token.access_token}`, 'user-agent': 'Ta' } })).json());
|
|
260
|
+
if (!user.id) {
|
|
261
|
+
throw new RelayAccountError(502, 'GitHub did not say who signed in.');
|
|
262
|
+
}
|
|
263
|
+
return { id: `github:${user.id}`, name: (_d = (_c = user.login) !== null && _c !== void 0 ? _c : user.name) !== null && _d !== void 0 ? _d : String(user.id) };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
exports.RelayGitHubAccounts = RelayGitHubAccounts;
|
|
267
|
+
RelayGitHubAccounts.CLIENT_ID = 'Ov23lip46sRQDlDM1xt7';
|
|
268
|
+
const CORS = {
|
|
269
|
+
'access-control-allow-origin': '*',
|
|
270
|
+
'access-control-allow-headers': 'authorization, content-type',
|
|
271
|
+
'access-control-allow-methods': 'GET, POST, OPTIONS',
|
|
272
|
+
'access-control-max-age': '86400',
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* The account service over HTTP, the same in a Worker, `ta relay` and a gateway: `/v1/account/sign-in`,
|
|
276
|
+
* `/v1/account/sign-in/poll`, `/v1/account/providers` and `/v1/account/call`. Tokens go in a header, never a
|
|
277
|
+
* cookie, so any page may call it.
|
|
278
|
+
*/
|
|
279
|
+
class RelayAccounts {
|
|
280
|
+
constructor(options) {
|
|
281
|
+
this.options = options;
|
|
282
|
+
}
|
|
283
|
+
static json(value, status = 200) {
|
|
284
|
+
return new Response(JSON.stringify(value !== null && value !== void 0 ? value : null), { status, headers: { 'content-type': 'application/json', 'cache-control': 'no-store', ...CORS } });
|
|
285
|
+
}
|
|
286
|
+
/** `<account id, base64url>.<secret>`: the account it is of, and what only its holder knows. */
|
|
287
|
+
static token(account) {
|
|
288
|
+
return `${relay_bytes_1.RelayBytes.toBase64(new TextEncoder().encode(account))}.${relay_bytes_1.RelayBytes.toBase64(relay_bytes_1.RelayBytes.random(32))}`;
|
|
289
|
+
}
|
|
290
|
+
static accountOf(token) {
|
|
291
|
+
const [account, secret] = token.split('.');
|
|
292
|
+
try {
|
|
293
|
+
return account && secret ? new TextDecoder().decode(relay_bytes_1.RelayBytes.fromBase64(account)) : undefined;
|
|
294
|
+
}
|
|
295
|
+
catch (_a) {
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
async handle(request) {
|
|
300
|
+
var _a, _b;
|
|
301
|
+
if (request.method === 'OPTIONS') {
|
|
302
|
+
return new Response(null, { status: 204, headers: CORS });
|
|
303
|
+
}
|
|
304
|
+
const path = new URL(request.url).pathname.slice(relay_account_1.RELAY_ACCOUNT_PATH.length);
|
|
305
|
+
try {
|
|
306
|
+
if (path === 'providers' && request.method === 'GET') {
|
|
307
|
+
return RelayAccounts.json(this.options.authenticate ? [] : this.options.providers.map(({ id, name }) => ({ id, name })));
|
|
308
|
+
}
|
|
309
|
+
if (request.method !== 'POST') {
|
|
310
|
+
return RelayAccounts.json({ error: 'Not found.' }, 404);
|
|
311
|
+
}
|
|
312
|
+
const body = (await request.json().catch(() => ({})));
|
|
313
|
+
if (path === 'sign-in') {
|
|
314
|
+
return RelayAccounts.json(await this.provider(body.provider).start());
|
|
315
|
+
}
|
|
316
|
+
if (path === 'sign-in/poll') {
|
|
317
|
+
return RelayAccounts.json(await this.poll(body.provider, body.handle));
|
|
318
|
+
}
|
|
319
|
+
if (path === 'call') {
|
|
320
|
+
return RelayAccounts.json({ value: await this.call((_a = request.headers.get('authorization')) !== null && _a !== void 0 ? _a : '', body.op, body.args) });
|
|
321
|
+
}
|
|
322
|
+
return RelayAccounts.json({ error: 'Not found.' }, 404);
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
const status = error instanceof RelayAccountError ? error.status : ((_b = error.status) !== null && _b !== void 0 ? _b : 500);
|
|
326
|
+
return RelayAccounts.json({ error: error instanceof Error ? error.message : String(error) }, status);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
provider(id) {
|
|
330
|
+
const provider = this.options.authenticate ? undefined : this.options.providers.find(one => one.id === id);
|
|
331
|
+
if (!provider) {
|
|
332
|
+
throw new RelayAccountError(404, 'No such way to sign in here.');
|
|
333
|
+
}
|
|
334
|
+
return provider;
|
|
335
|
+
}
|
|
336
|
+
async poll(id, handle) {
|
|
337
|
+
if (typeof handle !== 'string' || !handle || handle.length > 200) {
|
|
338
|
+
throw new RelayAccountError(400, 'No sign-in to poll.');
|
|
339
|
+
}
|
|
340
|
+
const signedIn = await this.provider(id).poll(handle);
|
|
341
|
+
if (signedIn === 'pending' || signedIn === 'slowDown') {
|
|
342
|
+
return { status: 'pending' };
|
|
343
|
+
}
|
|
344
|
+
const token = RelayAccounts.token(signedIn.id);
|
|
345
|
+
await this.options.book(signedIn.id).handle({ op: 'issue', account: signedIn, hash: await relay_identity_1.RelayKeys.hash(token) });
|
|
346
|
+
return { status: 'done', token, account: signedIn };
|
|
347
|
+
}
|
|
348
|
+
async call(authorization, op, args) {
|
|
349
|
+
const token = authorization.replace(/^Bearer\s+/i, '');
|
|
350
|
+
if (!relay_account_1.ACCOUNT_CALLS.includes(op)) {
|
|
351
|
+
throw new RelayAccountError(400, 'No such call.');
|
|
352
|
+
}
|
|
353
|
+
const request = { op: op, args: Array.isArray(args) ? args : [], max: this.options.maxDevices };
|
|
354
|
+
if (this.options.authenticate) {
|
|
355
|
+
const account = token ? await this.options.authenticate(token) : undefined;
|
|
356
|
+
if (!account) {
|
|
357
|
+
throw new RelayAccountError(401, 'Sign in again.');
|
|
358
|
+
}
|
|
359
|
+
return this.options.book(account.id).handle({ ...request, account });
|
|
360
|
+
}
|
|
361
|
+
const account = RelayAccounts.accountOf(token);
|
|
362
|
+
if (!account) {
|
|
363
|
+
throw new RelayAccountError(401, 'Sign in first.');
|
|
364
|
+
}
|
|
365
|
+
return this.options.book(account).handle({ ...request, hash: await relay_identity_1.RelayKeys.hash(token) });
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
exports.RelayAccounts = RelayAccounts;
|
|
369
|
+
/** The books of a `ta relay` or a gateway, in one process: the requests of one account one after another. */
|
|
370
|
+
class RelayAccountBooks {
|
|
371
|
+
constructor(storage) {
|
|
372
|
+
this.storage = storage;
|
|
373
|
+
/** Any token names an account: a book that holds none once its requests are done is not kept. */
|
|
374
|
+
this.books = new Map();
|
|
375
|
+
}
|
|
376
|
+
book(account) {
|
|
377
|
+
let kept = this.books.get(account);
|
|
378
|
+
if (!kept) {
|
|
379
|
+
kept = { book: new RelayAccountBook(this.storage(account)), chain: Promise.resolve() };
|
|
380
|
+
this.books.set(account, kept);
|
|
381
|
+
}
|
|
382
|
+
const one = kept;
|
|
383
|
+
return {
|
|
384
|
+
handle: request => {
|
|
385
|
+
const done = one.chain.then(() => one.book.handle(request));
|
|
386
|
+
const next = done
|
|
387
|
+
.catch(() => undefined)
|
|
388
|
+
.then(async () => {
|
|
389
|
+
// Dropped only when nothing came after it, before or while it looked.
|
|
390
|
+
if (one.chain === next && !(await one.book.signedIn()) && one.chain === next && this.books.get(account) === one) {
|
|
391
|
+
this.books.delete(account);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
one.chain = next;
|
|
395
|
+
return done;
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
exports.RelayAccountBooks = RelayAccountBooks;
|
|
401
|
+
//# sourceMappingURL=relay-accounts.js.map
|
|
@@ -97,6 +97,8 @@ export declare abstract class RelayHubsConnection {
|
|
|
97
97
|
/** A member asks the user to be friends, by the link of its room of friends. */
|
|
98
98
|
protected offered(_live: Live, _from: RelayPublicIdentity, _link: string): void;
|
|
99
99
|
protected readRoster(live: Live, sealed: string): Promise<void>;
|
|
100
|
+
/** The state of the room has this Ta's picture as it is now (RelayHubs). */
|
|
101
|
+
protected keepPicture(_live: Live): Promise<void>;
|
|
100
102
|
/**
|
|
101
103
|
* The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
|
|
102
104
|
* a member tells the owner, once a connection. A room joined with another identity keeps that one's.
|
|
@@ -422,7 +422,10 @@ class RelayHubsConnection {
|
|
|
422
422
|
}
|
|
423
423
|
// Not awaited: what it sends waits for the room to be ready, which may wait for this event.
|
|
424
424
|
void this.keepLook(live);
|
|
425
|
+
void this.keepPicture(live).catch(() => undefined);
|
|
425
426
|
}
|
|
427
|
+
/** The state of the room has this Ta's picture as it is now (RelayHubs). */
|
|
428
|
+
async keepPicture(_live) { }
|
|
426
429
|
/**
|
|
427
430
|
* The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
|
|
428
431
|
* a member tells the owner, once a connection. A room joined with another identity keeps that one's.
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { RelayHubsConnection } from './relay-hubs-connection';
|
|
1
|
+
import { type RelayFriends } from './relay-friends';
|
|
2
|
+
import { Live, RelayHubsConnection } from './relay-hubs-connection';
|
|
3
3
|
import type { RelayIdentityBundle, RelayIdentityImport, RelayRoomRecord } from './relay-hubs-protocol';
|
|
4
4
|
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
|
|
5
5
|
export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
|
|
6
6
|
protected abstract readonly book: RelayFriends;
|
|
7
|
+
abstract setState(roomId: string, key: string, value: unknown, version?: number): Promise<{
|
|
8
|
+
ok: boolean;
|
|
9
|
+
version: number;
|
|
10
|
+
}>;
|
|
7
11
|
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
8
12
|
setName(name: string): Promise<RelayPublicIdentity>;
|
|
9
13
|
/**
|
|
@@ -11,6 +15,24 @@ export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
|
|
|
11
15
|
* look that is none. The owners of rooms this Ta is in hear it, as a name.
|
|
12
16
|
*/
|
|
13
17
|
setLook(look: string | undefined): Promise<RelayPublicIdentity>;
|
|
18
|
+
/**
|
|
19
|
+
* The picture others see of the user (RelayIdentity.picture), or with null none, the dinosaur of the look again: put
|
|
20
|
+
* in the rooms this Ta is in now, and in those it connects to later.
|
|
21
|
+
*/
|
|
22
|
+
setPicture(picture: string | null): Promise<RelayIdentity>;
|
|
23
|
+
/**
|
|
24
|
+
* Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
|
|
25
|
+
* ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
|
|
26
|
+
*/
|
|
27
|
+
protected keepPicture(live: Live): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* The pictures of the Ta this one knows from its rooms, by the id of each (RelayIdentity.picture), its own among them:
|
|
30
|
+
* what each member put in a room under its member id, the one a Ta put in a room it owns (its room of friends) first.
|
|
31
|
+
*/
|
|
32
|
+
pictures(): Promise<{
|
|
33
|
+
self: string;
|
|
34
|
+
pictures: Record<string, string>;
|
|
35
|
+
}>;
|
|
14
36
|
/** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
|
|
15
37
|
protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
|
|
16
38
|
/** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHubsIdentity = void 0;
|
|
4
|
+
const relay_friends_1 = require("./relay-friends");
|
|
4
5
|
const relay_hubs_connection_1 = require("./relay-hubs-connection");
|
|
5
6
|
const relay_hubs_util_1 = require("./relay-hubs-util");
|
|
6
7
|
const relay_identity_1 = require("./relay-identity");
|
|
8
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
7
9
|
class RelayHubsIdentity extends relay_hubs_connection_1.RelayHubsConnection {
|
|
8
10
|
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
9
11
|
async setName(name) {
|
|
@@ -22,6 +24,85 @@ class RelayHubsIdentity extends relay_hubs_connection_1.RelayHubsConnection {
|
|
|
22
24
|
const next = relay_identity_1.RelayIdentityUtil.withLook(identity, look);
|
|
23
25
|
return next.look === identity.look ? relay_identity_1.RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
|
|
24
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* The picture others see of the user (RelayIdentity.picture), or with null none, the dinosaur of the look again: put
|
|
29
|
+
* in the rooms this Ta is in now, and in those it connects to later.
|
|
30
|
+
*/
|
|
31
|
+
async setPicture(picture) {
|
|
32
|
+
var _a;
|
|
33
|
+
if (picture !== null && !relay_identity_1.RelayIdentityUtil.isPicture(picture)) {
|
|
34
|
+
throw new Error('A picture is a data URL of a PNG, JPEG, WebP or SVG, of 16 KB at most.');
|
|
35
|
+
}
|
|
36
|
+
const identity = { ...(await this.identity()) };
|
|
37
|
+
if (((_a = identity.picture) !== null && _a !== void 0 ? _a : null) !== picture) {
|
|
38
|
+
if (picture === null) {
|
|
39
|
+
delete identity.picture;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
identity.picture = picture;
|
|
43
|
+
}
|
|
44
|
+
this.options.store.write('identity', identity);
|
|
45
|
+
this.own = identity;
|
|
46
|
+
// The pages draw the user by it, and read it again.
|
|
47
|
+
this.emit({ type: 'rooms' });
|
|
48
|
+
}
|
|
49
|
+
for (const live of this.live.values()) {
|
|
50
|
+
void this.keepPicture(live).catch(() => undefined);
|
|
51
|
+
}
|
|
52
|
+
return identity;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
|
|
56
|
+
* ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
|
|
57
|
+
*/
|
|
58
|
+
async keepPicture(live) {
|
|
59
|
+
var _a, _b, _c;
|
|
60
|
+
const { record } = live;
|
|
61
|
+
if (record.status !== 'member' || record.identity || (relay_friends_1.RelayFriendsUtil.isKind(record.kind) && !record.own)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
await this.ready(live);
|
|
65
|
+
const picture = (_a = (await this.identity()).picture) !== null && _a !== void 0 ? _a : null;
|
|
66
|
+
const key = relay_protocol_1.RELAY_PICTURE_KEY + record.member;
|
|
67
|
+
const kept = live.state.get(key);
|
|
68
|
+
if (live.record.status !== 'member' || ((_b = kept === null || kept === void 0 ? void 0 : kept.value) !== null && _b !== void 0 ? _b : null) === picture) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
await this.setState(record.id, key, picture, (_c = kept === null || kept === void 0 ? void 0 : kept.version) !== null && _c !== void 0 ? _c : 0);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The pictures of the Ta this one knows from its rooms, by the id of each (RelayIdentity.picture), its own among them:
|
|
75
|
+
* what each member put in a room under its member id, the one a Ta put in a room it owns (its room of friends) first.
|
|
76
|
+
*/
|
|
77
|
+
async pictures() {
|
|
78
|
+
const own = await this.identity();
|
|
79
|
+
const pictures = {};
|
|
80
|
+
const owned = new Set();
|
|
81
|
+
for (const live of this.live.values()) {
|
|
82
|
+
if (live.record.status !== 'member' || !live.roster) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
for (const member of live.roster.members) {
|
|
86
|
+
const kept = live.state.get(relay_protocol_1.RELAY_PICTURE_KEY + member.member);
|
|
87
|
+
if (member.guest || (kept === null || kept === void 0 ? void 0 : kept.by) !== member.member || !relay_identity_1.RelayIdentityUtil.isPicture(kept.value) || owned.has(member.id)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (!(member.id in pictures) || member.owner) {
|
|
91
|
+
pictures[member.id] = kept.value;
|
|
92
|
+
}
|
|
93
|
+
if (member.owner) {
|
|
94
|
+
owned.add(member.id);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (own.picture) {
|
|
99
|
+
pictures[own.id] = own.picture;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
delete pictures[own.id];
|
|
103
|
+
}
|
|
104
|
+
return { self: own.id, pictures };
|
|
105
|
+
}
|
|
25
106
|
/** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
|
|
26
107
|
async changeSelf(identity) {
|
|
27
108
|
var _a;
|