@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
package/src/index.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
base64UrlEncode,
|
|
3
|
+
buildAuthorizationUrl,
|
|
4
|
+
normalizeIssuer,
|
|
5
|
+
normalizeScopes,
|
|
6
|
+
randomUrlSafe,
|
|
7
|
+
requiredText,
|
|
8
|
+
requiredUrl,
|
|
9
|
+
} from './oidc-helpers.js';
|
|
10
|
+
|
|
11
|
+
export { buildAuthorizationUrl, normalizeIssuer } from './oidc-helpers.js';
|
|
12
|
+
export * from './silent-token-broker.js';
|
|
13
|
+
export { CALLBACK_MESSAGE, completeSilentTokenCallback } from './silent-token-browser.js';
|
|
14
|
+
|
|
1
15
|
export type OidcErrorCode =
|
|
2
16
|
| 'authorization_failed'
|
|
3
17
|
| 'callback_state_mismatch'
|
|
@@ -131,47 +145,6 @@ export interface PkceAuthorizationRequest {
|
|
|
131
145
|
readonly challenge: string;
|
|
132
146
|
}
|
|
133
147
|
|
|
134
|
-
/** Removes trailing issuer slashes while rejecting query strings and fragments. */
|
|
135
|
-
export function normalizeIssuer(issuer: string): string {
|
|
136
|
-
const trimmed = issuer.trim();
|
|
137
|
-
if (trimmed.length === 0) {
|
|
138
|
-
throw new TypeError('OIDC issuer must not be empty.');
|
|
139
|
-
}
|
|
140
|
-
const url = new URL(trimmed);
|
|
141
|
-
if (url.search.length !== 0 || url.hash.length !== 0) {
|
|
142
|
-
throw new TypeError('OIDC issuer must not include a query string or fragment.');
|
|
143
|
-
}
|
|
144
|
-
return url.toString().replace(/\/+$/u, '');
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Builds an authorization request from the endpoint supplied by discovery. */
|
|
148
|
-
export function buildAuthorizationUrl(
|
|
149
|
-
config: Pick<
|
|
150
|
-
OidcClientConfig,
|
|
151
|
-
'clientId' | 'redirectUri' | 'scopes' | 'offlineAccess' | 'audience' | 'resource'
|
|
152
|
-
>,
|
|
153
|
-
authorizationEndpoint: string,
|
|
154
|
-
pkce: PkceAuthorizationRequest,
|
|
155
|
-
prompt?: 'login',
|
|
156
|
-
): URL {
|
|
157
|
-
const endpoint = new URL(authorizationEndpoint);
|
|
158
|
-
endpoint.search = new URLSearchParams({
|
|
159
|
-
client_id: requiredText(config.clientId, 'OIDC client ID'),
|
|
160
|
-
redirect_uri: requiredUrl(config.redirectUri, 'OIDC redirect URI'),
|
|
161
|
-
response_type: 'code',
|
|
162
|
-
scope: normalizeScopes(config.scopes, config.offlineAccess).join(' '),
|
|
163
|
-
state: pkce.state,
|
|
164
|
-
code_challenge: pkce.challenge,
|
|
165
|
-
code_challenge_method: 'S256',
|
|
166
|
-
}).toString();
|
|
167
|
-
if (config.audience !== undefined)
|
|
168
|
-
endpoint.searchParams.set('audience', requiredText(config.audience, 'OIDC audience'));
|
|
169
|
-
if (config.resource !== undefined)
|
|
170
|
-
endpoint.searchParams.set('resource', requiredText(config.resource, 'OIDC resource'));
|
|
171
|
-
if (prompt !== undefined) endpoint.searchParams.set('prompt', prompt);
|
|
172
|
-
return endpoint;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
148
|
/** Returns an allowlisted message without reflecting untrusted error content. */
|
|
176
149
|
export function safeAuthErrorMessage(cause: unknown): string {
|
|
177
150
|
return cause instanceof OidcError ? SAFE_ERROR_MESSAGES[cause.code] : 'OIDC login failed.';
|
|
@@ -590,56 +563,12 @@ function normalizeConfig(config: OidcClientConfig): NormalizedConfig {
|
|
|
590
563
|
};
|
|
591
564
|
}
|
|
592
565
|
|
|
593
|
-
function normalizeScopes(scopes: readonly string[] | undefined, offlineAccess = false): string[] {
|
|
594
|
-
const requested = scopes ?? ['openid', 'profile', 'email'];
|
|
595
|
-
const normalized: string[] = [];
|
|
596
|
-
for (const scope of ['openid', ...requested, ...(offlineAccess ? ['offline_access'] : [])]) {
|
|
597
|
-
const trimmed = scope.trim();
|
|
598
|
-
if (trimmed.length === 0 || /\s/u.test(trimmed)) {
|
|
599
|
-
throw new TypeError('OIDC scopes must be non-empty and contain no whitespace.');
|
|
600
|
-
}
|
|
601
|
-
if (!normalized.includes(trimmed)) {
|
|
602
|
-
normalized.push(trimmed);
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
return normalized;
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
function requiredText(value: string, label: string): string {
|
|
609
|
-
const trimmed = value.trim();
|
|
610
|
-
if (trimmed.length === 0) {
|
|
611
|
-
throw new TypeError(`${label} must not be empty.`);
|
|
612
|
-
}
|
|
613
|
-
return trimmed;
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
function requiredUrl(value: string, label: string): string {
|
|
617
|
-
const trimmed = requiredText(value, label);
|
|
618
|
-
try {
|
|
619
|
-
return new URL(trimmed).toString();
|
|
620
|
-
} catch {
|
|
621
|
-
throw new TypeError(`${label} must be an absolute URL.`);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
|
|
625
566
|
function hasCallbackParameters(url: URL): boolean {
|
|
626
567
|
return ['code', 'state', 'error', 'iss', 'session_state'].some((name) =>
|
|
627
568
|
url.searchParams.has(name),
|
|
628
569
|
);
|
|
629
570
|
}
|
|
630
571
|
|
|
631
|
-
function randomUrlSafe(cryptoImplementation: Crypto, byteLength: number): string {
|
|
632
|
-
return base64UrlEncode(cryptoImplementation.getRandomValues(new Uint8Array(byteLength)));
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
function base64UrlEncode(bytes: Uint8Array): string {
|
|
636
|
-
let binary = '';
|
|
637
|
-
for (const byte of bytes) {
|
|
638
|
-
binary += String.fromCharCode(byte);
|
|
639
|
-
}
|
|
640
|
-
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '');
|
|
641
|
-
}
|
|
642
|
-
|
|
643
572
|
function readTransaction(raw: string | null): LoginTransaction | undefined {
|
|
644
573
|
if (raw === null) {
|
|
645
574
|
return undefined;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { OidcClientConfig, PkceAuthorizationRequest } from './index.js';
|
|
2
|
+
|
|
3
|
+
/** Removes trailing issuer slashes while rejecting query strings and fragments. */
|
|
4
|
+
export function normalizeIssuer(issuer: string): string {
|
|
5
|
+
const trimmed = issuer.trim();
|
|
6
|
+
if (trimmed.length === 0) {
|
|
7
|
+
throw new TypeError('OIDC issuer must not be empty.');
|
|
8
|
+
}
|
|
9
|
+
const url = new URL(trimmed);
|
|
10
|
+
if (url.search.length !== 0 || url.hash.length !== 0) {
|
|
11
|
+
throw new TypeError('OIDC issuer must not include a query string or fragment.');
|
|
12
|
+
}
|
|
13
|
+
return url.toString().replace(/\/+$/u, '');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Builds an authorization request from the endpoint supplied by discovery. */
|
|
17
|
+
export function buildAuthorizationUrl(
|
|
18
|
+
config: Pick<
|
|
19
|
+
OidcClientConfig,
|
|
20
|
+
'clientId' | 'redirectUri' | 'scopes' | 'offlineAccess' | 'audience' | 'resource'
|
|
21
|
+
>,
|
|
22
|
+
authorizationEndpoint: string,
|
|
23
|
+
pkce: PkceAuthorizationRequest,
|
|
24
|
+
prompt?: 'login',
|
|
25
|
+
): URL {
|
|
26
|
+
const endpoint = new URL(authorizationEndpoint);
|
|
27
|
+
endpoint.search = new URLSearchParams({
|
|
28
|
+
client_id: requiredText(config.clientId, 'OIDC client ID'),
|
|
29
|
+
redirect_uri: requiredUrl(config.redirectUri, 'OIDC redirect URI'),
|
|
30
|
+
response_type: 'code',
|
|
31
|
+
scope: normalizeScopes(config.scopes, config.offlineAccess).join(' '),
|
|
32
|
+
state: pkce.state,
|
|
33
|
+
code_challenge: pkce.challenge,
|
|
34
|
+
code_challenge_method: 'S256',
|
|
35
|
+
}).toString();
|
|
36
|
+
if (config.audience !== undefined)
|
|
37
|
+
endpoint.searchParams.set('audience', requiredText(config.audience, 'OIDC audience'));
|
|
38
|
+
if (config.resource !== undefined)
|
|
39
|
+
endpoint.searchParams.set('resource', requiredText(config.resource, 'OIDC resource'));
|
|
40
|
+
if (prompt !== undefined) endpoint.searchParams.set('prompt', prompt);
|
|
41
|
+
return endpoint;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function normalizeScopes(
|
|
45
|
+
scopes: readonly string[] | undefined,
|
|
46
|
+
offlineAccess = false,
|
|
47
|
+
): string[] {
|
|
48
|
+
const requested = scopes ?? ['openid', 'profile', 'email'];
|
|
49
|
+
const normalized: string[] = [];
|
|
50
|
+
for (const scope of ['openid', ...requested, ...(offlineAccess ? ['offline_access'] : [])]) {
|
|
51
|
+
const trimmed = scope.trim();
|
|
52
|
+
if (trimmed.length === 0 || /\s/u.test(trimmed)) {
|
|
53
|
+
throw new TypeError('OIDC scopes must be non-empty and contain no whitespace.');
|
|
54
|
+
}
|
|
55
|
+
if (!normalized.includes(trimmed)) {
|
|
56
|
+
normalized.push(trimmed);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return normalized;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function requiredText(value: string, label: string): string {
|
|
63
|
+
const trimmed = value.trim();
|
|
64
|
+
if (trimmed.length === 0) {
|
|
65
|
+
throw new TypeError(`${label} must not be empty.`);
|
|
66
|
+
}
|
|
67
|
+
return trimmed;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function requiredUrl(value: string, label: string): string {
|
|
71
|
+
const trimmed = requiredText(value, label);
|
|
72
|
+
try {
|
|
73
|
+
return new URL(trimmed).toString();
|
|
74
|
+
} catch {
|
|
75
|
+
throw new TypeError(`${label} must be an absolute URL.`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function randomUrlSafe(cryptoImplementation: Crypto, byteLength: number): string {
|
|
80
|
+
return base64UrlEncode(cryptoImplementation.getRandomValues(new Uint8Array(byteLength)));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function base64UrlEncode(bytes: Uint8Array): string {
|
|
84
|
+
let binary = '';
|
|
85
|
+
for (const byte of bytes) {
|
|
86
|
+
binary += String.fromCharCode(byte);
|
|
87
|
+
}
|
|
88
|
+
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '');
|
|
89
|
+
}
|