@web-ts-toolkit/oidc-vault-dpop-client 0.48.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/LICENSE +201 -0
- package/README.md +398 -0
- package/index.d.mts +269 -0
- package/index.d.ts +269 -0
- package/index.js +875 -0
- package/index.mjs +842 -0
- package/llms.txt +45 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright Junmin Ahn
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
# `@web-ts-toolkit/oidc-vault-dpop-client`
|
|
2
|
+
|
|
3
|
+
Browser-only DPoP client for [`@web-ts-toolkit/express-oidc-vault`](../express-oidc-vault/README.md)
|
|
4
|
+
vault flows, with `body` and `cookie` session transports. Framework-agnostic
|
|
5
|
+
vanilla TypeScript: persistent non-extractable P-256 keys, per-attempt proofs,
|
|
6
|
+
bounded nonce/refresh retries, and tab coordination for cookie sessions.
|
|
7
|
+
|
|
8
|
+
The package ships at an `es2022` bundle target for modern evergreen browsers
|
|
9
|
+
(Chrome 94+, Edge 94+, Firefox 93+, Safari 16+, declared via `browserslist`).
|
|
10
|
+
It touches browser globals (`crypto.subtle`, `indexedDB`, `sessionStorage`,
|
|
11
|
+
and — cookie transport only — `navigator.locks` plus `BroadcastChannel`) lazily
|
|
12
|
+
behind runtime feature asserts, never at module import time. There is no
|
|
13
|
+
`express`, no `node:*`, and no default export.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pnpm add @web-ts-toolkit/oidc-vault-dpop-client
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`jose` and `idb` are runtime `dependencies` of this package, not peers: the
|
|
22
|
+
single install above is enough. Do not install them separately unless your own
|
|
23
|
+
code imports them directly.
|
|
24
|
+
|
|
25
|
+
## Canonical imports
|
|
26
|
+
|
|
27
|
+
Named root imports only; no default export, no deep `dist/*` or `src/*` imports.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import {
|
|
31
|
+
createDeviceFingerprint,
|
|
32
|
+
createDpopProof,
|
|
33
|
+
createOidcVaultDpopSession,
|
|
34
|
+
fetchWithDpop,
|
|
35
|
+
fingerprintJsSignalSource,
|
|
36
|
+
getOrCreateDpopKey,
|
|
37
|
+
OidcVaultDpopClientError,
|
|
38
|
+
} from '@web-ts-toolkit/oidc-vault-dpop-client';
|
|
39
|
+
import type {
|
|
40
|
+
DpopAccessToken,
|
|
41
|
+
DpopApi,
|
|
42
|
+
DpopFetchContext,
|
|
43
|
+
DpopFetchOptions,
|
|
44
|
+
DpopKey,
|
|
45
|
+
OidcVaultDpopSession,
|
|
46
|
+
} from '@web-ts-toolkit/oidc-vault-dpop-client';
|
|
47
|
+
|
|
48
|
+
export const sessionApi = { createOidcVaultDpopSession, fetchWithDpop };
|
|
49
|
+
export type { DpopAccessToken, DpopKey, OidcVaultDpopSession };
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`DpopNonceCache` and the `DeviceFingerprint` family are also exported from the
|
|
53
|
+
root for typed consumption. Non-public coordination helpers
|
|
54
|
+
(`withDpopDatabase`, `resolveDpopScope`, cookie-version bookkeeping) stay
|
|
55
|
+
internal on purpose: scope to one `backendOrigin` + `basePath` per session and
|
|
56
|
+
let the session own key/proof/nonce coordination.
|
|
57
|
+
|
|
58
|
+
## Quickstart: body transport
|
|
59
|
+
|
|
60
|
+
One vault mount, one session. `basePath` must equal the backend's exact public
|
|
61
|
+
mount (the core default is `/auth/oidc`; the example app mounts
|
|
62
|
+
`/auth/oidc/body` and `/auth/oidc/cookie` side by side — pick the one you call).
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { createOidcVaultDpopSession, fetchWithDpop } from '@web-ts-toolkit/oidc-vault-dpop-client';
|
|
66
|
+
|
|
67
|
+
const backendOrigin = 'https://api.example.com';
|
|
68
|
+
const session = createOidcVaultDpopSession({
|
|
69
|
+
backendOrigin,
|
|
70
|
+
basePath: '/auth/oidc/body',
|
|
71
|
+
sessionTransport: 'body',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// A login button calls this; login creates/persists the key BEFORE POST and navigation.
|
|
75
|
+
export async function signIn(): Promise<void> {
|
|
76
|
+
await session.login('/callback?transport=body');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// On /callback or application bootstrap:
|
|
80
|
+
export async function bootstrap(): Promise<void> {
|
|
81
|
+
const url = new URL(location.href);
|
|
82
|
+
const code = url.searchParams.get('code');
|
|
83
|
+
if (code !== null) {
|
|
84
|
+
url.searchParams.delete('code');
|
|
85
|
+
history.replaceState(null, '', url.href);
|
|
86
|
+
await session.exchange(code);
|
|
87
|
+
} else {
|
|
88
|
+
await session.refresh(); // Existing persistent key only; no Authorization/ath.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function getProfile(): Promise<unknown> {
|
|
93
|
+
const response = await fetchWithDpop(
|
|
94
|
+
{ session, apis: [{ origin: backendOrigin, replayNamespace: 'my-app-api' }] },
|
|
95
|
+
`${backendOrigin}/api/profile`,
|
|
96
|
+
);
|
|
97
|
+
if (!response.ok) throw new Error('Protected API request failed.');
|
|
98
|
+
return response.json();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function signOut(): Promise<void> {
|
|
102
|
+
await session.logout();
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Lifecycle is `login → exchange(code) → getAccessToken()/fetchWithDpop →
|
|
107
|
+
refresh → logout`. Vault POSTs carry no `Authorization` and no `ath`, so local
|
|
108
|
+
JWT expiry never blocks `refresh`. All vault and API credential responses are
|
|
109
|
+
`Cache-Control: no-store`; body handles are tab-local (see
|
|
110
|
+
[Storage](#storage)).
|
|
111
|
+
|
|
112
|
+
## Quickstart: cookie transport
|
|
113
|
+
|
|
114
|
+
Same session shape, `sessionTransport: 'cookie'`, and the matching cookie
|
|
115
|
+
mount. Cookie mode additionally requires **Web Locks** and
|
|
116
|
+
**BroadcastChannel** at runtime; unsupported browsers fail explicitly via
|
|
117
|
+
`DPOP_TAB_COORDINATION_UNSUPPORTED`.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { createOidcVaultDpopSession, fetchWithDpop } from '@web-ts-toolkit/oidc-vault-dpop-client';
|
|
121
|
+
|
|
122
|
+
const backendOrigin = 'https://api.example.com';
|
|
123
|
+
const session = createOidcVaultDpopSession({
|
|
124
|
+
backendOrigin,
|
|
125
|
+
basePath: '/auth/oidc/cookie',
|
|
126
|
+
sessionTransport: 'cookie',
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
export async function signIn(): Promise<void> {
|
|
130
|
+
await session.login('/callback?transport=cookie');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function bootstrap(): Promise<void> {
|
|
134
|
+
const url = new URL(location.href);
|
|
135
|
+
const code = url.searchParams.get('code');
|
|
136
|
+
if (code !== null) {
|
|
137
|
+
url.searchParams.delete('code');
|
|
138
|
+
history.replaceState(null, '', url.href);
|
|
139
|
+
await session.exchange(code);
|
|
140
|
+
} else {
|
|
141
|
+
await session.refresh(); // Single-flight; winner token shared over BroadcastChannel.
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function getProfile(): Promise<unknown> {
|
|
146
|
+
const response = await fetchWithDpop(
|
|
147
|
+
{ session, apis: [{ origin: backendOrigin, replayNamespace: 'my-app-api' }] },
|
|
148
|
+
`${backendOrigin}/api/profile`,
|
|
149
|
+
);
|
|
150
|
+
if (!response.ok) throw new Error('Protected API request failed.');
|
|
151
|
+
return response.json();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export async function saveResource(value: string): Promise<unknown> {
|
|
155
|
+
// Mutations are single-attempt unless YOUR operation owns a server-enforced
|
|
156
|
+
// idempotency contract (see Retry policy).
|
|
157
|
+
const response = await fetchWithDpop(
|
|
158
|
+
{ session, apis: [{ origin: backendOrigin, replayNamespace: 'my-app-api' }] },
|
|
159
|
+
`${backendOrigin}/api/resource`,
|
|
160
|
+
{ method: 'PUT', headers: { 'Content-Type': 'text/plain' }, body: value, retry: 'idempotent' },
|
|
161
|
+
);
|
|
162
|
+
if (!response.ok) throw new Error('Protected API request failed.');
|
|
163
|
+
return response.json();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export async function signOut(): Promise<void> {
|
|
167
|
+
await session.logout();
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Cookie refresh uses one same-origin Web Lock per vault scope plus
|
|
172
|
+
`BroadcastChannel` winner-token delivery: a tab adopts a live peer token for up
|
|
173
|
+
to 180ms, otherwise it performs its own serialized refresh with the current
|
|
174
|
+
browser cookie. Peer adoption still validates payload, key binding, expiry,
|
|
175
|
+
generation, and current recognition. Each API call and retry signs its own
|
|
176
|
+
fresh proof. Web Locks coordinate tabs of one frontend origin, storage
|
|
177
|
+
partition, and vault scope — they are not a backend refresh lease, so other
|
|
178
|
+
frontends, devices, or processes can still race upstream refresh families.
|
|
179
|
+
|
|
180
|
+
## Backend pairing
|
|
181
|
+
|
|
182
|
+
Configure the vault mount this client calls. `backendOrigin` must be the exact
|
|
183
|
+
public origin (scheme + host + port); `basePath` must be the exact public
|
|
184
|
+
mount path — trailing slashes are normalized, anything else must match
|
|
185
|
+
character-for-character, because the key scope, proof `htu`, and request URL
|
|
186
|
+
all derive from it. `use 127.0.0.1` consistently in loopback development:
|
|
187
|
+
`localhost` is a different origin and a different key scope.
|
|
188
|
+
|
|
189
|
+
Backend options that must agree with this client (authoritative wire contract:
|
|
190
|
+
[`express-oidc-vault` README](../express-oidc-vault/README.md)):
|
|
191
|
+
|
|
192
|
+
- `basePath`: the same mount (e.g. `/auth/oidc/body` with `sessionTransport:
|
|
193
|
+
'body'`, `/auth/oidc/cookie` with `'cookie'`).
|
|
194
|
+
- `trustedOrigins`: list every browser source origin allowed to POST
|
|
195
|
+
login/guarded exchange (both transports) and cookie-authenticated
|
|
196
|
+
refresh/logout. Required for cross-site session cookie transport.
|
|
197
|
+
- `transactionCookie.sameSite`: keep the default `lax` for same-site
|
|
198
|
+
deployments. HTTPS cross-site SPAs need explicit `transactionCookie:
|
|
199
|
+
{ sameSite: 'none' }`, plus matching session-cookie deployment policy,
|
|
200
|
+
credentialed CORS, and a browser policy that permits third-party cookies.
|
|
201
|
+
- `deviceBinding.mode: 'required'` for bound flows, with a shared nonce secret
|
|
202
|
+
when nonces are enabled (`nonce: { secret, lifetimeSeconds }`, ≥32 random
|
|
203
|
+
bytes shared by instances of that protection space).
|
|
204
|
+
- The API's `replayNamespace` must equal the `replayNamespace` configured in
|
|
205
|
+
`fetchWithDpop` `apis` entries.
|
|
206
|
+
|
|
207
|
+
### CORS
|
|
208
|
+
|
|
209
|
+
The backend must answer credentialed cross-origin browser calls:
|
|
210
|
+
|
|
211
|
+
| Setting | Value |
|
|
212
|
+
| --------------- | ------------------------------------------------------------------------------- |
|
|
213
|
+
| Origin | Echo the exact trusted frontend origin (never `*`) |
|
|
214
|
+
| `credentials` | `true` |
|
|
215
|
+
| Allowed headers | `Content-Type`, `Authorization`, `DPoP`, and the configured fingerprint header |
|
|
216
|
+
| Exposed headers | `DPoP-Nonce`, `WWW-Authenticate` (the client reads nonce challenges from these) |
|
|
217
|
+
|
|
218
|
+
Vault Origin/Referer checks run independently of CORS. Cookie-authenticated
|
|
219
|
+
`refresh`/`logout` require an `Origin` (or valid `Referer`) matching
|
|
220
|
+
`backendOrigin` or `trustedOrigins` in every `SameSite` mode.
|
|
221
|
+
|
|
222
|
+
## Nonce retry rule
|
|
223
|
+
|
|
224
|
+
When nonces are enabled, a missing/expired nonce returns fixed
|
|
225
|
+
`OIDC_VAULT_USE_DPOP_NONCE`: **HTTP 400 at vault POSTs**
|
|
226
|
+
(login/exchange/refresh/logout), **HTTP 401 with `DPoP error="use_dpop_nonce"`
|
|
227
|
+
at protected APIs**. Both helpers retry **once** with a fresh
|
|
228
|
+
proof/`jti`/`iat`/signature/`ath`, then stop: repeated challenges throw
|
|
229
|
+
(`DPOP_NONCE_RETRY_EXHAUSTED` on vault POSTs; the API response is returned to
|
|
230
|
+
the caller). Nonces are cached in memory per protection space and key; vault
|
|
231
|
+
and API spaces are separate. Only `DPoP error="invalid_token"` triggers an API
|
|
232
|
+
refresh (at most one refresh/retry cycle); generic 401s, proof errors, 403s,
|
|
233
|
+
503s, and network failures never do.
|
|
234
|
+
|
|
235
|
+
## Storage
|
|
236
|
+
|
|
237
|
+
| What | Where | Lifetime / scope |
|
|
238
|
+
| ------------------------------------------------------- | --------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
|
|
239
|
+
| Access JWT | Memory only (`getAccessToken()`), never persisted | Until `expiresAt`; closing every tab discards all tokens |
|
|
240
|
+
| Body handle `{ sessionId, jkt }` | `sessionStorage`, tab-local | Scoped key `oidc-vault-dpop:handle:<scope>`; do not copy across tabs |
|
|
241
|
+
| Pending-login `jkt` marker | `sessionStorage`, survives the IdP navigation | Cleared at `exchange`; missing marker means fresh login required |
|
|
242
|
+
| Cookie handle | Backend `HttpOnly` session cookie, never browser-readable | Backend session TTL (example: 8h absolute); no vault `sid` in the JWT payload |
|
|
243
|
+
| DPoP private `CryptoKey` (non-extractable) + public JWK | IndexedDB object stores | Scope `[frontendOrigin, backendOrigin, normalizedBasePath]`; atomic first-key-wins across tabs |
|
|
244
|
+
| Cookie coordination `{ jkt, generation, active }` | IndexedDB, non-credential metadata only | Per scope; BroadcastChannel delivery is transient (≤180ms peer wait) |
|
|
245
|
+
|
|
246
|
+
`sessionStorage` tab-local body handles must not be shared between tabs, and
|
|
247
|
+
opener-cloned `sessionStorage` is not a shared-refresh strategy. No tokens or
|
|
248
|
+
handles enter `localStorage` or IndexedDB. Crypto runs outside the readwrite
|
|
249
|
+
transaction; export of the private key always fails by construction.
|
|
250
|
+
|
|
251
|
+
## Key loss and errors
|
|
252
|
+
|
|
253
|
+
Key loss or key change clears frontend auth and requires a fresh login —
|
|
254
|
+
`DPOP_KEY_LOST` (`requiresLogin: true`). Only fresh `login()` may create a
|
|
255
|
+
missing key; exchange/refresh/API/logout read the existing key and compare its
|
|
256
|
+
binding (`cnf.jkt` decode check plus key-equality check). There is no Bearer
|
|
257
|
+
fallback and no ephemeral-key fallback. Storage/crypto failures stop the
|
|
258
|
+
operation; clear frontend auth and request fresh login.
|
|
259
|
+
|
|
260
|
+
`OidcVaultDpopClientError` carries a stable `code`, a fixed message (never
|
|
261
|
+
keys, credentials, fingerprints, or URLs), `requiresLogin`, and an optional
|
|
262
|
+
HTTP `status`. Treat `requiresLogin === true` as "route to sign-in"; enrolled
|
|
263
|
+
recognition changes surface as `OIDC_VAULT_FINGERPRINT_REAUTH_REQUIRED` (clear
|
|
264
|
+
auth, start fresh POST login, no automatic re-enrollment).
|
|
265
|
+
|
|
266
|
+
## Credentials matrix
|
|
267
|
+
|
|
268
|
+
`credentials` is owned by the helpers; callers must not set it.
|
|
269
|
+
|
|
270
|
+
| Operation | Body transport | Cookie transport |
|
|
271
|
+
| -------------------- | ---------------------------------------- | ------------------------------------------------------------------ |
|
|
272
|
+
| `login` / `exchange` | `include` (temporary transaction cookie) | `include` (temporary transaction cookie) |
|
|
273
|
+
| `refresh` / `logout` | `omit` + `{ sessionId }` JSON body | `include` + `{}` JSON body (no `sessionId`) |
|
|
274
|
+
| `fetchWithDpop` API | `omit` default | `omit` default (`include` only for an explicitly cookie-using API) |
|
|
275
|
+
|
|
276
|
+
Cookie JSON always omits `sessionId`. Every request uses `redirect: 'error'`
|
|
277
|
+
and `cache: 'no-store'`. Vault POSTs present no access token; API calls send
|
|
278
|
+
`Authorization: DPoP <JWT>` plus a fresh same-key proof with `ath`.
|
|
279
|
+
|
|
280
|
+
## Retry policy (`fetchWithDpop`)
|
|
281
|
+
|
|
282
|
+
Scoped `fetch` for exactly the configured API origins: it owns
|
|
283
|
+
`Authorization`/`DPoP` (caller-set auth headers throw
|
|
284
|
+
`DPOP_AUTH_HEADER_OVERRIDE`), mints a fresh proof per attempt, and accepts only
|
|
285
|
+
exact origin matches with a valid `replayNamespace`.
|
|
286
|
+
|
|
287
|
+
| Method | Default attempts | Opt-in |
|
|
288
|
+
| ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
|
|
289
|
+
| `GET` / `HEAD` / `OPTIONS` | Retry once on nonce challenge, refresh once on `invalid_token` | `retry: 'never'` disables retries |
|
|
290
|
+
| Mutations (`POST` / `PUT` / `DELETE` / …) | **Single attempt**; a nonce challenge on an unsafe mutation is returned to the caller without implicit resubmission | `retry: 'idempotent'` only when the authorized operation owns a server-enforced idempotency contract |
|
|
291
|
+
|
|
292
|
+
Retry-enabled bodies must be replayable: strings, `URLSearchParams`, `Blob`,
|
|
293
|
+
`FormData`, `ArrayBuffer`, and views are snapshotted; streams throw
|
|
294
|
+
`DPOP_BODY_NOT_REPLAYABLE` before a retry-enabled request. A custom
|
|
295
|
+
`DpopNonceCache` can be shared via `fetchWithDpop({ session, apis, nonces },
|
|
296
|
+
…)`. DPoP does not sign bodies/query strings and does not replace API
|
|
297
|
+
authorization.
|
|
298
|
+
|
|
299
|
+
## Optional recognition (opt-in)
|
|
300
|
+
|
|
301
|
+
Recognition is change detection, not proof of possession and not an API sender
|
|
302
|
+
constraint. The helper rereads the current signal on POST login/exchange/refresh
|
|
303
|
+
only, persists and caches nothing; `undefined` deliberately means unenrolled.
|
|
304
|
+
Pass it to session options only after your application's own collection and
|
|
305
|
+
disclosure choice, and scope its header to the configured vault origin.
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
import {
|
|
309
|
+
createDeviceFingerprint,
|
|
310
|
+
createOidcVaultDpopSession,
|
|
311
|
+
fingerprintJsSignalSource,
|
|
312
|
+
} from '@web-ts-toolkit/oidc-vault-dpop-client';
|
|
313
|
+
|
|
314
|
+
// Disclosed, low-entropy demo signal. Replace with YOUR current-signal source;
|
|
315
|
+
// undefined deliberately means unenrolled.
|
|
316
|
+
const fingerprint = createDeviceFingerprint(async () => undefined);
|
|
317
|
+
|
|
318
|
+
export function createSessionWithRecognition(backendOrigin: string) {
|
|
319
|
+
return createOidcVaultDpopSession({ backendOrigin, basePath: '/auth/oidc', fingerprint });
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// If YOUR frontend opts into installing @fingerprintjs/fingerprintjs:
|
|
323
|
+
// import FingerprintJS from '@fingerprintjs/fingerprintjs';
|
|
324
|
+
// const vendorSource = fingerprintJsSignalSource(() => FingerprintJS.load());
|
|
325
|
+
// const vendorFingerprint = createDeviceFingerprint(vendorSource);
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
The server enrolls only a supplied login signal and compares before
|
|
329
|
+
exchange/refresh mutation. Enrolled omission/mismatch gives fixed **403
|
|
330
|
+
`OIDC_VAULT_FINGERPRINT_REAUTH_REQUIRED`**: clear frontend auth and start a
|
|
331
|
+
fresh POST login. Logout and API calls send no signal. Hashing is not
|
|
332
|
+
anonymization; raw signals are never logged.
|
|
333
|
+
|
|
334
|
+
## Safari and cross-site limits
|
|
335
|
+
|
|
336
|
+
- Cookie transport needs Web Locks + `BroadcastChannel`; Safari 16+ declares
|
|
337
|
+
support, but third-party-cookie blocking still applies to cross-site HTTPS
|
|
338
|
+
deployments — verify your deployment's browser policy instead of assuming
|
|
339
|
+
portability. The recorded browser evidence is **Chromium 151 + Firefox 153**
|
|
340
|
+
on same-site loopback HTTP; **WebKit/Safari is uncertified** in the Linux
|
|
341
|
+
test environment.
|
|
342
|
+
- Same-site subdomains can use Lax cookies with credentialed cross-origin
|
|
343
|
+
requests; cross-origin does not itself mean cross-site. Cross-site SPAs need
|
|
344
|
+
HTTPS, `transactionCookie: { sameSite: 'none' }`, compatible session-cookie
|
|
345
|
+
settings, and `trustedOrigins` covering the frontend.
|
|
346
|
+
- Stateless JWTs remain valid until expiry unless the application adds
|
|
347
|
+
revocation; `logout` revokes vault refresh state. Non-extractability prevents
|
|
348
|
+
key export, not same-browser XSS signing.
|
|
349
|
+
|
|
350
|
+
## Framework examples (TypeScript)
|
|
351
|
+
|
|
352
|
+
Minimal runnable demos live in `apps/`; each runs its own backend + IdP
|
|
353
|
+
fixture on dedicated ports so all three can run simultaneously:
|
|
354
|
+
|
|
355
|
+
| Demo | App | SPA | Backend | IdP |
|
|
356
|
+
| ------------------------------ | -------------------------------------- | ----------------------- | ----------------------- | ----------------------- |
|
|
357
|
+
| React 19 | `apps/oidc-vault-dpop-react-example` | `http://127.0.0.1:4320` | `http://127.0.0.1:4330` | `http://127.0.0.1:4331` |
|
|
358
|
+
| Vue 3 (SFC, `vue-tsc`) | `apps/oidc-vault-dpop-vue-example` | `http://127.0.0.1:4321` | `http://127.0.0.1:4340` | `http://127.0.0.1:4341` |
|
|
359
|
+
| Angular 22 (standalone, `ngc`) | `apps/oidc-vault-dpop-angular-example` | `http://127.0.0.1:4322` | `http://127.0.0.1:4350` | `http://127.0.0.1:4351` |
|
|
360
|
+
|
|
361
|
+
```sh
|
|
362
|
+
pnpm --filter oidc-vault-dpop-react-example dev:server # + dev
|
|
363
|
+
pnpm --filter oidc-vault-dpop-vue-example dev:server # + dev
|
|
364
|
+
pnpm --filter oidc-vault-dpop-angular-example dev:server # + dev
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
Each `dev:server` reuses `apps/oidc-vault-dpop-example/server` with
|
|
368
|
+
`FRONTEND_ORIGIN`/`PORT`/`IDP_PORT`/`BACKEND_ORIGIN` overrides (explicit env
|
|
369
|
+
wins over `--env-file`). All demos use body transport, a lazy session
|
|
370
|
+
singleton, and no client router — the callback is the same page reading
|
|
371
|
+
`?code=` once on boot.
|
|
372
|
+
|
|
373
|
+
## API reference
|
|
374
|
+
|
|
375
|
+
- `createOidcVaultDpopSession(options)` → `OidcVaultDpopSession`
|
|
376
|
+
(`login`/`exchange`/`refresh`/`logout`/`getAccessToken`/`getKey`/`clear`/`dispose`).
|
|
377
|
+
Options: `backendOrigin`, `basePath` (default `/auth/oidc`),
|
|
378
|
+
`sessionTransport` (`'body'` default | `'cookie'`), optional `fingerprint`,
|
|
379
|
+
injectable `fetch`/`navigate`/`now` for tests.
|
|
380
|
+
- `fetchWithDpop({ session, apis, fetch?, nonces?, now? }, input, options?)` →
|
|
381
|
+
`Promise<Response>`. `apis: DpopApi[]` pins `{ origin, replayNamespace,
|
|
382
|
+
credentials? }`.
|
|
383
|
+
- `getOrCreateDpopKey(scope, { create? })`, `createDpopProof(key, input)`,
|
|
384
|
+
`DpopNonceCache`, `createDeviceFingerprint(source, { headerName? })`,
|
|
385
|
+
`fingerprintJsSignalSource(load)`, `OidcVaultDpopClientError`.
|
|
386
|
+
- Types: `DpopKey`, `DpopKeyScope`, `DpopProofInput`, `DpopAccessToken`,
|
|
387
|
+
`DpopFetchContext`, `DpopFetchOptions`, `DpopApi`,
|
|
388
|
+
`OidcVaultDpopSession(Options)`, `DeviceFingerprint(Options)`,
|
|
389
|
+
`DeviceFingerprintSignalSource`, `FingerprintJsAgent`. Wire DTOs stay
|
|
390
|
+
single-sourced from `@web-ts-toolkit/express-oidc-vault` as types.
|
|
391
|
+
|
|
392
|
+
Worked end-to-end example (Vite SPA + Express vault/API + local OIDC
|
|
393
|
+
provider, both transports, real-browser evidence):
|
|
394
|
+
[`apps/oidc-vault-dpop-example`](../../apps/oidc-vault-dpop-example/README.md).
|
|
395
|
+
Migrating from the pre-publish copy: replace `copy src/auth/` + `pnpm add
|
|
396
|
+
jose idb` with `pnpm add @web-ts-toolkit/oidc-vault-dpop-client`, rewrite
|
|
397
|
+
`from './auth…'` imports to the package root, and keep `basePath` on the exact
|
|
398
|
+
`/body` vs `/cookie` mount you call.
|