@ctx9/web-auth 0.0.0-stage → 9.0.1
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/NOTICE +6 -0
- package/README.md +17 -2
- package/dist/adapters/express.d.ts +20 -0
- package/dist/adapters/express.d.ts.map +1 -0
- package/dist/adapters/express.js +24 -0
- package/dist/adapters/express.js.map +1 -0
- package/dist/adapters/node.d.ts +13 -0
- package/dist/adapters/node.d.ts.map +1 -0
- package/dist/adapters/node.js +30 -0
- package/dist/adapters/node.js.map +1 -0
- package/dist/adapters/tanstack-start.d.ts +10 -0
- package/dist/adapters/tanstack-start.d.ts.map +1 -0
- package/dist/adapters/tanstack-start.js +15 -0
- package/dist/adapters/tanstack-start.js.map +1 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +276 -0
- package/dist/index.js.map +1 -0
- package/dist/settings-gateway.d.ts +57 -0
- package/dist/settings-gateway.d.ts.map +1 -0
- package/dist/settings-gateway.js +128 -0
- package/dist/settings-gateway.js.map +1 -0
- package/package.json +49 -3
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.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ctx9/web-auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework-neutral server-side protocol for CTX9 confidential OIDC browser clients. Applications inject transaction/session persistence and keep their own client ID, secret, audience, redirect, cookie, tables, and authorization policy.
|
|
4
|
+
|
|
5
|
+
Licensed under Apache-2.0, with public npm distribution prepared for 9.0.1. Source and release acceptance are at [ContextNine/ctx9-web](https://github.com/ContextNine/ctx9-web). This is a relying-party package, not an identity authority or account-management service.
|
|
6
|
+
|
|
7
|
+
Never bundle this package into browser code or expose its transaction/token results to browser storage.
|
|
8
|
+
|
|
9
|
+
The package also signs and verifies 30-second tenant/account settings assertions for same-origin product BFFs. Each assertion binds one user, current tenant membership version, and exact internal method, path, and body digest. Product servers create assertions with their existing OIDC client secret; Platform verifies them against its bootstrap copy and rechecks every membership and capability from canonical state. Browsers never receive these assertions.
|
|
10
|
+
|
|
11
|
+
`requestTenantAccountSettings` serializes, signs, and sends that request without returning the assertion to browser code. It returns untrusted JSON for the product BFF to parse against its response contract.
|
|
12
|
+
|
|
13
|
+
Public server entry points:
|
|
14
|
+
|
|
15
|
+
- `@ctx9/web-auth` for the injected OIDC protocol;
|
|
16
|
+
- `@ctx9/web-auth/tanstack-start` for standard Request/Response server functions;
|
|
17
|
+
- `@ctx9/web-auth/node` for Node HTTP;
|
|
18
|
+
- `@ctx9/web-auth/express` for a dependency-free structural Express adapter.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type AuthorizationCallback } from "../index.js";
|
|
2
|
+
/** Structural contracts keep Express optional while matching its request/response API. */
|
|
3
|
+
export interface ExpressRequestLike {
|
|
4
|
+
originalUrl: string;
|
|
5
|
+
get(name: string): string | undefined;
|
|
6
|
+
}
|
|
7
|
+
export interface ExpressResponseLike {
|
|
8
|
+
append(name: string, value: string): unknown;
|
|
9
|
+
end(): unknown;
|
|
10
|
+
location(value: string): unknown;
|
|
11
|
+
status(code: number): unknown;
|
|
12
|
+
}
|
|
13
|
+
export declare function parseExpressAuthorizationCallback(request: ExpressRequestLike, applicationOrigin: string): AuthorizationCallback;
|
|
14
|
+
export declare function assertExpressSameOriginMutation(request: ExpressRequestLike, expectedOrigin: string): void;
|
|
15
|
+
export declare function writeExpressRedirect(response: ExpressResponseLike, options: {
|
|
16
|
+
location: string;
|
|
17
|
+
cookies?: readonly string[];
|
|
18
|
+
status?: 302 | 303;
|
|
19
|
+
}): void;
|
|
20
|
+
//# sourceMappingURL=express.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.d.ts","sourceRoot":"","sources":["../../src/adapters/express.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,aAAa,CAAC;AAErB,0FAA0F;AAC1F,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACvC;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7C,GAAG,IAAI,OAAO,CAAC;IACf,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACjC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAC/B;AAWD,wBAAgB,iCAAiC,CAC/C,OAAO,EAAE,kBAAkB,EAC3B,iBAAiB,EAAE,MAAM,GACxB,qBAAqB,CAIvB;AAED,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,kBAAkB,EAC3B,cAAc,EAAE,MAAM,GACrB,IAAI,CAEN;AAED,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,mBAAmB,EAC7B,OAAO,EAAE;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;CACpB,GACA,IAAI,CAMN"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { assertSameOriginMutation, parseAuthorizationCallback, } from "../index.js";
|
|
2
|
+
function expressHeaders(request) {
|
|
3
|
+
const headers = new Headers();
|
|
4
|
+
for (const name of ["origin", "sec-fetch-site"]) {
|
|
5
|
+
const value = request.get(name);
|
|
6
|
+
if (value)
|
|
7
|
+
headers.set(name, value);
|
|
8
|
+
}
|
|
9
|
+
return headers;
|
|
10
|
+
}
|
|
11
|
+
export function parseExpressAuthorizationCallback(request, applicationOrigin) {
|
|
12
|
+
return parseAuthorizationCallback(new URL(request.originalUrl, applicationOrigin));
|
|
13
|
+
}
|
|
14
|
+
export function assertExpressSameOriginMutation(request, expectedOrigin) {
|
|
15
|
+
assertSameOriginMutation(expressHeaders(request), expectedOrigin);
|
|
16
|
+
}
|
|
17
|
+
export function writeExpressRedirect(response, options) {
|
|
18
|
+
response.status(options.status ?? 303);
|
|
19
|
+
response.location(options.location);
|
|
20
|
+
for (const cookie of options.cookies ?? [])
|
|
21
|
+
response.append("set-cookie", cookie);
|
|
22
|
+
response.end();
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=express.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.js","sourceRoot":"","sources":["../../src/adapters/express.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,0BAA0B,GAE3B,MAAM,aAAa,CAAC;AAerB,SAAS,cAAc,CAAC,OAA2B;IACjD,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;IAC9B,KAAK,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,gBAAgB,CAAU,EAAE,CAAC;QACzD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAChC,IAAI,KAAK;YAAE,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACtC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,iCAAiC,CAC/C,OAA2B,EAC3B,iBAAyB;IAEzB,OAAO,0BAA0B,CAC/B,IAAI,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAChD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,+BAA+B,CAC7C,OAA2B,EAC3B,cAAsB;IAEtB,wBAAwB,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,cAAc,CAAC,CAAC;AACpE,CAAC;AAED,MAAM,UAAU,oBAAoB,CAClC,QAA6B,EAC7B,OAIC;IAED,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC;IACvC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACpC,KAAK,MAAM,MAAM,IAAI,OAAO,CAAC,OAAO,IAAI,EAAE;QACxC,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IACxC,QAAQ,CAAC,GAAG,EAAE,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { type AuthorizationCallback } from "../index.js";
|
|
3
|
+
type NodeRequest = Pick<IncomingMessage, "headers" | "url">;
|
|
4
|
+
type NodeResponse = Pick<ServerResponse, "end" | "setHeader" | "statusCode">;
|
|
5
|
+
export declare function parseNodeAuthorizationCallback(request: NodeRequest, applicationOrigin: string): AuthorizationCallback;
|
|
6
|
+
export declare function assertNodeSameOriginMutation(request: NodeRequest, expectedOrigin: string): void;
|
|
7
|
+
export declare function writeNodeRedirect(response: NodeResponse, options: {
|
|
8
|
+
location: string;
|
|
9
|
+
cookies?: readonly string[];
|
|
10
|
+
status?: 302 | 303;
|
|
11
|
+
}): void;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=node.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../src/adapters/node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEjE,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,aAAa,CAAC;AAErB,KAAK,WAAW,GAAG,IAAI,CAAC,eAAe,EAAE,SAAS,GAAG,KAAK,CAAC,CAAC;AAC5D,KAAK,YAAY,GAAG,IAAI,CAAC,cAAc,EAAE,KAAK,GAAG,WAAW,GAAG,YAAY,CAAC,CAAC;AAc7E,wBAAgB,8BAA8B,CAC5C,OAAO,EAAE,WAAW,EACpB,iBAAiB,EAAE,MAAM,GACxB,qBAAqB,CAGvB;AAED,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,WAAW,EACpB,cAAc,EAAE,MAAM,GACrB,IAAI,CAEN;AAED,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,YAAY,EACtB,OAAO,EAAE;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;CACpB,GACA,IAAI,CAMN"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { assertSameOriginMutation, parseAuthorizationCallback, } from "../index.js";
|
|
2
|
+
function nodeHeaders(request) {
|
|
3
|
+
const headers = new Headers();
|
|
4
|
+
for (const [name, value] of Object.entries(request.headers)) {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
for (const item of value)
|
|
7
|
+
headers.append(name, item);
|
|
8
|
+
}
|
|
9
|
+
else if (value !== undefined) {
|
|
10
|
+
headers.set(name, value);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return headers;
|
|
14
|
+
}
|
|
15
|
+
export function parseNodeAuthorizationCallback(request, applicationOrigin) {
|
|
16
|
+
if (!request.url)
|
|
17
|
+
throw new Error("request-url-missing");
|
|
18
|
+
return parseAuthorizationCallback(new URL(request.url, applicationOrigin));
|
|
19
|
+
}
|
|
20
|
+
export function assertNodeSameOriginMutation(request, expectedOrigin) {
|
|
21
|
+
assertSameOriginMutation(nodeHeaders(request), expectedOrigin);
|
|
22
|
+
}
|
|
23
|
+
export function writeNodeRedirect(response, options) {
|
|
24
|
+
response.statusCode = options.status ?? 303;
|
|
25
|
+
response.setHeader("location", options.location);
|
|
26
|
+
if (options.cookies?.length)
|
|
27
|
+
response.setHeader("set-cookie", options.cookies);
|
|
28
|
+
response.end();
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../src/adapters/node.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,wBAAwB,EACxB,0BAA0B,GAE3B,MAAM,aAAa,CAAC;AAKrB,SAAS,WAAW,CAAC,OAAoB;IACvC,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;IAC9B,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5D,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,KAAK,MAAM,IAAI,IAAI,KAAK;gBAAE,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACvD,CAAC;aAAM,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,8BAA8B,CAC5C,OAAoB,EACpB,iBAAyB;IAEzB,IAAI,CAAC,OAAO,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IACzD,OAAO,0BAA0B,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,4BAA4B,CAC1C,OAAoB,EACpB,cAAsB;IAEtB,wBAAwB,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,cAAc,CAAC,CAAC;AACjE,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,QAAsB,EACtB,OAIC;IAED,QAAQ,CAAC,UAAU,GAAG,OAAO,CAAC,MAAM,IAAI,GAAG,CAAC;IAC5C,QAAQ,CAAC,SAAS,CAAC,UAAU,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IACjD,IAAI,OAAO,CAAC,OAAO,EAAE,MAAM;QACzB,QAAQ,CAAC,SAAS,CAAC,YAAY,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACpD,QAAQ,CAAC,GAAG,EAAE,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type AuthorizationCallback } from "../index.js";
|
|
2
|
+
/** TanStack Start server functions expose standard Request and Response objects. */
|
|
3
|
+
export declare function parseTanStackAuthorizationCallback(request: Request): AuthorizationCallback;
|
|
4
|
+
export declare function assertTanStackSameOriginMutation(request: Request, expectedOrigin: string): void;
|
|
5
|
+
export declare function tanStackRedirectResponse(options: {
|
|
6
|
+
location: string;
|
|
7
|
+
cookies?: readonly string[];
|
|
8
|
+
status?: 302 | 303;
|
|
9
|
+
}): Response;
|
|
10
|
+
//# sourceMappingURL=tanstack-start.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tanstack-start.d.ts","sourceRoot":"","sources":["../../src/adapters/tanstack-start.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,aAAa,CAAC;AAErB,oFAAoF;AACpF,wBAAgB,kCAAkC,CAChD,OAAO,EAAE,OAAO,GACf,qBAAqB,CAEvB;AAED,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,OAAO,EAChB,cAAc,EAAE,MAAM,GACrB,IAAI,CAEN;AAED,wBAAgB,wBAAwB,CAAC,OAAO,EAAE;IAChD,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;CACpB,GAAG,QAAQ,CAKX"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { assertSameOriginMutation, parseAuthorizationCallback, } from "../index.js";
|
|
2
|
+
/** TanStack Start server functions expose standard Request and Response objects. */
|
|
3
|
+
export function parseTanStackAuthorizationCallback(request) {
|
|
4
|
+
return parseAuthorizationCallback(new URL(request.url));
|
|
5
|
+
}
|
|
6
|
+
export function assertTanStackSameOriginMutation(request, expectedOrigin) {
|
|
7
|
+
assertSameOriginMutation(request.headers, expectedOrigin);
|
|
8
|
+
}
|
|
9
|
+
export function tanStackRedirectResponse(options) {
|
|
10
|
+
const headers = new Headers({ location: options.location });
|
|
11
|
+
for (const cookie of options.cookies ?? [])
|
|
12
|
+
headers.append("set-cookie", cookie);
|
|
13
|
+
return new Response(null, { status: options.status ?? 303, headers });
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=tanstack-start.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tanstack-start.js","sourceRoot":"","sources":["../../src/adapters/tanstack-start.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,0BAA0B,GAE3B,MAAM,aAAa,CAAC;AAErB,oFAAoF;AACpF,MAAM,UAAU,kCAAkC,CAChD,OAAgB;IAEhB,OAAO,0BAA0B,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1D,CAAC;AAED,MAAM,UAAU,gCAAgC,CAC9C,OAAgB,EAChB,cAAsB;IAEtB,wBAAwB,CAAC,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;AAC5D,CAAC;AAED,MAAM,UAAU,wBAAwB,CAAC,OAIxC;IACC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC5D,KAAK,MAAM,MAAM,IAAI,OAAO,CAAC,OAAO,IAAI,EAAE;QACxC,OAAO,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IACvC,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;AACxE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type JWTPayload } from "jose";
|
|
2
|
+
export * from "./settings-gateway.js";
|
|
3
|
+
export interface OidcClientPolicy {
|
|
4
|
+
issuer: string;
|
|
5
|
+
clientId: string;
|
|
6
|
+
clientSecret: string;
|
|
7
|
+
redirectUri: string;
|
|
8
|
+
audience: string;
|
|
9
|
+
scopes: readonly string[];
|
|
10
|
+
}
|
|
11
|
+
export interface AuthorizationTransaction {
|
|
12
|
+
stateDigest: string;
|
|
13
|
+
clientId: string;
|
|
14
|
+
redirectUri: string;
|
|
15
|
+
nonce: string;
|
|
16
|
+
verifierCiphertext: string;
|
|
17
|
+
verifierIv: string;
|
|
18
|
+
purpose: string;
|
|
19
|
+
returnTo?: string;
|
|
20
|
+
createdAt: Date;
|
|
21
|
+
expiresAt: Date;
|
|
22
|
+
}
|
|
23
|
+
export interface AuthorizationTransactionStore {
|
|
24
|
+
create(transaction: AuthorizationTransaction): Promise<void>;
|
|
25
|
+
consume(stateDigest: string, now: Date): Promise<AuthorizationTransaction | undefined>;
|
|
26
|
+
}
|
|
27
|
+
export type AuthorizationCallback = {
|
|
28
|
+
kind: "success";
|
|
29
|
+
code: string;
|
|
30
|
+
state: string;
|
|
31
|
+
} | {
|
|
32
|
+
kind: "error";
|
|
33
|
+
state: string;
|
|
34
|
+
error: OAuthAuthorizationError;
|
|
35
|
+
};
|
|
36
|
+
export interface VerifiedIdentityToken {
|
|
37
|
+
payload: JWTPayload;
|
|
38
|
+
protectedHeader: {
|
|
39
|
+
alg?: string;
|
|
40
|
+
kid?: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface CompleteAuthorizationOptions {
|
|
44
|
+
fetch?: typeof fetch;
|
|
45
|
+
now?: Date;
|
|
46
|
+
verifyIdToken?: (token: string, policy: OidcClientPolicy, nonce: string) => Promise<VerifiedIdentityToken>;
|
|
47
|
+
}
|
|
48
|
+
export declare class OAuthAuthorizationError extends Error {
|
|
49
|
+
readonly oauthError: string;
|
|
50
|
+
readonly oauthErrorDescription: string;
|
|
51
|
+
constructor(oauthError: string, oauthErrorDescription: string);
|
|
52
|
+
}
|
|
53
|
+
export declare class OAuthTokenExchangeError extends Error {
|
|
54
|
+
readonly status: number;
|
|
55
|
+
readonly oauthError: string;
|
|
56
|
+
readonly oauthErrorDescription: string;
|
|
57
|
+
constructor(status: number, oauthError: string, oauthErrorDescription: string);
|
|
58
|
+
}
|
|
59
|
+
export declare function digestOpaque(value: string): string;
|
|
60
|
+
export declare function equalOpaqueDigest(left: string, right: string): boolean;
|
|
61
|
+
export declare function encodeOidcBasicCredentials(clientId: string, clientSecret: string): string;
|
|
62
|
+
export declare function sealPkceVerifier(verifier: string, transactionKey: string): {
|
|
63
|
+
ciphertext: string;
|
|
64
|
+
iv: string;
|
|
65
|
+
};
|
|
66
|
+
export declare function openPkceVerifier(ciphertext: string, iv: string, transactionKey: string): string;
|
|
67
|
+
export declare function parseAuthorizationCallback(input: URL | URLSearchParams | Record<string, unknown>): AuthorizationCallback;
|
|
68
|
+
export declare function beginAuthorization(policy: OidcClientPolicy, store: AuthorizationTransactionStore, transactionKey: string, options: {
|
|
69
|
+
purpose: string;
|
|
70
|
+
returnTo?: string;
|
|
71
|
+
prompt?: "login";
|
|
72
|
+
maxAge?: 0;
|
|
73
|
+
now?: Date;
|
|
74
|
+
lifetimeMs?: number;
|
|
75
|
+
}): Promise<string>;
|
|
76
|
+
export declare function verifyIdentityToken(token: string, policy: OidcClientPolicy, nonce: string): Promise<VerifiedIdentityToken>;
|
|
77
|
+
export declare function completeAuthorization(policy: OidcClientPolicy, store: AuthorizationTransactionStore, transactionKey: string, callback: AuthorizationCallback, options?: CompleteAuthorizationOptions): Promise<{
|
|
78
|
+
transaction: AuthorizationTransaction;
|
|
79
|
+
identity: VerifiedIdentityToken;
|
|
80
|
+
}>;
|
|
81
|
+
export declare function normalizeSameOriginReturnTo(value: string, origin: string, maxLength?: number): string;
|
|
82
|
+
export declare function serializeHostCookie(options: {
|
|
83
|
+
name: `__Host-${string}`;
|
|
84
|
+
value: string;
|
|
85
|
+
maxAgeSeconds: number;
|
|
86
|
+
sameSite: "Strict" | "Lax";
|
|
87
|
+
httpOnly?: boolean;
|
|
88
|
+
}): string;
|
|
89
|
+
export declare function assertSameOriginMutation(headers: Headers, expectedOrigin: string): void;
|
|
90
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAiC,KAAK,UAAU,EAAE,MAAM,MAAM,CAAC;AAGtE,cAAc,uBAAuB,CAAC;AAoBtC,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,wBAAwB;IACvC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,kBAAkB,EAAE,MAAM,CAAC;IAC3B,UAAU,EAAE,MAAM,CAAC;IAC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,gBAAgB,EACxB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,qBAAqB,CAAC,CAgBhC;AAED,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,gBAAgB,EACxB,KAAK,EAAE,6BAA6B,EACpC,cAAc,EAAE,MAAM,EACtB,QAAQ,EAAE,qBAAqB,EAC/B,OAAO,GAAE,4BAAiC,GACzC,OAAO,CAAC;IACT,WAAW,EAAE,wBAAwB,CAAC;IACtC,QAAQ,EAAE,qBAAqB,CAAC;CACjC,CAAC,CA6DD;AAED,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,SAAS,SAAM,GACd,MAAM,CAYR;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE;IAC3C,IAAI,EAAE,UAAU,MAAM,EAAE,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,QAAQ,GAAG,KAAK,CAAC;IAC3B,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,GAAG,MAAM,CAaT;AAED,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,OAAO,EAChB,cAAc,EAAE,MAAM,GACrB,IAAI,CAQN"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes, timingSafeEqual, } from "node:crypto";
|
|
2
|
+
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
export * from "./settings-gateway.js";
|
|
5
|
+
const safeOAuthErrors = new Set([
|
|
6
|
+
"access_denied",
|
|
7
|
+
"account_selection_required",
|
|
8
|
+
"consent_required",
|
|
9
|
+
"interaction_required",
|
|
10
|
+
"invalid_grant",
|
|
11
|
+
"invalid_request",
|
|
12
|
+
"invalid_scope",
|
|
13
|
+
"invalid_target",
|
|
14
|
+
"login_required",
|
|
15
|
+
"server_error",
|
|
16
|
+
"temporarily_unavailable",
|
|
17
|
+
"unauthorized_client",
|
|
18
|
+
"unsupported_response_type",
|
|
19
|
+
]);
|
|
20
|
+
const callbackRecord = z.record(z.string(), z.unknown());
|
|
21
|
+
export class OAuthAuthorizationError extends Error {
|
|
22
|
+
oauthError;
|
|
23
|
+
oauthErrorDescription;
|
|
24
|
+
constructor(oauthError, oauthErrorDescription) {
|
|
25
|
+
super(`oidc-authorization-${oauthError.replaceAll("_", "-")}`);
|
|
26
|
+
this.oauthError = oauthError;
|
|
27
|
+
this.oauthErrorDescription = oauthErrorDescription;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export class OAuthTokenExchangeError extends Error {
|
|
31
|
+
status;
|
|
32
|
+
oauthError;
|
|
33
|
+
oauthErrorDescription;
|
|
34
|
+
constructor(status, oauthError, oauthErrorDescription) {
|
|
35
|
+
super("oidc-token-exchange-failed");
|
|
36
|
+
this.status = status;
|
|
37
|
+
this.oauthError = oauthError;
|
|
38
|
+
this.oauthErrorDescription = oauthErrorDescription;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function sha256(value) {
|
|
42
|
+
return createHash("sha256").update(value).digest();
|
|
43
|
+
}
|
|
44
|
+
export function digestOpaque(value) {
|
|
45
|
+
return sha256(value).toString("hex");
|
|
46
|
+
}
|
|
47
|
+
export function equalOpaqueDigest(left, right) {
|
|
48
|
+
const leftBytes = Buffer.from(left, "hex");
|
|
49
|
+
const rightBytes = Buffer.from(right, "hex");
|
|
50
|
+
return (leftBytes.length === rightBytes.length &&
|
|
51
|
+
timingSafeEqual(leftBytes, rightBytes));
|
|
52
|
+
}
|
|
53
|
+
function assertSecret(name, value) {
|
|
54
|
+
if (Buffer.byteLength(value, "utf8") < 32)
|
|
55
|
+
throw new Error(`${name}-too-short`);
|
|
56
|
+
}
|
|
57
|
+
function assertPolicy(policy) {
|
|
58
|
+
assertSecret("oidc-client-secret", policy.clientSecret);
|
|
59
|
+
const issuer = new URL(policy.issuer);
|
|
60
|
+
const redirect = new URL(policy.redirectUri);
|
|
61
|
+
const audience = new URL(policy.audience);
|
|
62
|
+
if (issuer.protocol !== "https:" ||
|
|
63
|
+
redirect.protocol !== "https:" ||
|
|
64
|
+
audience.protocol !== "https:" ||
|
|
65
|
+
policy.scopes.length === 0 ||
|
|
66
|
+
!policy.scopes.includes("openid"))
|
|
67
|
+
throw new Error("oidc-client-policy-invalid");
|
|
68
|
+
}
|
|
69
|
+
export function encodeOidcBasicCredentials(clientId, clientSecret) {
|
|
70
|
+
const encode = (value) => new URLSearchParams({ value }).toString().slice("value=".length);
|
|
71
|
+
return `Basic ${Buffer.from(`${encode(clientId)}:${encode(clientSecret)}`).toString("base64")}`;
|
|
72
|
+
}
|
|
73
|
+
export function sealPkceVerifier(verifier, transactionKey) {
|
|
74
|
+
assertSecret("oidc-transaction-key", transactionKey);
|
|
75
|
+
const iv = randomBytes(12);
|
|
76
|
+
const cipher = createCipheriv("aes-256-gcm", sha256(transactionKey), iv);
|
|
77
|
+
cipher.setAAD(Buffer.from("ctx9:web-auth:pkce:v1"));
|
|
78
|
+
const bytes = Buffer.concat([
|
|
79
|
+
cipher.update(verifier, "utf8"),
|
|
80
|
+
cipher.final(),
|
|
81
|
+
cipher.getAuthTag(),
|
|
82
|
+
]);
|
|
83
|
+
return {
|
|
84
|
+
ciphertext: bytes.toString("base64url"),
|
|
85
|
+
iv: iv.toString("base64url"),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function openPkceVerifier(ciphertext, iv, transactionKey) {
|
|
89
|
+
assertSecret("oidc-transaction-key", transactionKey);
|
|
90
|
+
const bytes = Buffer.from(ciphertext, "base64url");
|
|
91
|
+
if (bytes.length <= 16)
|
|
92
|
+
throw new Error("oidc-transaction-invalid");
|
|
93
|
+
const decipher = createDecipheriv("aes-256-gcm", sha256(transactionKey), Buffer.from(iv, "base64url"));
|
|
94
|
+
decipher.setAAD(Buffer.from("ctx9:web-auth:pkce:v1"));
|
|
95
|
+
decipher.setAuthTag(bytes.subarray(-16));
|
|
96
|
+
return Buffer.concat([
|
|
97
|
+
decipher.update(bytes.subarray(0, -16)),
|
|
98
|
+
decipher.final(),
|
|
99
|
+
]).toString("utf8");
|
|
100
|
+
}
|
|
101
|
+
function single(value) {
|
|
102
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
103
|
+
}
|
|
104
|
+
function callbackValues(input) {
|
|
105
|
+
if (input instanceof URL)
|
|
106
|
+
return Object.fromEntries(input.searchParams);
|
|
107
|
+
if (input instanceof URLSearchParams)
|
|
108
|
+
return Object.fromEntries(input);
|
|
109
|
+
return callbackRecord.parse(input);
|
|
110
|
+
}
|
|
111
|
+
export function parseAuthorizationCallback(input) {
|
|
112
|
+
const values = callbackValues(input);
|
|
113
|
+
const state = single(values.state);
|
|
114
|
+
const code = single(values.code);
|
|
115
|
+
const rawError = single(values.error);
|
|
116
|
+
if (rawError) {
|
|
117
|
+
if (!state)
|
|
118
|
+
throw new Error("oidc-callback-invalid");
|
|
119
|
+
const error = safeOAuthErrors.has(rawError) ? rawError : "unknown";
|
|
120
|
+
const rawDescription = single(values.error_description);
|
|
121
|
+
const description = rawDescription && /^[a-zA-Z0-9 .,:_()/-]{1,160}$/u.test(rawDescription)
|
|
122
|
+
? rawDescription
|
|
123
|
+
: "not provided";
|
|
124
|
+
return {
|
|
125
|
+
kind: "error",
|
|
126
|
+
state,
|
|
127
|
+
error: new OAuthAuthorizationError(error, description),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (!code || !state)
|
|
131
|
+
throw new Error("oidc-callback-invalid");
|
|
132
|
+
return { kind: "success", code, state };
|
|
133
|
+
}
|
|
134
|
+
export async function beginAuthorization(policy, store, transactionKey, options) {
|
|
135
|
+
assertPolicy(policy);
|
|
136
|
+
assertSecret("oidc-transaction-key", transactionKey);
|
|
137
|
+
const now = options.now ?? new Date();
|
|
138
|
+
const state = randomBytes(32).toString("base64url");
|
|
139
|
+
const nonce = randomBytes(32).toString("base64url");
|
|
140
|
+
const verifier = randomBytes(48).toString("base64url");
|
|
141
|
+
const sealed = sealPkceVerifier(verifier, transactionKey);
|
|
142
|
+
await store.create({
|
|
143
|
+
stateDigest: digestOpaque(state),
|
|
144
|
+
clientId: policy.clientId,
|
|
145
|
+
redirectUri: policy.redirectUri,
|
|
146
|
+
nonce,
|
|
147
|
+
verifierCiphertext: sealed.ciphertext,
|
|
148
|
+
verifierIv: sealed.iv,
|
|
149
|
+
purpose: options.purpose,
|
|
150
|
+
...(options.returnTo ? { returnTo: options.returnTo } : {}),
|
|
151
|
+
createdAt: now,
|
|
152
|
+
expiresAt: new Date(now.getTime() + (options.lifetimeMs ?? 5 * 60_000)),
|
|
153
|
+
});
|
|
154
|
+
const authorization = new URL(`${policy.issuer}/oauth2/authorize`);
|
|
155
|
+
authorization.search = new URLSearchParams({
|
|
156
|
+
response_type: "code",
|
|
157
|
+
client_id: policy.clientId,
|
|
158
|
+
redirect_uri: policy.redirectUri,
|
|
159
|
+
scope: policy.scopes.join(" "),
|
|
160
|
+
state,
|
|
161
|
+
nonce,
|
|
162
|
+
code_challenge: sha256(verifier).toString("base64url"),
|
|
163
|
+
code_challenge_method: "S256",
|
|
164
|
+
resource: policy.audience,
|
|
165
|
+
}).toString();
|
|
166
|
+
if (options.prompt)
|
|
167
|
+
authorization.searchParams.set("prompt", options.prompt);
|
|
168
|
+
if (options.maxAge === 0)
|
|
169
|
+
authorization.searchParams.set("max_age", "0");
|
|
170
|
+
return authorization.toString();
|
|
171
|
+
}
|
|
172
|
+
function safeTokenError(body) {
|
|
173
|
+
if (!body || typeof body !== "object")
|
|
174
|
+
return { error: "unknown", description: "not provided" };
|
|
175
|
+
const value = body;
|
|
176
|
+
const rawError = single(value.error);
|
|
177
|
+
const rawDescription = single(value.error_description);
|
|
178
|
+
return {
|
|
179
|
+
error: rawError && safeOAuthErrors.has(rawError) ? rawError : "unknown",
|
|
180
|
+
description: rawDescription && /^[a-zA-Z0-9 .,:_()/-]{1,160}$/u.test(rawDescription)
|
|
181
|
+
? rawDescription
|
|
182
|
+
: "not provided",
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
export async function verifyIdentityToken(token, policy, nonce) {
|
|
186
|
+
const keys = createRemoteJWKSet(new URL(`${policy.issuer}/jwks`));
|
|
187
|
+
const verified = await jwtVerify(token, keys, {
|
|
188
|
+
issuer: policy.issuer,
|
|
189
|
+
audience: policy.clientId,
|
|
190
|
+
});
|
|
191
|
+
if (verified.payload.nonce !== nonce)
|
|
192
|
+
throw new Error("oidc-nonce-invalid");
|
|
193
|
+
return {
|
|
194
|
+
payload: verified.payload,
|
|
195
|
+
protectedHeader: {
|
|
196
|
+
alg: verified.protectedHeader.alg,
|
|
197
|
+
...(verified.protectedHeader.kid
|
|
198
|
+
? { kid: verified.protectedHeader.kid }
|
|
199
|
+
: {}),
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
export async function completeAuthorization(policy, store, transactionKey, callback, options = {}) {
|
|
204
|
+
assertPolicy(policy);
|
|
205
|
+
if (callback.kind === "error")
|
|
206
|
+
throw callback.error;
|
|
207
|
+
const now = options.now ?? new Date();
|
|
208
|
+
const transaction = await store.consume(digestOpaque(callback.state), now);
|
|
209
|
+
if (!transaction ||
|
|
210
|
+
transaction.expiresAt <= now ||
|
|
211
|
+
transaction.clientId !== policy.clientId ||
|
|
212
|
+
transaction.redirectUri !== policy.redirectUri)
|
|
213
|
+
throw new Error("oidc-transaction-invalid");
|
|
214
|
+
const verifier = openPkceVerifier(transaction.verifierCiphertext, transaction.verifierIv, transactionKey);
|
|
215
|
+
const response = await (options.fetch ?? fetch)(`${policy.issuer}/oauth2/token`, {
|
|
216
|
+
method: "POST",
|
|
217
|
+
headers: {
|
|
218
|
+
authorization: encodeOidcBasicCredentials(policy.clientId, policy.clientSecret),
|
|
219
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
220
|
+
},
|
|
221
|
+
body: new URLSearchParams({
|
|
222
|
+
grant_type: "authorization_code",
|
|
223
|
+
code: callback.code,
|
|
224
|
+
redirect_uri: policy.redirectUri,
|
|
225
|
+
client_id: policy.clientId,
|
|
226
|
+
code_verifier: verifier,
|
|
227
|
+
resource: policy.audience,
|
|
228
|
+
}),
|
|
229
|
+
redirect: "error",
|
|
230
|
+
});
|
|
231
|
+
if (!response.ok) {
|
|
232
|
+
const safe = safeTokenError(await response.json().catch(() => undefined));
|
|
233
|
+
throw new OAuthTokenExchangeError(response.status, safe.error, safe.description);
|
|
234
|
+
}
|
|
235
|
+
const body = await response.json();
|
|
236
|
+
if (!body ||
|
|
237
|
+
typeof body !== "object" ||
|
|
238
|
+
!("id_token" in body) ||
|
|
239
|
+
typeof body.id_token !== "string")
|
|
240
|
+
throw new Error("oidc-token-response-invalid");
|
|
241
|
+
const identity = await (options.verifyIdToken ?? verifyIdentityToken)(body.id_token, policy, transaction.nonce);
|
|
242
|
+
return { transaction, identity };
|
|
243
|
+
}
|
|
244
|
+
export function normalizeSameOriginReturnTo(value, origin, maxLength = 512) {
|
|
245
|
+
if (!value ||
|
|
246
|
+
value.length > maxLength ||
|
|
247
|
+
!value.startsWith("/") ||
|
|
248
|
+
value.startsWith("//"))
|
|
249
|
+
throw new Error("return-to-invalid");
|
|
250
|
+
const parsed = new URL(value, origin);
|
|
251
|
+
if (parsed.origin !== origin || parsed.hash)
|
|
252
|
+
throw new Error("return-to-invalid");
|
|
253
|
+
return `${parsed.pathname}${parsed.search}`;
|
|
254
|
+
}
|
|
255
|
+
export function serializeHostCookie(options) {
|
|
256
|
+
if (!options.value || /[;\r\n]/u.test(options.value))
|
|
257
|
+
throw new Error("cookie-value-invalid");
|
|
258
|
+
return [
|
|
259
|
+
`${options.name}=${options.value}`,
|
|
260
|
+
"Path=/",
|
|
261
|
+
"Secure",
|
|
262
|
+
options.httpOnly === false ? undefined : "HttpOnly",
|
|
263
|
+
`SameSite=${options.sameSite}`,
|
|
264
|
+
`Max-Age=${String(options.maxAgeSeconds)}`,
|
|
265
|
+
]
|
|
266
|
+
.filter((value) => Boolean(value))
|
|
267
|
+
.join("; ");
|
|
268
|
+
}
|
|
269
|
+
export function assertSameOriginMutation(headers, expectedOrigin) {
|
|
270
|
+
const site = headers.get("sec-fetch-site");
|
|
271
|
+
const origin = headers.get("origin");
|
|
272
|
+
if ((site && site !== "same-origin" && site !== "none") ||
|
|
273
|
+
origin !== expectedOrigin)
|
|
274
|
+
throw new Error("browser-origin-denied");
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAmB,MAAM,MAAM,CAAC;AACtE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,cAAc,uBAAuB,CAAC;AAEtC,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC;IAC9B,eAAe;IACf,4BAA4B;IAC5B,kBAAkB;IAClB,sBAAsB;IACtB,eAAe;IACf,iBAAiB;IACjB,eAAe;IACf,gBAAgB;IAChB,gBAAgB;IAChB,cAAc;IACd,yBAAyB;IACzB,qBAAqB;IACrB,2BAA2B;CAC5B,CAAC,CAAC;AAEH,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;AAmDzD,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAErC;IACA;IAFX,YACW,UAAkB,EAClB,qBAA6B;QAEtC,KAAK,CAAC,sBAAsB,UAAU,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;QAHtD,eAAU,GAAV,UAAU,CAAQ;QAClB,0BAAq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CAAC;IAC1C,OAAO;QACL,GAAG,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,KAAK,EAAE;QAClC,QAAQ;QACR,QAAQ;QACR,OAAO,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU;QACnD,YAAY,OAAO,CAAC,QAAQ,EAAE;QAC9B,WAAW,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE;KAC3C;SACE,MAAM,CAAC,CAAC,KAAK,EAAmB,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;SAClD,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,wBAAwB,CACtC,OAAgB,EAChB,cAAsB;IAEtB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAC3C,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACrC,IACE,CAAC,IAAI,IAAI,IAAI,KAAK,aAAa,IAAI,IAAI,KAAK,MAAM,CAAC;QACnD,MAAM,KAAK,cAAc;QAEzB,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const tenantAccountSettingsAudience = "urn:ctx9:tenant-account-settings";
|
|
3
|
+
declare const methodSchema: z.ZodEnum<{
|
|
4
|
+
GET: "GET";
|
|
5
|
+
POST: "POST";
|
|
6
|
+
PATCH: "PATCH";
|
|
7
|
+
DELETE: "DELETE";
|
|
8
|
+
}>;
|
|
9
|
+
export type TenantAccountSettingsMethod = z.infer<typeof methodSchema>;
|
|
10
|
+
export interface TenantAccountSettingsAssertion {
|
|
11
|
+
clientId: string;
|
|
12
|
+
userId: string;
|
|
13
|
+
tenantId: string;
|
|
14
|
+
membershipVersion: number;
|
|
15
|
+
requestId: string;
|
|
16
|
+
}
|
|
17
|
+
export declare class TenantAccountSettingsRequestError extends Error {
|
|
18
|
+
readonly status: number;
|
|
19
|
+
readonly safeCode: string;
|
|
20
|
+
constructor(status: number, safeCode: string);
|
|
21
|
+
}
|
|
22
|
+
/** Creates a short-lived assertion bound to one server-to-server request. */
|
|
23
|
+
export declare function createTenantAccountSettingsAssertion({ clientId, clientSecret, userId, tenantId, membershipVersion, method, path, body, now, }: {
|
|
24
|
+
clientId: string;
|
|
25
|
+
clientSecret: string;
|
|
26
|
+
userId: string;
|
|
27
|
+
tenantId: string;
|
|
28
|
+
membershipVersion: number;
|
|
29
|
+
method: TenantAccountSettingsMethod;
|
|
30
|
+
path: `/internal/settings${string}`;
|
|
31
|
+
body?: string;
|
|
32
|
+
now?: Date;
|
|
33
|
+
}): Promise<string>;
|
|
34
|
+
/** Verifies the caller, expiry, and exact method/path/body request binding. */
|
|
35
|
+
export declare function verifyTenantAccountSettingsAssertion({ token, method, path, body, resolveClientSecret, now, }: {
|
|
36
|
+
token: string;
|
|
37
|
+
method: TenantAccountSettingsMethod;
|
|
38
|
+
path: `/internal/settings${string}`;
|
|
39
|
+
body?: string;
|
|
40
|
+
resolveClientSecret: (clientId: string) => string | undefined;
|
|
41
|
+
now?: Date;
|
|
42
|
+
}): Promise<TenantAccountSettingsAssertion>;
|
|
43
|
+
/** Sends one assertion-authenticated request from a product BFF to Platform. */
|
|
44
|
+
export declare function requestTenantAccountSettings({ origin, clientId, clientSecret, userId, tenantId, membershipVersion, method, path, body, request, }: {
|
|
45
|
+
origin: string;
|
|
46
|
+
clientId: string;
|
|
47
|
+
clientSecret: string;
|
|
48
|
+
userId: string;
|
|
49
|
+
tenantId: string;
|
|
50
|
+
membershipVersion: number;
|
|
51
|
+
method: TenantAccountSettingsMethod;
|
|
52
|
+
path: `/internal/settings${string}`;
|
|
53
|
+
body?: unknown;
|
|
54
|
+
request?: typeof fetch;
|
|
55
|
+
}): Promise<unknown>;
|
|
56
|
+
export {};
|
|
57
|
+
//# s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AC/E,wBAAsB,oCAAoC,CAAC,EACzD,KAAK,EACL,MAAM,EACN,IAAI,EACJ,IAAS,EACT,mBAAmB,EACnB,GAAgB,GACjB,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,2BAA2B,CAAC;IACpC,IAAI,EAAE,qBAAqB,MAAM,EAAE,CAAC;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mBAAmB,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IAC9D,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ,GAAG,OAAO,CAAC,8BAA8B,CAAC,CA4B1C;AAED,gFAAgF;AAChF,wBAAsB,4BAA4B,CAAC,EACjD,MAAM,EACN,QAAQ,EACR,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,iBAAiB,EACjB,MAAM,EACN,IAAI,EACJ,IAAI,EACJ,OAAe,GAChB,EAAE;IACD,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,MAAM,EAAE,2BAA2B,CAAC;IACpC,IAAI,EAAE,qBAAqB,MAAM,EAAE,CAAC;IACpC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;CACxB,GAAG,OAAO,CAAC,OAAO,CAAC,CAqCnB"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { decodeJwt, jwtVerify, SignJWT } from "jose";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
export const tenantAccountSettingsAudience = "urn:ctx9:tenant-account-settings";
|
|
5
|
+
const methodSchema = z.enum(["GET", "POST", "PATCH", "DELETE"]);
|
|
6
|
+
const assertionSchema = z.object({
|
|
7
|
+
iss: z.string().min(1).max(128),
|
|
8
|
+
sub: z.string().min(1).max(256),
|
|
9
|
+
aud: z.union([
|
|
10
|
+
z.literal(tenantAccountSettingsAudience),
|
|
11
|
+
z.array(z.literal(tenantAccountSettingsAudience)).min(1),
|
|
12
|
+
]),
|
|
13
|
+
iat: z.number().int(),
|
|
14
|
+
exp: z.number().int(),
|
|
15
|
+
jti: z.uuid(),
|
|
16
|
+
schema_version: z.literal(1),
|
|
17
|
+
tenant_id: z.uuid(),
|
|
18
|
+
membership_version: z.number().int().positive(),
|
|
19
|
+
method: methodSchema,
|
|
20
|
+
path: z.string().min(1).max(512).startsWith("/internal/settings"),
|
|
21
|
+
body_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
|
22
|
+
});
|
|
23
|
+
export class TenantAccountSettingsRequestError extends Error {
|
|
24
|
+
status;
|
|
25
|
+
safeCode;
|
|
26
|
+
constructor(status, safeCode) {
|
|
27
|
+
super("tenant-account-settings-request-failed");
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.safeCode = safeCode;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function digestBody(body) {
|
|
33
|
+
return createHash("sha256").update(body).digest("hex");
|
|
34
|
+
}
|
|
35
|
+
function signingKey(secret) {
|
|
36
|
+
if (Buffer.byteLength(secret, "utf8") < 32)
|
|
37
|
+
throw new Error("settings-client-secret-too-short");
|
|
38
|
+
return new TextEncoder().encode(secret);
|
|
39
|
+
}
|
|
40
|
+
/** Creates a short-lived assertion bound to one server-to-server request. */
|
|
41
|
+
export async function createTenantAccountSettingsAssertion({ clientId, clientSecret, userId, tenantId, membershipVersion, method, path, body = "", now = new Date(), }) {
|
|
42
|
+
const issuedAt = Math.floor(now.getTime() / 1000);
|
|
43
|
+
return new SignJWT({
|
|
44
|
+
schema_version: 1,
|
|
45
|
+
tenant_id: tenantId,
|
|
46
|
+
membership_version: membershipVersion,
|
|
47
|
+
method,
|
|
48
|
+
path,
|
|
49
|
+
body_sha256: digestBody(body),
|
|
50
|
+
})
|
|
51
|
+
.setProtectedHeader({ alg: "HS256", typ: "JWT" })
|
|
52
|
+
.setIssuer(clientId)
|
|
53
|
+
.setSubject(userId)
|
|
54
|
+
.setAudience(tenantAccountSettingsAudience)
|
|
55
|
+
.setIssuedAt(issuedAt)
|
|
56
|
+
.setExpirationTime(issuedAt + 30)
|
|
57
|
+
.setJti(randomUUID())
|
|
58
|
+
.sign(signingKey(clientSecret));
|
|
59
|
+
}
|
|
60
|
+
/** Verifies the caller, expiry, and exact method/path/body request binding. */
|
|
61
|
+
export async function verifyTenantAccountSettingsAssertion({ token, method, path, body = "", resolveClientSecret, now = new Date(), }) {
|
|
62
|
+
const decoded = decodeJwt(token);
|
|
63
|
+
if (typeof decoded.iss !== "string")
|
|
64
|
+
throw new Error("settings-assertion-issuer-required");
|
|
65
|
+
const secret = resolveClientSecret(decoded.iss);
|
|
66
|
+
if (!secret)
|
|
67
|
+
throw new Error("settings-assertion-client-denied");
|
|
68
|
+
const verified = await jwtVerify(token, signingKey(secret), {
|
|
69
|
+
algorithms: ["HS256"],
|
|
70
|
+
audience: tenantAccountSettingsAudience,
|
|
71
|
+
issuer: decoded.iss,
|
|
72
|
+
currentDate: now,
|
|
73
|
+
clockTolerance: 2,
|
|
74
|
+
});
|
|
75
|
+
const assertion = assertionSchema.parse(verified.payload);
|
|
76
|
+
if (assertion.method !== method ||
|
|
77
|
+
assertion.path !== path ||
|
|
78
|
+
assertion.body_sha256 !== digestBody(body)) {
|
|
79
|
+
throw new Error("settings-assertion-request-mismatch");
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
clientId: assertion.iss,
|
|
83
|
+
userId: assertion.sub,
|
|
84
|
+
tenantId: assertion.tenant_id,
|
|
85
|
+
membershipVersion: assertion.membership_version,
|
|
86
|
+
requestId: assertion.jti,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** Sends one assertion-authenticated request from a product BFF to Platform. */
|
|
90
|
+
export async function requestTenantAccountSettings({ origin, clientId, clientSecret, userId, tenantId, membershipVersion, method, path, body, request = fetch, }) {
|
|
91
|
+
const serializedBody = body === undefined ? "" : JSON.stringify(body);
|
|
92
|
+
const assertion = await createTenantAccountSettingsAssertion({
|
|
93
|
+
clientId,
|
|
94
|
+
clientSecret,
|
|
95
|
+
userId,
|
|
96
|
+
tenantId,
|
|
97
|
+
membershipVersion,
|
|
98
|
+
method,
|
|
99
|
+
path,
|
|
100
|
+
body: serializedBody,
|
|
101
|
+
});
|
|
102
|
+
const response = await request(new URL(path, origin), {
|
|
103
|
+
method,
|
|
104
|
+
redirect: "error",
|
|
105
|
+
headers: {
|
|
106
|
+
authorization: `Bearer ${assertion}`,
|
|
107
|
+
...(body === undefined
|
|
108
|
+
? {}
|
|
109
|
+
: { "content-type": "application/json; charset=utf-8" }),
|
|
110
|
+
},
|
|
111
|
+
...(body === undefined ? {} : { body: serializedBody }),
|
|
112
|
+
});
|
|
113
|
+
if (!response.ok) {
|
|
114
|
+
const failure = await response.json().catch(() => undefined);
|
|
115
|
+
const safeCode = failure &&
|
|
116
|
+
typeof failure === "object" &&
|
|
117
|
+
"error" in failure &&
|
|
118
|
+
typeof failure.error === "string" &&
|
|
119
|
+
/^[a-z0-9-]{1,80}$/u.test(failure.error)
|
|
120
|
+
? failure.error
|
|
121
|
+
: "unknown";
|
|
122
|
+
throw new TenantAccountSettingsRequestError(response.status, safeCode);
|
|
123
|
+
}
|
|
124
|
+
if (response.status === 204)
|
|
125
|
+
return undefined;
|
|
126
|
+
return response.json();
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=settings-gateway.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settings-gateway.js","sourceRoot":"","sources":["../src/settings-gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,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;QACjB,MAAM,OAAO,GAAY,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QACtE,MAAM,QAAQ,GACZ,OAAO;YACP,OAAO,OAAO,KAAK,QAAQ;YAC3B,OAAO,IAAI,OAAO;YAClB,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ;YACjC,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YACtC,CAAC,CAAC,OAAO,CAAC,KAAK;YACf,CAAC,CAAC,SAAS,CAAC;QAChB,MAAM,IAAI,iCAAiC,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACzE,CAAC;IACD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG;QAAE,OAAO,SAAS,CAAC;IAC9C,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;AACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ctx9/web-auth",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "9.0.1",
|
|
4
|
+
"description": "Shared CTX9 confidential OIDC BFF protocol",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/ContextNine/ctx9-web.git",
|
|
9
|
+
"directory": "packages/web-auth"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"NOTICE"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./express": {
|
|
24
|
+
"types": "./dist/adapters/express.d.ts",
|
|
25
|
+
"default": "./dist/adapters/express.js"
|
|
26
|
+
},
|
|
27
|
+
"./node": {
|
|
28
|
+
"types": "./dist/adapters/node.d.ts",
|
|
29
|
+
"default": "./dist/adapters/node.js"
|
|
30
|
+
},
|
|
31
|
+
"./tanstack-start": {
|
|
32
|
+
"types": "./dist/adapters/tanstack-start.d.ts",
|
|
33
|
+
"default": "./dist/adapters/tanstack-start.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"registry": "https://registry.npmjs.org/"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": "^22.16.0 || ^24.21.0 || ^26.11.1"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"jose": "6.2.8",
|
|
45
|
+
"zod": "4.4.3"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "pnpm clean && tsc -p tsconfig.build.json",
|
|
49
|
+
"clean": "rm -rf dist",
|
|
50
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
51
|
+
}
|
|
6
52
|
}
|