@smartcrab/browser 0.1.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 +202 -0
- package/dist/base64url.d.ts +12 -0
- package/dist/base64url.js +44 -0
- package/dist/client.d.ts +192 -0
- package/dist/client.js +551 -0
- package/dist/errors.d.ts +95 -0
- package/dist/errors.js +44 -0
- package/dist/http.d.ts +49 -0
- package/dist/http.js +113 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +26 -0
- package/dist/pkce.d.ts +25 -0
- package/dist/pkce.js +21 -0
- package/dist/webauthn.d.ts +26 -0
- package/dist/webauthn.js +214 -0
- package/package.json +41 -0
- package/src/base64url.ts +47 -0
- package/src/client-me.test.ts +321 -0
- package/src/client-tokens.test.ts +378 -0
- package/src/client.test.ts +714 -0
- package/src/client.ts +1117 -0
- package/src/errors.ts +154 -0
- package/src/http.test.ts +225 -0
- package/src/http.ts +173 -0
- package/src/index.ts +68 -0
- package/src/pkce.test.ts +51 -0
- package/src/pkce.ts +43 -0
- package/src/webauthn.ts +295 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local base64url (RFC 4648 §5, no padding) helpers.
|
|
3
|
+
*
|
|
4
|
+
* Duplicated intentionally from `@smartcrab/identifiers` rather than
|
|
5
|
+
* imported: that package is not a dependency of this SDK (only
|
|
6
|
+
* `@smartcrab/contracts-public` is), and adding it just for ~40 lines of
|
|
7
|
+
* alphabet math would widen the published dependency surface for no
|
|
8
|
+
* behavioral gain. Implementations are byte-for-byte compatible.
|
|
9
|
+
*/
|
|
10
|
+
export declare const base64UrlEncodeBytes: (bytes: Uint8Array) => string;
|
|
11
|
+
export declare const base64UrlDecodeToBytes: (encoded: string) => Uint8Array;
|
|
12
|
+
export declare const bufferToBase64Url: (buffer: ArrayBuffer) => string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local base64url (RFC 4648 §5, no padding) helpers.
|
|
3
|
+
*
|
|
4
|
+
* Duplicated intentionally from `@smartcrab/identifiers` rather than
|
|
5
|
+
* imported: that package is not a dependency of this SDK (only
|
|
6
|
+
* `@smartcrab/contracts-public` is), and adding it just for ~40 lines of
|
|
7
|
+
* alphabet math would widen the published dependency surface for no
|
|
8
|
+
* behavioral gain. Implementations are byte-for-byte compatible.
|
|
9
|
+
*/
|
|
10
|
+
const BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
11
|
+
export const base64UrlEncodeBytes = (bytes) => {
|
|
12
|
+
let out = "";
|
|
13
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
14
|
+
const b0 = bytes[i] ?? 0;
|
|
15
|
+
const b1 = bytes[i + 1];
|
|
16
|
+
const b2 = bytes[i + 2];
|
|
17
|
+
out += BASE64URL_ALPHABET[b0 >> 2] ?? "";
|
|
18
|
+
out += BASE64URL_ALPHABET[((b0 & 0x03) << 4) | ((b1 ?? 0) >> 4)] ?? "";
|
|
19
|
+
if (b1 !== undefined)
|
|
20
|
+
out += BASE64URL_ALPHABET[((b1 & 0x0f) << 2) | ((b2 ?? 0) >> 6)] ?? "";
|
|
21
|
+
if (b2 !== undefined)
|
|
22
|
+
out += BASE64URL_ALPHABET[b2 & 0x3f] ?? "";
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
export const base64UrlDecodeToBytes = (encoded) => {
|
|
27
|
+
const out = [];
|
|
28
|
+
let buffer = 0;
|
|
29
|
+
let bits = 0;
|
|
30
|
+
for (const char of encoded) {
|
|
31
|
+
const index = BASE64URL_ALPHABET.indexOf(char);
|
|
32
|
+
if (index < 0) {
|
|
33
|
+
throw new Error(`Invalid base64url character: ${char}`);
|
|
34
|
+
}
|
|
35
|
+
buffer = (buffer << 6) | index;
|
|
36
|
+
bits += 6;
|
|
37
|
+
if (bits >= 8) {
|
|
38
|
+
bits -= 8;
|
|
39
|
+
out.push((buffer >>> bits) & 0xff);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return new Uint8Array(out);
|
|
43
|
+
};
|
|
44
|
+
export const bufferToBase64Url = (buffer) => base64UrlEncodeBytes(new Uint8Array(buffer));
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { ClientId, EmailChallengeResponse, EmailResendRequest, EmailStartRequest, EmailVerifyRequest, EmailVerifyResponse, MeDeleteResponse, MeEmailChangeStartRequest, MeEmailChangeStartResponse, MeEmailChangeVerifyNewRequest, MeEmailChangeVerifyNewResponse, MeEmailChangeVerifyOldRequest, MeEmailChangeVerifyOldResponse, MeIdentitiesListResponse, MeIdentityLinkRequest, MeIdentityLinkResponse, MeIdentityUnlinkResponse, MePasskeyRegistrationOptionsResponse, MePasskeyRegistrationVerifyRequest, MePasskeyRegistrationVerifyResponse, MePasskeyDeleteResponse, MePasskeysListResponse, MeSessionRevokeResponse, MeSessionsListResponse, MeSessionsRevokeAllResponse, MeUpdateRequest, MeUpdateResponse, MeUser, PasskeyAuthenticationOptionsRequest, PasskeyAuthenticationOptionsResponse, PasskeyAuthenticationVerifyRequest, PasskeyAuthenticationVerifyResponse, PasskeyRegistrationOptionsResponse, PasskeyRegistrationVerifyRequest, PublicConfigResponse, SocialProvider, SocialStartResponse, TransactionCancelResponse, TransactionCompleteResponse, TransactionCreateResponse, TransactionGetResponse, TransactionPasskeyRegistrationVerifyResponse } from "@smartcrab/contracts-public";
|
|
2
|
+
import type { Outcome } from "@smartcrab/contracts-public";
|
|
3
|
+
import type { BrowserAuthError } from "./errors.js";
|
|
4
|
+
import type { FetchPort } from "./http.js";
|
|
5
|
+
import type { WebAuthnBrowserPort } from "./webauthn.js";
|
|
6
|
+
/**
|
|
7
|
+
* `BrowserAuthClient` — the SPA-facing client for the public authentication
|
|
8
|
+
* API (design.md §16, §22.3).
|
|
9
|
+
*
|
|
10
|
+
* Security posture (design.md §22.3, §32.1/§32.2):
|
|
11
|
+
* - Authorization Code + PKCE S256; `state` and `nonce` are generated when not supplied.
|
|
12
|
+
* - Access and refresh tokens stay in memory by default; access tokens never enter Web Storage.
|
|
13
|
+
* - Short-lived PKCE callback metadata is stored in `sessionStorage` (never `localStorage`) so a
|
|
14
|
+
* top-level redirect can resume after the SPA reloads. It is consumed once and expires after
|
|
15
|
+
* 10 minutes; when storage is unavailable, the current client falls back to its memory copy.
|
|
16
|
+
* - Refresh-token persistence is a separate explicit opt-in via `refreshTokenStorage`.
|
|
17
|
+
* - No silent-iframe / third-party-cookie renewal. After reload, reauthorize top-level via
|
|
18
|
+
* `beginHostedAuthorization()` against the existing Hosted Session, or call `restoreSession()`
|
|
19
|
+
* when opt-in refresh-token persistence is configured.
|
|
20
|
+
*
|
|
21
|
+
* Result channel: `@praha/byethrow` does not resolve from this package (not a
|
|
22
|
+
* declared dependency), so every fallible method returns the local
|
|
23
|
+
* `Outcome<T, BrowserAuthError>` discriminated union re-exported from
|
|
24
|
+
* `@smartcrab/contracts-public` instead of `Result`.
|
|
25
|
+
*/
|
|
26
|
+
/** Top-level navigation port (design.md §20.6/§22.3: external, top-level redirects only). */
|
|
27
|
+
export interface RedirectPort {
|
|
28
|
+
assign(url: string): void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* OPT-IN refresh token persistence (design.md §22.3: "refresh tokenの永続保存
|
|
32
|
+
* は既定で無効").
|
|
33
|
+
*
|
|
34
|
+
* WARNING — think before enabling: anything written here survives page
|
|
35
|
+
* reloads and is readable by every script running on your origin, including
|
|
36
|
+
* any XSS payload. Prefer the default (memory-only) and re-authorize through
|
|
37
|
+
* the Hosted Session after reload. If you enable this, use the most
|
|
38
|
+
* constrained storage available to you (e.g. an encrypted, origin-isolated
|
|
39
|
+
* store), never plain `localStorage` on a hostile-script surface, and combine
|
|
40
|
+
* it with a strict CSP (design.md §32.2). The platform rotates refresh tokens
|
|
41
|
+
* on every use and detects replay (design.md §32.1), which limits — but does
|
|
42
|
+
* not eliminate — the blast radius of a stolen token.
|
|
43
|
+
*/
|
|
44
|
+
export interface RefreshTokenStoragePort {
|
|
45
|
+
load(): Promise<string | null> | string | null;
|
|
46
|
+
save(refreshToken: string): Promise<void> | void;
|
|
47
|
+
clear(): Promise<void> | void;
|
|
48
|
+
}
|
|
49
|
+
export interface BrowserAuthClientConfig {
|
|
50
|
+
/** Public API origin, e.g. `https://id.example.com` (trailing slash tolerated). */
|
|
51
|
+
readonly baseUrl: string;
|
|
52
|
+
readonly clientId: ClientId;
|
|
53
|
+
/** Must be byte-for-byte registered on the client (design.md §15.9). */
|
|
54
|
+
readonly redirectUri: string;
|
|
55
|
+
/** Space-delimited OIDC scopes; defaults to `openid profile email`. */
|
|
56
|
+
readonly scope?: string;
|
|
57
|
+
/** RFC 8707 resource indicators to bind to the grant (design.md §15.7). */
|
|
58
|
+
readonly resource?: readonly string[];
|
|
59
|
+
/**
|
|
60
|
+
* Issuer (`https://host/e/env_xxx`). Optional: discovered and cached from
|
|
61
|
+
* `GET /v1/config` on first token/authorize call when omitted.
|
|
62
|
+
*/
|
|
63
|
+
readonly issuer?: string;
|
|
64
|
+
/** fetch override (tests, custom instrumentation). Defaults to global `fetch`. */
|
|
65
|
+
readonly fetch?: FetchPort;
|
|
66
|
+
/** WebAuthn ceremony port. Defaults to `navigator.credentials`. */
|
|
67
|
+
readonly webAuthn?: WebAuthnBrowserPort;
|
|
68
|
+
/** Top-level navigation port. Defaults to `window.location.assign`. */
|
|
69
|
+
readonly redirect?: RedirectPort;
|
|
70
|
+
/** OPT-IN refresh token persistence — read the security warning on the type. */
|
|
71
|
+
readonly refreshTokenStorage?: RefreshTokenStoragePort;
|
|
72
|
+
/** Clock override for tests (epoch ms). */
|
|
73
|
+
readonly now?: () => number;
|
|
74
|
+
}
|
|
75
|
+
/** A live auth transaction plus the client-generated `state` that correlates its completion. */
|
|
76
|
+
export interface BrowserTransaction {
|
|
77
|
+
readonly transactionId: string;
|
|
78
|
+
readonly state: string;
|
|
79
|
+
readonly expiresAt: string;
|
|
80
|
+
readonly availableMethods: TransactionCreateResponse["available_methods"];
|
|
81
|
+
}
|
|
82
|
+
export interface CreateTransactionInput {
|
|
83
|
+
readonly scope?: string;
|
|
84
|
+
readonly resource?: readonly string[];
|
|
85
|
+
/** App-supplied correlation value; the SDK generates one when omitted. */
|
|
86
|
+
readonly state?: string;
|
|
87
|
+
readonly nonce?: string;
|
|
88
|
+
}
|
|
89
|
+
/** Session metadata. Note: the access token itself is only ever handed out by `getAccessToken()`. */
|
|
90
|
+
export interface AuthenticatedSession {
|
|
91
|
+
/** Epoch ms when the in-memory access token expires. */
|
|
92
|
+
readonly accessTokenExpiresAt: number;
|
|
93
|
+
readonly scope: string;
|
|
94
|
+
readonly hasRefreshToken: boolean;
|
|
95
|
+
}
|
|
96
|
+
/** Auth state snapshot for `subscribe`/`getSnapshot` (React bindings consume this). */
|
|
97
|
+
export interface BrowserAuthSnapshot {
|
|
98
|
+
readonly status: "signed_out" | "signed_in";
|
|
99
|
+
readonly accessTokenExpiresAt?: number;
|
|
100
|
+
readonly scope?: string;
|
|
101
|
+
readonly hasRefreshToken: boolean;
|
|
102
|
+
}
|
|
103
|
+
export type BrowserAuthListener = (snapshot: BrowserAuthSnapshot) => void;
|
|
104
|
+
export interface HostedAuthorization {
|
|
105
|
+
readonly url: string;
|
|
106
|
+
readonly state: string;
|
|
107
|
+
}
|
|
108
|
+
export interface BrowserAuthClient {
|
|
109
|
+
/** `GET /v1/config` (design.md §16.2), schema-validated. */
|
|
110
|
+
getPublicConfig(): Promise<Outcome<PublicConfigResponse, BrowserAuthError>>;
|
|
111
|
+
createTransaction(input?: CreateTransactionInput): Promise<Outcome<BrowserTransaction, BrowserAuthError>>;
|
|
112
|
+
getTransaction(transactionId: string): Promise<Outcome<TransactionGetResponse, BrowserAuthError>>;
|
|
113
|
+
cancelTransaction(transactionId: string): Promise<Outcome<TransactionCancelResponse, BrowserAuthError>>;
|
|
114
|
+
startEmailOtp(transactionId: string, input: EmailStartRequest): Promise<Outcome<EmailChallengeResponse, BrowserAuthError>>;
|
|
115
|
+
resendEmailOtp(transactionId: string, input: EmailResendRequest): Promise<Outcome<EmailChallengeResponse, BrowserAuthError>>;
|
|
116
|
+
verifyEmailOtp(transactionId: string, input: EmailVerifyRequest): Promise<Outcome<EmailVerifyResponse, BrowserAuthError>>;
|
|
117
|
+
getPasskeyRegistrationOptions(transactionId: string): Promise<Outcome<PasskeyRegistrationOptionsResponse, BrowserAuthError>>;
|
|
118
|
+
verifyPasskeyRegistration(transactionId: string, input: PasskeyRegistrationVerifyRequest): Promise<Outcome<TransactionPasskeyRegistrationVerifyResponse, BrowserAuthError>>;
|
|
119
|
+
/** Full registration ceremony: options → authenticator → verify. */
|
|
120
|
+
startPasskeyRegistration(transactionId: string, input?: {
|
|
121
|
+
readonly name?: string;
|
|
122
|
+
}): Promise<Outcome<TransactionPasskeyRegistrationVerifyResponse, BrowserAuthError>>;
|
|
123
|
+
getPasskeyAuthenticationOptions(transactionId: string, input?: PasskeyAuthenticationOptionsRequest): Promise<Outcome<PasskeyAuthenticationOptionsResponse, BrowserAuthError>>;
|
|
124
|
+
verifyPasskeyAuthentication(transactionId: string, input: PasskeyAuthenticationVerifyRequest): Promise<Outcome<PasskeyAuthenticationVerifyResponse, BrowserAuthError>>;
|
|
125
|
+
/** Full authentication ceremony (discoverable-credential default, design.md §17.2). */
|
|
126
|
+
startPasskeyAuthentication(transactionId: string, input?: PasskeyAuthenticationOptionsRequest): Promise<Outcome<PasskeyAuthenticationVerifyResponse, BrowserAuthError>>;
|
|
127
|
+
/**
|
|
128
|
+
* Starts social sign-in and performs a TOP-LEVEL redirect to the returned
|
|
129
|
+
* `authorization_url` (embedded iframes/WebViews are never used). The
|
|
130
|
+
* provider callback eventually lands on `redirectUri`; resume with
|
|
131
|
+
* `handleHostedAuthorizationCallback`.
|
|
132
|
+
*/
|
|
133
|
+
startSocialSignIn(transactionId: string, provider: SocialProvider): Promise<Outcome<SocialStartResponse, BrowserAuthError>>;
|
|
134
|
+
completeTransaction(transactionId: string): Promise<Outcome<TransactionCompleteResponse, BrowserAuthError>>;
|
|
135
|
+
/**
|
|
136
|
+
* Exchanges a one-time authorization code at the token endpoint using the
|
|
137
|
+
* PKCE verifier registered under `state`. Fails with `state_mismatch` when
|
|
138
|
+
* the state is unknown or expired.
|
|
139
|
+
*/
|
|
140
|
+
exchangeAuthorizationCode(input: {
|
|
141
|
+
readonly code: string;
|
|
142
|
+
readonly state: string;
|
|
143
|
+
}): Promise<Outcome<AuthenticatedSession, BrowserAuthError>>;
|
|
144
|
+
/** Builds `{issuer}/oauth2/authorize` with fresh PKCE/state/nonce (design.md §15.1). */
|
|
145
|
+
createHostedAuthorizationUrl(): Promise<Outcome<HostedAuthorization, BrowserAuthError>>;
|
|
146
|
+
/** Same as `createHostedAuthorizationUrl` plus a top-level navigation. */
|
|
147
|
+
beginHostedAuthorization(): Promise<Outcome<{
|
|
148
|
+
readonly state: string;
|
|
149
|
+
}, BrowserAuthError>>;
|
|
150
|
+
/** Parses the authorize/social callback URL and exchanges the code it carries. */
|
|
151
|
+
handleHostedAuthorizationCallback(callbackUrl: string): Promise<Outcome<AuthenticatedSession, BrowserAuthError>>;
|
|
152
|
+
getSnapshot(): BrowserAuthSnapshot;
|
|
153
|
+
subscribe(listener: BrowserAuthListener): () => void;
|
|
154
|
+
/** Runtime WebAuthn capability (design.md §21.2: UI must hide passkeys when false). */
|
|
155
|
+
isPasskeySupported(): boolean;
|
|
156
|
+
/** Current access token; silently refreshes first when expired and a refresh token exists. */
|
|
157
|
+
getAccessToken(): Promise<Outcome<string, BrowserAuthError>>;
|
|
158
|
+
/** Explicit refresh. Concurrent calls share one in-flight request (design.md §21.7). */
|
|
159
|
+
refreshSession(): Promise<Outcome<AuthenticatedSession, BrowserAuthError>>;
|
|
160
|
+
/**
|
|
161
|
+
* Reload recovery for the OPT-IN persistence path: loads the persisted
|
|
162
|
+
* refresh token and immediately rotates it into a fresh in-memory session.
|
|
163
|
+
* Without `refreshTokenStorage` (or an empty store) this resolves to
|
|
164
|
+
* `not_authenticated` and the app should start a fresh authorization.
|
|
165
|
+
*/
|
|
166
|
+
restoreSession(): Promise<Outcome<AuthenticatedSession, BrowserAuthError>>;
|
|
167
|
+
/** Best-effort refresh-token revocation, then full local cleanup. */
|
|
168
|
+
signOut(): Promise<Outcome<{
|
|
169
|
+
readonly signedOut: true;
|
|
170
|
+
}, BrowserAuthError>>;
|
|
171
|
+
getMe(): Promise<Outcome<MeUser, BrowserAuthError>>;
|
|
172
|
+
updateMe(input: MeUpdateRequest): Promise<Outcome<MeUpdateResponse, BrowserAuthError>>;
|
|
173
|
+
listMyPasskeys(): Promise<Outcome<MePasskeysListResponse, BrowserAuthError>>;
|
|
174
|
+
getMyPasskeyRegistrationOptions(): Promise<Outcome<MePasskeyRegistrationOptionsResponse, BrowserAuthError>>;
|
|
175
|
+
verifyMyPasskeyRegistration(input: MePasskeyRegistrationVerifyRequest): Promise<Outcome<MePasskeyRegistrationVerifyResponse, BrowserAuthError>>;
|
|
176
|
+
/** Full me-scoped registration ceremony: options → authenticator → verify. */
|
|
177
|
+
registerMyPasskey(input?: {
|
|
178
|
+
readonly name?: string;
|
|
179
|
+
}): Promise<Outcome<MePasskeyRegistrationVerifyResponse, BrowserAuthError>>;
|
|
180
|
+
deleteMyPasskey(passkeyId: string): Promise<Outcome<MePasskeyDeleteResponse, BrowserAuthError>>;
|
|
181
|
+
listMyIdentities(): Promise<Outcome<MeIdentitiesListResponse, BrowserAuthError>>;
|
|
182
|
+
linkMyIdentity(provider: SocialProvider, input: MeIdentityLinkRequest): Promise<Outcome<MeIdentityLinkResponse, BrowserAuthError>>;
|
|
183
|
+
unlinkMyIdentity(identityId: string): Promise<Outcome<MeIdentityUnlinkResponse, BrowserAuthError>>;
|
|
184
|
+
listMySessions(): Promise<Outcome<MeSessionsListResponse, BrowserAuthError>>;
|
|
185
|
+
revokeMySession(sessionId: string): Promise<Outcome<MeSessionRevokeResponse, BrowserAuthError>>;
|
|
186
|
+
revokeAllMySessions(): Promise<Outcome<MeSessionsRevokeAllResponse, BrowserAuthError>>;
|
|
187
|
+
startMyEmailChange(input: MeEmailChangeStartRequest): Promise<Outcome<MeEmailChangeStartResponse, BrowserAuthError>>;
|
|
188
|
+
verifyMyEmailChangeOld(input: MeEmailChangeVerifyOldRequest): Promise<Outcome<MeEmailChangeVerifyOldResponse, BrowserAuthError>>;
|
|
189
|
+
verifyMyEmailChangeNew(input: MeEmailChangeVerifyNewRequest): Promise<Outcome<MeEmailChangeVerifyNewResponse, BrowserAuthError>>;
|
|
190
|
+
deleteMyAccount(): Promise<Outcome<MeDeleteResponse, BrowserAuthError>>;
|
|
191
|
+
}
|
|
192
|
+
export declare const createBrowserAuthClient: (config: BrowserAuthClientConfig) => BrowserAuthClient;
|