@adminide-stack/clock-tik-mobile 12.0.37-alpha.1 → 12.0.37-alpha.2

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.
@@ -8,10 +8,16 @@
8
8
  * a minute) that signs the sheet in and goes on to the page. Without one (signed out here, offline, a
9
9
  * Yantra that does not have it yet) the page opens as before.
10
10
  *
11
+ * A Yantra surface (`https://<name>-surface.<base>/`: Travel, Shopping...) is opened the same way: the
12
+ * link goes to the Yantra app of that base, signs the sheet in there and goes on to the surface, which finds
13
+ * the session (its cookie covers the whole base) and mints its own token from it. Without it the surface
14
+ * showed its own "Sign in to Yantra" card.
15
+ *
11
16
  * The ID token only ever goes to a Yantra app this build knows: the configured one, the one Lucy's agent
12
17
  * names, and production's and staging's. A link to anywhere else (one in Lucy's reply, say) opens as is.
13
18
  */
14
19
  import { agentAppUrl, auth0Token, KNOWN_YANTRA_APPS } from '../chat/auth';
20
+ import { platformBase } from '../chat/results';
15
21
  import { openInSheet, openUrl, yantraAppUrl } from './links';
16
22
 
17
23
  export const APP_SIGN_IN_START_PATH = '/resources/app-sign-in/start';
@@ -24,7 +30,11 @@ export interface SignInDeps {
24
30
  fetch: typeof fetch;
25
31
  }
26
32
 
27
- const defaultDeps: SignInDeps = { token: auth0Token, agentApp: agentAppUrl, fetch: (...a) => fetch(...a) };
33
+ const defaultDeps: SignInDeps = {
34
+ token: auth0Token,
35
+ agentApp: agentAppUrl,
36
+ fetch: (input, init) => fetch(input, init),
37
+ };
28
38
 
29
39
  function originOf(url: string): string {
30
40
  try {
@@ -34,15 +44,30 @@ function originOf(url: string): string {
34
44
  }
35
45
  }
36
46
 
37
- /** The Yantra apps the ID token may go to. */
47
+ /** The Yantra apps the ID token may go to: the one Lucy's agent names first, then the configured one. */
38
48
  export async function yantraOrigins(agentApp: SignInDeps['agentApp'] = agentAppUrl): Promise<Set<string>> {
39
49
  const named = await agentApp().catch(() => '');
40
- return new Set([yantraAppUrl(), named, ...KNOWN_YANTRA_APPS].map(originOf).filter(Boolean));
50
+ return new Set([named, yantraAppUrl(), ...KNOWN_YANTRA_APPS].map(originOf).filter(Boolean));
51
+ }
52
+
53
+ /**
54
+ * The Yantra app a surface belongs to: for `https://<name>-surface.<base>`, the first of `apps` whose host has
55
+ * that base (platformBase); '' for anything else. Read with patterns: React Native's URL has no hostname.
56
+ */
57
+ export function appForSurface(origin: string, apps: Iterable<string>): string {
58
+ const m = /^https:\/\/[a-z0-9-]+-surface\.([a-z0-9.-]+)$/i.exec(origin);
59
+ if (!m) return '';
60
+ const base = m[1].toLowerCase();
61
+ for (const app of apps) {
62
+ const host = /^https:\/\/([^/:]+)$/i.exec(app)?.[1];
63
+ if (host && platformBase(host) === base) return app;
64
+ }
65
+ return '';
41
66
  }
42
67
 
43
68
  /**
44
69
  * `url` as a link that opens it signed in to the person's Yantra account, when it is a page of a Yantra
45
- * app and Yantra gives one; else `url` itself.
70
+ * app or one of its surfaces and Yantra gives one; else `url` itself.
46
71
  */
47
72
  export async function signedInYantraLink(url: string, deps: SignInDeps = defaultDeps): Promise<string> {
48
73
  let target: URL;
@@ -51,7 +76,12 @@ export async function signedInYantraLink(url: string, deps: SignInDeps = default
51
76
  } catch {
52
77
  return url;
53
78
  }
54
- if (target.protocol !== 'https:' || !(await yantraOrigins(deps.agentApp)).has(target.origin)) return url;
79
+ if (target.protocol !== 'https:') return url;
80
+ const apps = await yantraOrigins(deps.agentApp);
81
+ // A page of the app itself, or a surface: then the app of its base signs the sheet in and goes on to it.
82
+ const app = apps.has(target.origin) ? target.origin : appForSurface(target.origin, apps);
83
+ if (!app) return url;
84
+ const returnTo = app === target.origin ? `${target.pathname}${target.search}${target.hash}` : target.toString();
55
85
 
56
86
  let token: string;
57
87
  try {
@@ -63,17 +93,17 @@ export async function signedInYantraLink(url: string, deps: SignInDeps = default
63
93
  const controller = new AbortController();
64
94
  const timer = setTimeout(() => controller.abort(), START_TIMEOUT_MS);
65
95
  try {
66
- const res = await deps.fetch(`${target.origin}${APP_SIGN_IN_START_PATH}`, {
96
+ const res = await deps.fetch(`${app}${APP_SIGN_IN_START_PATH}`, {
67
97
  method: 'POST',
68
98
  headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
69
- body: JSON.stringify({ returnTo: `${target.pathname}${target.search}${target.hash}` }),
99
+ body: JSON.stringify({ returnTo }),
70
100
  signal: controller.signal,
71
101
  });
72
102
  if (!res.ok) return url;
73
103
  const body = (await res.json().catch(() => null)) as { url?: unknown } | null;
74
104
  const link = typeof body?.url === 'string' ? body.url : '';
75
- // Only ever a page of the same Yantra.
76
- return link && originOf(link) === target.origin ? link : url;
105
+ // Only ever the sign-in page of the Yantra that was asked.
106
+ return link && originOf(link) === app ? link : url;
77
107
  } catch {
78
108
  return url;
79
109
  } finally {
@@ -87,7 +117,7 @@ export async function openYantraUrl(url: string): Promise<void> {
87
117
  return openUrl(await signedInYantraLink(url));
88
118
  }
89
119
 
90
- /** openInSheet, signed in when it is a Yantra page. */
120
+ /** openInSheet, signed in when it is a Yantra page or surface. */
91
121
  export async function openYantraInSheet(url: string): Promise<void> {
92
122
  if (!url) return;
93
123
  return openInSheet(await signedInYantraLink(url));