@celljs/ta-gateway 4.17.0 → 4.18.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.
@@ -79,7 +79,7 @@ export var GatewayCli;
79
79
  return;
80
80
  }
81
81
  const url = await gateway.listen();
82
- const ways = gateway.flow.providers.map(provider => provider.name);
82
+ const ways = [...gateway.flow.providers, ...gateway.flow.polled].map(provider => provider.name);
83
83
  console.log(`ta gateway: ${url} (${dir})`);
84
84
  console.log(`ta gateway: sandboxes of ${gateway.environments.providers.map(provider => `${provider.id} (${provider.type})`).join(', ') || 'none'}${ways.length ? `; sign-in with ${ways.join(', ')}` : '; no provider of accounts: sign people in with ta gateway link <account>'}`);
85
85
  const stop = () => void gateway.close().then(() => process.exit(0));
@@ -127,7 +127,10 @@ export declare class GatewayServer {
127
127
  protected signedIn(response: ServerResponse, token: string, account: SignInAccount, origin: string): void;
128
128
  /** As the pages of signing in read it: behind a proxy of TLS, its X-Forwarded-Proto makes the cookie Secure. */
129
129
  protected origin(request: IncomingMessage): string;
130
- /** `/api/files` of the Ta of the tenant whose cookie came along. */
130
+ /**
131
+ * `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
132
+ * another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
133
+ */
131
134
  protected files(request: IncomingMessage, response: ServerResponse): Promise<void>;
132
135
  protected page(request: IncomingMessage, response: ServerResponse, path: string): void;
133
136
  protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
@@ -163,7 +163,7 @@ export class GatewayServer {
163
163
  const owners = AccessUtil.parseList(env.TA_GATEWAY_ADMINS);
164
164
  this.policy = new GatewayPolicy(options.dir, owners);
165
165
  const allowed = [...AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
166
- this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join(options.dir, 'access.json'), allowed), SignInProviders.publicUrl(env));
166
+ this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join(options.dir, 'access.json'), allowed), SignInProviders.publicUrl(env), SignInProviders.polled(env));
167
167
  this.tenants = new TenantStore(options.dir);
168
168
  this.links = new SignInLinks(options.dir);
169
169
  this.environments = new Environments(providers, this.tenants, {
@@ -280,6 +280,7 @@ export class GatewayServer {
280
280
  }
281
281
  async close() {
282
282
  clearInterval(this.ticker);
283
+ this.flow.stopPolling();
283
284
  await this.tick().catch(() => undefined);
284
285
  for (const socket of this.sockets) {
285
286
  socket.close(1001, 'The gateway is stopping.');
@@ -424,15 +425,30 @@ export class GatewayServer {
424
425
  origin(request) {
425
426
  return SignInUtil.origin(request, this.flow.publicUrl);
426
427
  }
427
- /** `/api/files` of the Ta of the tenant whose cookie came along. */
428
+ /**
429
+ * `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
430
+ * another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
431
+ */
428
432
  async files(request, response) {
429
433
  var _a, _b;
434
+ const header = request.headers.authorization;
435
+ const bearer = (header === null || header === void 0 ? void 0 : header.startsWith('Bearer ')) ? header.slice('Bearer '.length) : undefined;
436
+ if (bearer !== undefined || request.method === 'OPTIONS') {
437
+ response.setHeader('Access-Control-Allow-Origin', '*');
438
+ response.setHeader('Access-Control-Allow-Headers', 'Authorization, Range');
439
+ response.setHeader('Access-Control-Expose-Headers', 'Content-Type, Content-Length, Content-Range');
440
+ response.setHeader('Access-Control-Max-Age', '600');
441
+ }
442
+ if (request.method === 'OPTIONS') {
443
+ SignInPageUtil.end(response, 204);
444
+ return;
445
+ }
430
446
  if (request.method !== 'GET' && request.method !== 'HEAD') {
431
447
  response.setHeader('Allow', 'GET, HEAD');
432
448
  SignInPageUtil.end(response, 405);
433
449
  return;
434
450
  }
435
- const caller = this.caller((_a = SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
451
+ const caller = this.caller((_a = bearer !== null && bearer !== void 0 ? bearer : SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
436
452
  if (!caller) {
437
453
  SignInPageUtil.end(response, 404);
438
454
  return;
@@ -82,7 +82,7 @@ var GatewayCli;
82
82
  return;
83
83
  }
84
84
  const url = await gateway.listen();
85
- const ways = gateway.flow.providers.map(provider => provider.name);
85
+ const ways = [...gateway.flow.providers, ...gateway.flow.polled].map(provider => provider.name);
86
86
  console.log(`ta gateway: ${url} (${dir})`);
87
87
  console.log(`ta gateway: sandboxes of ${gateway.environments.providers.map(provider => `${provider.id} (${provider.type})`).join(', ') || 'none'}${ways.length ? `; sign-in with ${ways.join(', ')}` : '; no provider of accounts: sign people in with ta gateway link <account>'}`);
88
88
  const stop = () => void gateway.close().then(() => process.exit(0));
@@ -127,7 +127,10 @@ export declare class GatewayServer {
127
127
  protected signedIn(response: ServerResponse, token: string, account: SignInAccount, origin: string): void;
128
128
  /** As the pages of signing in read it: behind a proxy of TLS, its X-Forwarded-Proto makes the cookie Secure. */
129
129
  protected origin(request: IncomingMessage): string;
130
- /** `/api/files` of the Ta of the tenant whose cookie came along. */
130
+ /**
131
+ * `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
132
+ * another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
133
+ */
131
134
  protected files(request: IncomingMessage, response: ServerResponse): Promise<void>;
132
135
  protected page(request: IncomingMessage, response: ServerResponse, path: string): void;
133
136
  protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
@@ -166,7 +166,7 @@ class GatewayServer {
166
166
  const owners = access_store_1.AccessUtil.parseList(env.TA_GATEWAY_ADMINS);
167
167
  this.policy = new gateway_policy_1.GatewayPolicy(options.dir, owners);
168
168
  const allowed = [...access_store_1.AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
169
- this.flow = new sign_in_flow_1.SignInFlow(sign_in_provider_1.SignInProviders.fromEnv(env), new access_store_1.AccessStore((0, node_path_1.join)(options.dir, 'access.json'), allowed), sign_in_provider_1.SignInProviders.publicUrl(env));
169
+ this.flow = new sign_in_flow_1.SignInFlow(sign_in_provider_1.SignInProviders.fromEnv(env), new access_store_1.AccessStore((0, node_path_1.join)(options.dir, 'access.json'), allowed), sign_in_provider_1.SignInProviders.publicUrl(env), sign_in_provider_1.SignInProviders.polled(env));
170
170
  this.tenants = new tenant_store_1.TenantStore(options.dir);
171
171
  this.links = new sign_in_links_1.SignInLinks(options.dir);
172
172
  this.environments = new environments_1.Environments(providers, this.tenants, {
@@ -283,6 +283,7 @@ class GatewayServer {
283
283
  }
284
284
  async close() {
285
285
  clearInterval(this.ticker);
286
+ this.flow.stopPolling();
286
287
  await this.tick().catch(() => undefined);
287
288
  for (const socket of this.sockets) {
288
289
  socket.close(1001, 'The gateway is stopping.');
@@ -427,15 +428,30 @@ class GatewayServer {
427
428
  origin(request) {
428
429
  return sign_in_flow_1.SignInUtil.origin(request, this.flow.publicUrl);
429
430
  }
430
- /** `/api/files` of the Ta of the tenant whose cookie came along. */
431
+ /**
432
+ * `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
433
+ * another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
434
+ */
431
435
  async files(request, response) {
432
436
  var _a, _b;
437
+ const header = request.headers.authorization;
438
+ const bearer = (header === null || header === void 0 ? void 0 : header.startsWith('Bearer ')) ? header.slice('Bearer '.length) : undefined;
439
+ if (bearer !== undefined || request.method === 'OPTIONS') {
440
+ response.setHeader('Access-Control-Allow-Origin', '*');
441
+ response.setHeader('Access-Control-Allow-Headers', 'Authorization, Range');
442
+ response.setHeader('Access-Control-Expose-Headers', 'Content-Type, Content-Length, Content-Range');
443
+ response.setHeader('Access-Control-Max-Age', '600');
444
+ }
445
+ if (request.method === 'OPTIONS') {
446
+ sign_in_pages_1.SignInPageUtil.end(response, 204);
447
+ return;
448
+ }
433
449
  if (request.method !== 'GET' && request.method !== 'HEAD') {
434
450
  response.setHeader('Allow', 'GET, HEAD');
435
451
  sign_in_pages_1.SignInPageUtil.end(response, 405);
436
452
  return;
437
453
  }
438
- const caller = this.caller((_a = sign_in_pages_1.SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
454
+ const caller = this.caller((_a = bearer !== null && bearer !== void 0 ? bearer : sign_in_pages_1.SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
439
455
  if (!caller) {
440
456
  sign_in_pages_1.SignInPageUtil.end(response, 404);
441
457
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-gateway",
3
- "version": "4.17.0",
3
+ "version": "4.18.0",
4
4
  "description": "ta gateway: one Ta per account, each in a sandbox of its own, behind one sign-in",
5
5
  "homepage": "https://github.com/cellbang/ta",
6
6
  "bugs": {
@@ -29,9 +29,9 @@
29
29
  "watch": "tsc -b -w --preserveWatchOutput"
30
30
  },
31
31
  "dependencies": {
32
- "@celljs/rpc": "4.17.0",
33
- "@celljs/ta-host": "4.17.0",
34
- "@celljs/ta-protocol": "4.17.0"
32
+ "@celljs/rpc": "4.18.0",
33
+ "@celljs/ta-host": "4.18.0",
34
+ "@celljs/ta-protocol": "4.18.0"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"