@baukit/suite-client 0.0.0-stage → 0.10.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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +73 -2
  3. package/dist/api.d.ts +103 -0
  4. package/dist/api.d.ts.map +1 -0
  5. package/dist/api.js +67 -0
  6. package/dist/api.js.map +1 -0
  7. package/dist/authorize.d.ts +48 -0
  8. package/dist/authorize.d.ts.map +1 -0
  9. package/dist/authorize.js +123 -0
  10. package/dist/authorize.js.map +1 -0
  11. package/dist/cjs/package.json +1 -0
  12. package/dist/cjs/peers.d.ts +14 -0
  13. package/dist/cjs/peers.d.ts.map +1 -0
  14. package/dist/cjs/peers.js +63 -0
  15. package/dist/cjs/peers.js.map +1 -0
  16. package/dist/connected.d.ts +29 -0
  17. package/dist/connected.d.ts.map +1 -0
  18. package/dist/connected.js +51 -0
  19. package/dist/connected.js.map +1 -0
  20. package/dist/index.d.ts +9 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +9 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/linked.d.ts +28 -0
  25. package/dist/linked.d.ts.map +1 -0
  26. package/dist/linked.js +50 -0
  27. package/dist/linked.js.map +1 -0
  28. package/dist/messages.d.ts +455 -0
  29. package/dist/messages.d.ts.map +1 -0
  30. package/dist/messages.js +279 -0
  31. package/dist/messages.js.map +1 -0
  32. package/dist/navigation.d.ts +24 -0
  33. package/dist/navigation.d.ts.map +1 -0
  34. package/dist/navigation.js +80 -0
  35. package/dist/navigation.js.map +1 -0
  36. package/dist/peers.d.ts +14 -0
  37. package/dist/peers.d.ts.map +1 -0
  38. package/dist/peers.js +59 -0
  39. package/dist/peers.js.map +1 -0
  40. package/dist/session.d.ts +26 -0
  41. package/dist/session.d.ts.map +1 -0
  42. package/dist/session.js +88 -0
  43. package/dist/session.js.map +1 -0
  44. package/dist/urls.d.ts +17 -0
  45. package/dist/urls.d.ts.map +1 -0
  46. package/dist/urls.js +46 -0
  47. package/dist/urls.js.map +1 -0
  48. package/package.json +63 -4
  49. package/src/api.ts +139 -0
  50. package/src/authorize.ts +141 -0
  51. package/src/client.test.ts +575 -0
  52. package/src/connected.ts +54 -0
  53. package/src/index.ts +8 -0
  54. package/src/linked.ts +56 -0
  55. package/src/messages.ts +365 -0
  56. package/src/navigation.test.ts +99 -0
  57. package/src/navigation.ts +94 -0
  58. package/src/peers.test.ts +85 -0
  59. package/src/peers.ts +77 -0
  60. package/src/session.ts +88 -0
  61. package/src/urls.ts +58 -0
package/dist/urls.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ export interface SuiteReturnPolicy {
2
+ readonly platform: 'web' | 'native';
3
+ readonly origin: string;
4
+ }
5
+ export declare function suiteReturnUrl(policy: SuiteReturnPolicy): string;
6
+ export declare function suiteReturnValidator(policy: SuiteReturnPolicy): (url: string) => boolean;
7
+ /** Allows only the two suite pages. Other native intents belong to the product. */
8
+ export declare function createSuiteNativeIntentValidator(scheme: string): (value: string) => boolean;
9
+ export declare function openSuitePeer(peer: {
10
+ readonly scheme: string;
11
+ readonly webUrl: string;
12
+ }, options: {
13
+ readonly platform: 'web' | 'native';
14
+ readonly openUrl: (url: string) => Promise<void>;
15
+ readonly allowLoopback?: boolean;
16
+ }): Promise<void>;
17
+ //# sourceMappingURL=urls.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"urls.d.ts","sourceRoot":"","sources":["../src/urls.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,EAAE,KAAK,GAAG,QAAQ,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AACD,wBAAgB,cAAc,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,CAEhE;AACD,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,iBAAiB,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAKxF;AACD,mFAAmF;AACnF,wBAAgB,gCAAgC,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAW3F;AACD,wBAAsB,aAAa,CACjC,IAAI,EAAE;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC1D,OAAO,EAAE;IACP,QAAQ,CAAC,QAAQ,EAAE,KAAK,GAAG,QAAQ,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACjD,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CAClC,GACA,OAAO,CAAC,IAAI,CAAC,CAsBf"}
package/dist/urls.js ADDED
@@ -0,0 +1,46 @@
1
+ import { createReturnUrlValidator } from '@baukit/integrations-client';
2
+ export function suiteReturnUrl(policy) {
3
+ return policy.platform === 'web' ? `${policy.origin}/suite/linked` : `${policy.origin}/linked`;
4
+ }
5
+ export function suiteReturnValidator(policy) {
6
+ return createReturnUrlValidator({
7
+ origin: policy.origin,
8
+ paths: [policy.platform === 'web' ? '/suite/linked' : '/linked'],
9
+ });
10
+ }
11
+ /** Allows only the two suite pages. Other native intents belong to the product. */
12
+ export function createSuiteNativeIntentValidator(scheme) {
13
+ if (!/^[a-z][a-z0-9+.-]*$/.test(scheme))
14
+ throw new TypeError('Invalid app scheme.');
15
+ const routes = new Set([
16
+ `${scheme}://suite/authorize`,
17
+ `${scheme}://suite/linked`,
18
+ `${scheme}:///suite/authorize`,
19
+ `${scheme}:///suite/linked`,
20
+ '/suite/authorize',
21
+ '/suite/linked',
22
+ ]);
23
+ return (value) => routes.has(value.split(/[?#]/, 1)[0] ?? '');
24
+ }
25
+ export async function openSuitePeer(peer, options) {
26
+ const web = new URL(peer.webUrl);
27
+ const loopback = options.allowLoopback === true && ['localhost', '127.0.0.1', '[::1]'].includes(web.hostname);
28
+ if (!/^[a-z][a-z0-9+.-]*$/.test(peer.scheme) ||
29
+ web.username !== '' ||
30
+ web.password !== '' ||
31
+ (web.protocol !== 'https:' && !(web.protocol === 'http:' && loopback))) {
32
+ throw new TypeError('Invalid peer URL.');
33
+ }
34
+ if (options.platform === 'native') {
35
+ try {
36
+ await options.openUrl(`${peer.scheme}://`);
37
+ return;
38
+ }
39
+ catch {
40
+ await options.openUrl(peer.webUrl);
41
+ return;
42
+ }
43
+ }
44
+ await options.openUrl(peer.webUrl);
45
+ }
46
+ //# sourceMappingURL=urls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"urls.js","sourceRoot":"","sources":["../src/urls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAMvE,MAAM,UAAU,cAAc,CAAC,MAAyB;IACtD,OAAO,MAAM,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,eAAe,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,SAAS,CAAC;AACjG,CAAC;AACD,MAAM,UAAU,oBAAoB,CAAC,MAAyB;IAC5D,OAAO,wBAAwB,CAAC;QAC9B,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,KAAK,EAAE,CAAC,MAAM,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,SAAS,CAAC;KACjE,CAAC,CAAC;AACL,CAAC;AACD,mFAAmF;AACnF,MAAM,UAAU,gCAAgC,CAAC,MAAc;IAC7D,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,qBAAqB,CAAC,CAAC;IACpF,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC;QACrB,GAAG,MAAM,oBAAoB;QAC7B,GAAG,MAAM,iBAAiB;QAC1B,GAAG,MAAM,qBAAqB;QAC9B,GAAG,MAAM,kBAAkB;QAC3B,kBAAkB;QAClB,eAAe;KAChB,CAAC,CAAC;IACH,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AAChE,CAAC;AACD,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,IAA0D,EAC1D,OAIC;IAED,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACjC,MAAM,QAAQ,GACZ,OAAO,CAAC,aAAa,KAAK,IAAI,IAAI,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC/F,IACE,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;QACxC,GAAG,CAAC,QAAQ,KAAK,EAAE;QACnB,GAAG,CAAC,QAAQ,KAAK,EAAE;QACnB,CAAC,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,CAAC,GAAG,CAAC,QAAQ,KAAK,OAAO,IAAI,QAAQ,CAAC,CAAC,EACtE,CAAC;QACD,MAAM,IAAI,SAAS,CAAC,mBAAmB,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,OAAO,CAAC,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAClC,IAAI,CAAC;YACH,MAAM,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC;YAC3C,OAAO;QACT,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACnC,OAAO;QACT,CAAC;IACH,CAAC;IACD,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@baukit/suite-client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.10.2",
4
+ "description": "Headless suite linking, authorization state and connected app clients.",
5
+ "license": "MIT",
6
+ "author": "Patrick Koss",
7
+ "homepage": "https://github.com/PatrickKoss/baukit#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/PatrickKoss/baukit.git",
11
+ "directory": "typescript/packages/suite-client"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/PatrickKoss/baukit/issues"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "type": "module",
20
+ "engines": {
21
+ "node": ">=24.0.0"
22
+ },
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "./peers": {
31
+ "import": {
32
+ "types": "./dist/peers.d.ts",
33
+ "default": "./dist/peers.js"
34
+ },
35
+ "require": {
36
+ "types": "./dist/cjs/peers.d.ts",
37
+ "default": "./dist/cjs/peers.js"
38
+ }
39
+ }
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "src",
44
+ "README.md",
45
+ "LICENSE"
46
+ ],
47
+ "scripts": {
48
+ "build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node scripts/build-cjs.mjs",
49
+ "prepare": "pnpm run build",
50
+ "test": "tsc -p tsconfig.test.json && vitest run --config ../../vitest.config.ts --maxWorkers=4 && node --test scripts/peers.test.cjs && node ../../scripts/test-packed-exports.mjs --load ./peers",
51
+ "lint": "eslint src --max-warnings=0",
52
+ "format:check": "prettier --check src scripts package.json turbo.json tsconfig.json tsconfig.test.json tsconfig.cjs.json"
53
+ },
54
+ "devDependencies": {
55
+ "@baukit/integrations-client": "workspace:*",
56
+ "@baukit/localization-core": "workspace:*",
57
+ "@types/node": "26.6.4",
58
+ "typescript": "6.0.3",
59
+ "vitest": "5.0.3"
60
+ },
61
+ "peerDependencies": {
62
+ "@baukit/integrations-client": "^0.10.2",
63
+ "@baukit/localization-core": "^0.10.2"
64
+ }
65
+ }
package/src/api.ts ADDED
@@ -0,0 +1,139 @@
1
+ export type RewardMode = 'native' | 'source_xp' | 'off';
2
+ export type DeliveryHealth = 'healthy' | 'degraded' | 'needs_attention' | 'disabled';
3
+ export interface SuiteLink {
4
+ readonly id: string;
5
+ readonly peerApp: string;
6
+ readonly role: 'initiator' | 'authorizer';
7
+ readonly remoteLinkId: string;
8
+ readonly remoteDisplayName: string | null;
9
+ readonly status: 'active' | 'needs_attention' | 'revoked';
10
+ readonly sends: readonly string[];
11
+ readonly receives: readonly string[];
12
+ readonly shareXp: boolean;
13
+ readonly rewardMode: RewardMode;
14
+ readonly deliveryHealth: DeliveryHealth;
15
+ readonly consecutiveFailures: number;
16
+ readonly lastDeliveryAt: string | null;
17
+ readonly lastFailureAt: string | null;
18
+ readonly lastFailureCode: string | null;
19
+ readonly lastReceivedAt: string | null;
20
+ readonly replayEarliest: string;
21
+ readonly createdAt: string;
22
+ readonly updatedAt: string;
23
+ }
24
+ export interface SuitePeer {
25
+ readonly id: string;
26
+ readonly displayName: string;
27
+ readonly scheme: string;
28
+ readonly webUrl: string;
29
+ readonly shareXpAvailable: boolean;
30
+ readonly sends: readonly string[];
31
+ readonly receives: readonly string[];
32
+ readonly rewardModes: readonly RewardMode[];
33
+ readonly link: string | null;
34
+ }
35
+ export interface AuthorizationPreview {
36
+ readonly peer: string;
37
+ readonly authorizerSends: readonly string[];
38
+ readonly initiatorSends: readonly string[];
39
+ readonly existingLink: boolean;
40
+ readonly autoApprove: boolean;
41
+ readonly hintMismatch: boolean;
42
+ }
43
+ export interface AuthorizationInput {
44
+ readonly client: string;
45
+ readonly state: string;
46
+ readonly codeChallenge: string;
47
+ readonly hint?: string;
48
+ }
49
+ export interface SuiteDelivery {
50
+ readonly jobId: string;
51
+ readonly linkId: string;
52
+ readonly eventType: string | null;
53
+ readonly status: 'pending' | 'running' | 'succeeded' | 'failed' | 'cancelled';
54
+ readonly attemptCount: number;
55
+ readonly lastFailureCode: string | null;
56
+ readonly createdAt: string;
57
+ readonly updatedAt: string;
58
+ }
59
+ /** Supply the product's authenticated, JSON-decoding HTTP transport. */
60
+ export interface SuiteTransport {
61
+ request<T>(method: 'GET' | 'POST' | 'PATCH' | 'DELETE', path: string, body?: unknown): Promise<T>;
62
+ }
63
+ export class SuiteFlowError extends Error {
64
+ public constructor(public readonly code: string) {
65
+ super(code);
66
+ }
67
+ }
68
+ export function suiteErrorCode(error: unknown): string {
69
+ if (error instanceof Error && 'code' in error && typeof error.code === 'string')
70
+ return error.code;
71
+ return 'suite_unavailable';
72
+ }
73
+ const linkPath = (id: string) => `/suite/links/${encodeURIComponent(id)}`;
74
+ export class SuiteClient {
75
+ public constructor(private readonly transport: SuiteTransport) {}
76
+ public peers(): Promise<readonly SuitePeer[]> {
77
+ return this.transport.request('GET', '/suite/peers');
78
+ }
79
+ public list(): Promise<readonly SuiteLink[]> {
80
+ return this.transport.request('GET', '/suite/links');
81
+ }
82
+ public get(id: string): Promise<SuiteLink> {
83
+ return this.transport.request('GET', linkPath(id));
84
+ }
85
+ public start(input: {
86
+ readonly peerApp: string;
87
+ readonly returnUrl: string;
88
+ readonly stateNonce: string;
89
+ }): Promise<{ readonly requestId: string; readonly authorizeUrl: string }> {
90
+ return this.transport.request('POST', '/suite/links', input);
91
+ }
92
+ public complete(requestId: string, code: string): Promise<SuiteLink> {
93
+ return this.transport.request(
94
+ 'POST',
95
+ `/suite/links/requests/${encodeURIComponent(requestId)}/complete`,
96
+ { code },
97
+ );
98
+ }
99
+ public preview(
100
+ client: string,
101
+ hint?: string,
102
+ hintDomain?: string,
103
+ ): Promise<AuthorizationPreview> {
104
+ const query = new URLSearchParams({ client });
105
+ if (hint !== undefined) query.set('hint', hint);
106
+ if (hintDomain !== undefined) query.set('hint_domain', hintDomain);
107
+ return this.transport.request('GET', `/suite/authorizations/preview?${query.toString()}`);
108
+ }
109
+ public authorize(input: AuthorizationInput): Promise<{ readonly redirectUrl: string }> {
110
+ return this.transport.request('POST', '/suite/authorizations', input);
111
+ }
112
+ public deny(input: {
113
+ readonly client: string;
114
+ readonly state: string;
115
+ }): Promise<{ readonly redirectUrl: string }> {
116
+ return this.transport.request('POST', '/suite/authorizations/deny', input);
117
+ }
118
+ public update(
119
+ id: string,
120
+ input: { readonly shareXp?: boolean; readonly rewardMode?: RewardMode },
121
+ ): Promise<SuiteLink> {
122
+ return this.transport.request('PATCH', linkPath(id), input);
123
+ }
124
+ public disconnect(id: string): Promise<void> {
125
+ return this.transport.request('DELETE', linkPath(id));
126
+ }
127
+ public test(id: string): Promise<void> {
128
+ return this.transport.request('POST', `${linkPath(id)}/test`);
129
+ }
130
+ public reenable(id: string): Promise<SuiteLink> {
131
+ return this.transport.request('POST', `${linkPath(id)}/reenable`);
132
+ }
133
+ public replay(id: string, since: string): Promise<void> {
134
+ return this.transport.request('POST', `${linkPath(id)}/replay`, { since });
135
+ }
136
+ public deliveries(id: string): Promise<readonly SuiteDelivery[]> {
137
+ return this.transport.request('GET', `${linkPath(id)}/deliveries`);
138
+ }
139
+ }
@@ -0,0 +1,141 @@
1
+ import {
2
+ SuiteClient,
3
+ suiteErrorCode,
4
+ type AuthorizationInput,
5
+ type AuthorizationPreview,
6
+ } from './api.js';
7
+ export type AuthorizeState =
8
+ | { readonly type: 'loading' | 'login' | 'framed' | 'web_required' | 'invalid' | 'submitting' }
9
+ | { readonly type: 'consent' | 'hint_mismatch'; readonly preview: AuthorizationPreview }
10
+ | { readonly type: 'redirect'; readonly url: string }
11
+ | { readonly type: 'failed'; readonly code: string };
12
+ export interface AuthorizeEnvironment {
13
+ readonly web: boolean;
14
+ readonly framed: boolean;
15
+ readonly signedIn: boolean;
16
+ }
17
+ /** Products render this state and supply their own login and account-switch UI. */
18
+ export class SuiteAuthorizeMachine {
19
+ #state: AuthorizeState = { type: 'loading' };
20
+ #preview: AuthorizationPreview | undefined;
21
+ #started = false;
22
+ public constructor(
23
+ private readonly client: SuiteClient,
24
+ private readonly input: AuthorizationInput & { readonly hintDomain?: string },
25
+ private readonly environment: AuthorizeEnvironment,
26
+ ) {}
27
+ public get state(): AuthorizeState {
28
+ return this.#state;
29
+ }
30
+ public async load(): Promise<AuthorizeState> {
31
+ if (this.#started) return this.#state;
32
+ this.#started = true;
33
+ if (this.environment.framed) return this.set({ type: 'framed' });
34
+ if (!this.environment.web) return this.set({ type: 'web_required' });
35
+ if (!this.input.client || !this.input.state || !this.input.codeChallenge)
36
+ return this.set({ type: 'invalid' });
37
+ if (!this.environment.signedIn) return this.set({ type: 'login' });
38
+ try {
39
+ this.#preview = await this.client.preview(
40
+ this.input.client,
41
+ this.input.hint,
42
+ this.input.hintDomain,
43
+ );
44
+ if (this.#preview.hintMismatch)
45
+ return this.set({ type: 'hint_mismatch', preview: this.#preview });
46
+ if (this.#preview.autoApprove) return await this.approve();
47
+ return this.set({ type: 'consent', preview: this.#preview });
48
+ } catch (error) {
49
+ return this.set({ type: 'failed', code: suiteErrorCode(error) });
50
+ }
51
+ }
52
+ public async approve(): Promise<AuthorizeState> {
53
+ if (
54
+ this.environment.framed ||
55
+ !this.environment.web ||
56
+ !this.environment.signedIn ||
57
+ this.#preview === undefined ||
58
+ this.#preview.hintMismatch ||
59
+ this.#state.type === 'submitting' ||
60
+ this.#state.type === 'redirect'
61
+ )
62
+ return this.#state;
63
+ this.set({ type: 'submitting' });
64
+ try {
65
+ const { client, state, codeChallenge, hint } = this.input;
66
+ const result = await this.client.authorize({
67
+ client,
68
+ state,
69
+ codeChallenge,
70
+ ...(hint === undefined ? {} : { hint }),
71
+ });
72
+ return this.set({ type: 'redirect', url: result.redirectUrl });
73
+ } catch (error) {
74
+ return this.set({ type: 'failed', code: suiteErrorCode(error) });
75
+ }
76
+ }
77
+ public async deny(): Promise<AuthorizeState> {
78
+ if (
79
+ !['consent', 'hint_mismatch', 'failed'].includes(this.#state.type) ||
80
+ this.environment.framed
81
+ )
82
+ return this.#state;
83
+ this.set({ type: 'submitting' });
84
+ try {
85
+ const result = await this.client.deny({ client: this.input.client, state: this.input.state });
86
+ return this.set({ type: 'redirect', url: result.redirectUrl });
87
+ } catch (error) {
88
+ return this.set({ type: 'failed', code: suiteErrorCode(error) });
89
+ }
90
+ }
91
+ private set(state: AuthorizeState): AuthorizeState {
92
+ this.#state = state;
93
+ return state;
94
+ }
95
+ }
96
+
97
+ /** Rejects duplicate protocol values before any preview or authorization request. */
98
+ export function parseSuiteAuthorizeQuery(
99
+ query: URLSearchParams,
100
+ ): (AuthorizationInput & { readonly hintDomain?: string }) | null {
101
+ for (const key of ['client', 'state', 'code_challenge', 'hint', 'hint_domain']) {
102
+ if (query.getAll(key).length > 1) return null;
103
+ }
104
+ const client = query.get('client');
105
+ const state = query.get('state');
106
+ const codeChallenge = query.get('code_challenge');
107
+ if (!client || !state || !codeChallenge) return null;
108
+ const hint = query.get('hint');
109
+ const hintDomain = query.get('hint_domain');
110
+ return {
111
+ client,
112
+ state,
113
+ codeChallenge,
114
+ ...(hint === null ? {} : { hint }),
115
+ ...(hintDomain === null ? {} : { hintDomain }),
116
+ };
117
+ }
118
+ export function suiteAuthorizationReturnPath(
119
+ input: AuthorizationInput & { readonly hintDomain?: string },
120
+ ): string {
121
+ const query = new URLSearchParams({
122
+ client: input.client,
123
+ state: input.state,
124
+ code_challenge: input.codeChallenge,
125
+ });
126
+ if (input.hint !== undefined) query.set('hint', input.hint);
127
+ if (input.hintDomain !== undefined) query.set('hint_domain', input.hintDomain);
128
+ return `/suite/authorize?${query.toString()}`;
129
+ }
130
+ /** Logout must finish before login starts, so auto-approval cannot use the old session. */
131
+ export async function switchSuiteAccount(
132
+ input: AuthorizationInput & { readonly hintDomain?: string },
133
+ auth: {
134
+ readonly logout: (returnPath: string) => Promise<void>;
135
+ readonly login: (returnPath: string) => Promise<void>;
136
+ },
137
+ ): Promise<void> {
138
+ const path = suiteAuthorizationReturnPath(input);
139
+ await auth.logout(path);
140
+ await auth.login(path);
141
+ }