@baukit/auth-web 0.10.31 → 0.11.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/README.md +122 -1
- package/dist/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -72
- package/dist/index.js.map +1 -1
- package/dist/oidc-helpers.d.ts +11 -0
- package/dist/oidc-helpers.d.ts.map +1 -0
- package/dist/oidc-helpers.js +73 -0
- package/dist/oidc-helpers.js.map +1 -0
- package/dist/silent-token-broker.d.ts +83 -0
- package/dist/silent-token-broker.d.ts.map +1 -0
- package/dist/silent-token-broker.js +386 -0
- package/dist/silent-token-broker.js.map +1 -0
- package/dist/silent-token-browser.d.ts +6 -0
- package/dist/silent-token-browser.d.ts.map +1 -0
- package/dist/silent-token-browser.js +84 -0
- package/dist/silent-token-browser.js.map +1 -0
- package/dist/silent-token-errors.d.ts +7 -0
- package/dist/silent-token-errors.d.ts.map +1 -0
- package/dist/silent-token-errors.js +10 -0
- package/dist/silent-token-errors.js.map +1 -0
- package/package.json +7 -3
- package/src/index.ts +14 -85
- package/src/oidc-helpers.ts +89 -0
- package/src/silent-token-broker.test.ts +900 -0
- package/src/silent-token-broker.ts +527 -0
- package/src/silent-token-browser.test.ts +161 -0
- package/src/silent-token-browser.ts +92 -0
- package/src/silent-token-errors.ts +29 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { SilentAuthorizationWindow } from './silent-token-broker.js';
|
|
2
|
+
import { SilentTokenError } from './silent-token-errors.js';
|
|
3
|
+
|
|
4
|
+
export const CALLBACK_MESSAGE = 'baukit-silent-token-callback';
|
|
5
|
+
const POPUP_FEATURES = 'popup,width=520,height=720';
|
|
6
|
+
|
|
7
|
+
/** Run on a dedicated same-origin callback page with no application initialization. */
|
|
8
|
+
export function completeSilentTokenCallback(browser: Window = window): boolean {
|
|
9
|
+
const callbackUrl = browser.location.href;
|
|
10
|
+
browser.history.replaceState(null, '', browser.location.pathname);
|
|
11
|
+
const opener: Window | null = browser.opener as Window | null;
|
|
12
|
+
const recipient = opener ?? (browser.parent !== browser ? browser.parent : null);
|
|
13
|
+
if (!recipient) return false;
|
|
14
|
+
recipient.postMessage({ type: CALLBACK_MESSAGE, url: callbackUrl }, browser.location.origin);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function openSilentAuthorizationWindow(
|
|
19
|
+
browser: Window,
|
|
20
|
+
interactive: boolean,
|
|
21
|
+
): SilentAuthorizationWindow {
|
|
22
|
+
const frame = interactive ? undefined : browser.document.createElement('iframe');
|
|
23
|
+
if (frame) {
|
|
24
|
+
frame.hidden = true;
|
|
25
|
+
frame.title = 'Authorization';
|
|
26
|
+
browser.document.body.append(frame);
|
|
27
|
+
}
|
|
28
|
+
const popup = interactive ? browser.open('about:blank', '_blank', POPUP_FEATURES) : null;
|
|
29
|
+
const target = frame?.contentWindow ?? popup;
|
|
30
|
+
if (!target) {
|
|
31
|
+
frame?.remove();
|
|
32
|
+
throw new SilentTokenError('popup_blocked');
|
|
33
|
+
}
|
|
34
|
+
let cleanup: () => void = () => undefined;
|
|
35
|
+
let cancel: () => void = () => undefined;
|
|
36
|
+
return {
|
|
37
|
+
get closed() {
|
|
38
|
+
return popup?.closed ?? false;
|
|
39
|
+
},
|
|
40
|
+
receive: (url, signal) =>
|
|
41
|
+
new Promise<string>((resolve, reject) => {
|
|
42
|
+
const finish = (value?: string) => {
|
|
43
|
+
cleanup();
|
|
44
|
+
if (value !== undefined) resolve(value);
|
|
45
|
+
else
|
|
46
|
+
reject(
|
|
47
|
+
signal.reason instanceof Error ? signal.reason : new SilentTokenError('aborted'),
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
const abort = () => {
|
|
51
|
+
finish();
|
|
52
|
+
};
|
|
53
|
+
const message = (event: MessageEvent<unknown>) => {
|
|
54
|
+
if (
|
|
55
|
+
event.origin !== browser.location.origin ||
|
|
56
|
+
event.source !== target ||
|
|
57
|
+
typeof event.data !== 'object' ||
|
|
58
|
+
event.data === null ||
|
|
59
|
+
!('type' in event.data) ||
|
|
60
|
+
event.data.type !== CALLBACK_MESSAGE ||
|
|
61
|
+
!('url' in event.data) ||
|
|
62
|
+
typeof event.data.url !== 'string'
|
|
63
|
+
)
|
|
64
|
+
return;
|
|
65
|
+
finish(event.data.url);
|
|
66
|
+
};
|
|
67
|
+
cleanup = () => {
|
|
68
|
+
browser.removeEventListener('message', message);
|
|
69
|
+
signal.removeEventListener('abort', abort);
|
|
70
|
+
};
|
|
71
|
+
cancel = abort;
|
|
72
|
+
browser.addEventListener('message', message);
|
|
73
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
74
|
+
if (signal.aborted) {
|
|
75
|
+
abort();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
target.location.replace(url.toString());
|
|
80
|
+
} catch (cause) {
|
|
81
|
+
cleanup();
|
|
82
|
+
reject(cause instanceof Error ? cause : new SilentTokenError('aborted'));
|
|
83
|
+
}
|
|
84
|
+
}),
|
|
85
|
+
close: () => {
|
|
86
|
+
cancel();
|
|
87
|
+
cleanup();
|
|
88
|
+
frame?.remove();
|
|
89
|
+
popup?.close();
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type SilentTokenErrorCode =
|
|
2
|
+
| 'invalid_product'
|
|
3
|
+
| 'invalid_account_binding'
|
|
4
|
+
| 'invalid_url'
|
|
5
|
+
| 'unsupported_environment'
|
|
6
|
+
| 'unknown_product_or_audience'
|
|
7
|
+
| 'disposed'
|
|
8
|
+
| 'popup_blocked'
|
|
9
|
+
| 'aborted'
|
|
10
|
+
| 'invalid_callback'
|
|
11
|
+
| 'invalid_state'
|
|
12
|
+
| 'invalid_issuer'
|
|
13
|
+
| 'authorization_rejected'
|
|
14
|
+
| 'missing_code'
|
|
15
|
+
| 'invalid_tokens'
|
|
16
|
+
| 'invalid_nonce'
|
|
17
|
+
| 'invalid_client'
|
|
18
|
+
| 'invalid_metadata'
|
|
19
|
+
| 'http_error'
|
|
20
|
+
| 'invalid_response'
|
|
21
|
+
| 'unavailable_or_invalid_response';
|
|
22
|
+
|
|
23
|
+
/** Contains only library-owned codes, never provider content or tokens. */
|
|
24
|
+
export class SilentTokenError extends Error {
|
|
25
|
+
public constructor(public readonly code: SilentTokenErrorCode) {
|
|
26
|
+
super(`Silent authorization failed: ${code}.`);
|
|
27
|
+
this.name = 'SilentTokenError';
|
|
28
|
+
}
|
|
29
|
+
}
|