@baukit/api-runtime 0.7.2 → 0.7.4
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/package.json +2 -1
- package/src/backoff.test.ts +32 -0
- package/src/backoff.ts +22 -0
- package/src/erasure.test.ts +333 -0
- package/src/erasure.ts +229 -0
- package/src/idempotency.test.ts +423 -0
- package/src/idempotency.ts +258 -0
- package/src/index.test.ts +453 -0
- package/src/index.ts +727 -0
- package/src/unverified-display-identity.test.ts +72 -0
- package/src/unverified-display-identity.ts +96 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { unverifiedDisplayIdentityHintsFromJwt } from './index.js';
|
|
4
|
+
|
|
5
|
+
const fallback = { displayName: 'Local account', initials: 'LA' } as const;
|
|
6
|
+
|
|
7
|
+
function unsignedJwt(payload: unknown): string {
|
|
8
|
+
const json = JSON.stringify(payload);
|
|
9
|
+
const bytes = new TextEncoder().encode(json);
|
|
10
|
+
const binary = Array.from(bytes, (byte) => String.fromCharCode(byte)).join('');
|
|
11
|
+
return `header.${btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '')}.signature`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('unverified display identity hints', () => {
|
|
15
|
+
it('prefers name and derives at most two Unicode-aware initials', () => {
|
|
16
|
+
expect(
|
|
17
|
+
unverifiedDisplayIdentityHintsFromJwt(
|
|
18
|
+
unsignedJwt({ name: ' Émilie du Châtelet ', preferred_username: 'emilie' }),
|
|
19
|
+
fallback,
|
|
20
|
+
),
|
|
21
|
+
).toEqual({ displayName: 'Émilie du Châtelet', initials: 'ÉD' });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('combines given and family names before username and email', () => {
|
|
25
|
+
expect(
|
|
26
|
+
unverifiedDisplayIdentityHintsFromJwt(
|
|
27
|
+
unsignedJwt({
|
|
28
|
+
given_name: 'Ada',
|
|
29
|
+
family_name: 'Lovelace',
|
|
30
|
+
preferred_username: 'ada',
|
|
31
|
+
email: 'ada@example.test',
|
|
32
|
+
}),
|
|
33
|
+
fallback,
|
|
34
|
+
),
|
|
35
|
+
).toEqual({ displayName: 'Ada Lovelace', initials: 'AL' });
|
|
36
|
+
expect(
|
|
37
|
+
unverifiedDisplayIdentityHintsFromJwt(unsignedJwt({ given_name: 'Ada' }), fallback),
|
|
38
|
+
).toEqual({ displayName: 'Ada', initials: 'A' });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('uses username, then email, when name claims are absent', () => {
|
|
42
|
+
expect(
|
|
43
|
+
unverifiedDisplayIdentityHintsFromJwt(
|
|
44
|
+
unsignedJwt({ preferred_username: 'ada', email: 'other@example.test' }),
|
|
45
|
+
fallback,
|
|
46
|
+
),
|
|
47
|
+
).toEqual({ displayName: 'ada', initials: 'A' });
|
|
48
|
+
expect(
|
|
49
|
+
unverifiedDisplayIdentityHintsFromJwt(unsignedJwt({ email: 'ada@example.test' }), fallback),
|
|
50
|
+
).toEqual({ displayName: 'ada@example.test', initials: 'A' });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it.each([
|
|
54
|
+
'not-a-jwt',
|
|
55
|
+
'header.%%%%.signature',
|
|
56
|
+
'header.e2JhZA.signature',
|
|
57
|
+
unsignedJwt([]),
|
|
58
|
+
unsignedJwt({ name: ' ', preferred_username: 17 }),
|
|
59
|
+
`header.${'a'.repeat(16_385)}.signature`,
|
|
60
|
+
])('returns product fallback text for malformed or unusable input', (token) => {
|
|
61
|
+
expect(unverifiedDisplayIdentityHintsFromJwt(token, fallback)).toEqual(fallback);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('rejects empty product fallback text even when claims are present', () => {
|
|
65
|
+
expect(() =>
|
|
66
|
+
unverifiedDisplayIdentityHintsFromJwt(unsignedJwt({ name: 'Ada' }), {
|
|
67
|
+
displayName: ' ',
|
|
68
|
+
initials: 'L',
|
|
69
|
+
}),
|
|
70
|
+
).toThrow(RangeError);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const MAX_JWT_PAYLOAD_CHARACTERS = 16_384;
|
|
2
|
+
|
|
3
|
+
/** Unverified JWT text suitable only for display. */
|
|
4
|
+
export interface UnverifiedDisplayIdentityHints {
|
|
5
|
+
readonly displayName: string;
|
|
6
|
+
readonly initials: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Product copy used when a JWT has no usable display claims. */
|
|
10
|
+
export interface UnverifiedDisplayIdentityFallback {
|
|
11
|
+
readonly displayName: string;
|
|
12
|
+
readonly initials: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Decodes unverified JWT claims and chooses display-only identity hints.
|
|
17
|
+
*
|
|
18
|
+
* Never use this result for authorization, storage partitioning, analytics
|
|
19
|
+
* identity, or any other security decision. Use a server-validated subject for
|
|
20
|
+
* those decisions.
|
|
21
|
+
*/
|
|
22
|
+
export function unverifiedDisplayIdentityHintsFromJwt(
|
|
23
|
+
token: string,
|
|
24
|
+
fallback: UnverifiedDisplayIdentityFallback,
|
|
25
|
+
): UnverifiedDisplayIdentityHints {
|
|
26
|
+
const safeFallback = requiredFallback(fallback);
|
|
27
|
+
const payload = decodeUnverifiedJwtPayload(token);
|
|
28
|
+
if (payload === undefined) return safeFallback;
|
|
29
|
+
|
|
30
|
+
const givenName = textClaim(payload, 'given_name');
|
|
31
|
+
const familyName = textClaim(payload, 'family_name');
|
|
32
|
+
const combinedName = [givenName, familyName].filter((value) => value !== undefined).join(' ');
|
|
33
|
+
const displayName =
|
|
34
|
+
textClaim(payload, 'name') ??
|
|
35
|
+
(combinedName === '' ? undefined : combinedName) ??
|
|
36
|
+
textClaim(payload, 'preferred_username') ??
|
|
37
|
+
textClaim(payload, 'email');
|
|
38
|
+
if (displayName === undefined) return safeFallback;
|
|
39
|
+
|
|
40
|
+
const initials = displayName
|
|
41
|
+
.split(/\s+/u)
|
|
42
|
+
.filter((part) => part !== '')
|
|
43
|
+
.slice(0, 2)
|
|
44
|
+
.map((part) => Array.from(part)[0]?.toUpperCase() ?? '')
|
|
45
|
+
.join('');
|
|
46
|
+
|
|
47
|
+
return { displayName, initials: initials === '' ? safeFallback.initials : initials };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function requiredFallback(
|
|
51
|
+
fallback: UnverifiedDisplayIdentityFallback,
|
|
52
|
+
): UnverifiedDisplayIdentityFallback {
|
|
53
|
+
const displayName = fallback.displayName.trim();
|
|
54
|
+
const initials = fallback.initials.trim();
|
|
55
|
+
if (displayName === '' || initials === '') {
|
|
56
|
+
throw new RangeError('Display identity fallback text must not be empty.');
|
|
57
|
+
}
|
|
58
|
+
return { displayName, initials };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function decodeUnverifiedJwtPayload(token: string): Readonly<Record<string, unknown>> | undefined {
|
|
62
|
+
const segments = token.split('.');
|
|
63
|
+
const encoded = segments.length === 3 ? segments[1] : undefined;
|
|
64
|
+
if (
|
|
65
|
+
encoded === undefined ||
|
|
66
|
+
encoded === '' ||
|
|
67
|
+
encoded.length > MAX_JWT_PAYLOAD_CHARACTERS ||
|
|
68
|
+
!/^[A-Za-z0-9_-]+$/u.test(encoded)
|
|
69
|
+
) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const normalized = encoded.replaceAll('-', '+').replaceAll('_', '/');
|
|
75
|
+
const binary = atob(`${normalized}${'='.repeat((4 - (normalized.length % 4)) % 4)}`);
|
|
76
|
+
const escapedBytes = Array.from(
|
|
77
|
+
binary,
|
|
78
|
+
(character) => `%${character.charCodeAt(0).toString(16).padStart(2, '0')}`,
|
|
79
|
+
).join('');
|
|
80
|
+
const value: unknown = JSON.parse(decodeURIComponent(escapedBytes));
|
|
81
|
+
return isPlainRecord(value) ? value : undefined;
|
|
82
|
+
} catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isPlainRecord(value: unknown): value is Readonly<Record<string, unknown>> {
|
|
88
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return false;
|
|
89
|
+
const prototype = Object.getPrototypeOf(value) as unknown;
|
|
90
|
+
return prototype === Object.prototype || prototype === null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function textClaim(payload: Readonly<Record<string, unknown>>, claim: string): string | undefined {
|
|
94
|
+
const value = payload[claim];
|
|
95
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;
|
|
96
|
+
}
|