@tutti-os/auth-bridge 0.0.1 → 0.0.49
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/README.md +21 -3
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +29 -20
- package/dist/{chunk-B7N5AC4Q.js → chunk-M2P3SN64.js} +3 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.js +216 -54
- package/dist/{shared-Bm-pnst6.d.ts → shared-CPA5wTKA.d.ts} +1 -1
- package/package.json +6 -1
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/README.md
CHANGED
|
@@ -5,7 +5,7 @@ High-level Tutti auth helpers for browser apps and Node/Electron clients.
|
|
|
5
5
|
## Browser
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
|
-
import { createTuttiBrowserAuthClient } from
|
|
8
|
+
import { createTuttiBrowserAuthClient } from "@tutti-os/auth-bridge/browser";
|
|
9
9
|
|
|
10
10
|
const auth = createTuttiBrowserAuthClient();
|
|
11
11
|
|
|
@@ -17,12 +17,30 @@ await auth.logout();
|
|
|
17
17
|
## Node
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
|
-
import { createTuttiNodeAuthClient } from
|
|
20
|
+
import { createTuttiNodeAuthClient } from "@tutti-os/auth-bridge/node";
|
|
21
21
|
|
|
22
22
|
const auth = createTuttiNodeAuthClient({
|
|
23
23
|
authJsonPath,
|
|
24
|
-
appCallbackUrl
|
|
24
|
+
appCallbackUrl
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
const { session, user } = await auth.login();
|
|
28
28
|
```
|
|
29
|
+
|
|
30
|
+
## Local Desktop Callback
|
|
31
|
+
|
|
32
|
+
The Node bridge starts a loopback HTTP server for desktop sign-in. New desktop OAuth completions use:
|
|
33
|
+
|
|
34
|
+
- `GET /oauth/callback?state=<base64url>&transfer_code=<code>`
|
|
35
|
+
- `GET /oauth/callback?state=<base64url>&error=<providerError>`
|
|
36
|
+
|
|
37
|
+
The callback validates the signed login state, redeems the desktop transfer code through the account service, writes `auth.json`, fetches user info, and redirects the browser back to the web result page:
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
/auth/login/callback?desktopBridgeStatus=success&openAppUrl=<safe-deeplink>
|
|
41
|
+
/auth/login/callback?desktopBridgeStatus=error&desktopBridgeError=<safeCode>&openAppUrl=<safe-deeplink>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`openAppUrl` is only used to focus or reopen Tutti. It is sanitized to the allowed app schemes and must not carry `transfer_code`, sessions, or tokens.
|
|
45
|
+
|
|
46
|
+
`/oauth/health` and `POST /oauth/complete` remain available for compatibility with older web clients, but the current web flow redirects the browser to `/oauth/callback` instead of fetching the local bridge.
|
package/dist/browser.d.ts
CHANGED
package/dist/browser.js
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
buildAccountUrl,
|
|
6
6
|
mapUserInfo,
|
|
7
7
|
readEnvelopeError
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-M2P3SN64.js";
|
|
9
9
|
|
|
10
10
|
// src/browser.ts
|
|
11
11
|
function defaultOpenUrl(url) {
|
|
@@ -25,22 +25,28 @@ function createTuttiBrowserAuthClient(options = {}) {
|
|
|
25
25
|
return {
|
|
26
26
|
login(loginOptions = {}) {
|
|
27
27
|
const url = new URL(authLoginUrl);
|
|
28
|
-
url.searchParams.set(
|
|
28
|
+
url.searchParams.set(
|
|
29
|
+
"redirect_uri",
|
|
30
|
+
loginOptions.redirectUri ?? resolveCurrentRedirectUri()
|
|
31
|
+
);
|
|
29
32
|
if (loginOptions.locale) {
|
|
30
33
|
url.searchParams.set("locale", loginOptions.locale);
|
|
31
34
|
}
|
|
32
35
|
openUrl(url.toString());
|
|
33
36
|
},
|
|
34
37
|
async getUserInfo() {
|
|
35
|
-
const response = await fetch(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
const response = await fetch(
|
|
39
|
+
buildAccountUrl(accountBaseUrl, "/user/v1/user_info"),
|
|
40
|
+
{
|
|
41
|
+
method: "POST",
|
|
42
|
+
credentials: "include",
|
|
43
|
+
headers: {
|
|
44
|
+
Accept: "application/json",
|
|
45
|
+
"Content-Type": "application/json"
|
|
46
|
+
},
|
|
47
|
+
body: JSON.stringify({})
|
|
48
|
+
}
|
|
49
|
+
);
|
|
44
50
|
const payload = await readJsonEnvelope(response);
|
|
45
51
|
if (response.status === 401 || payload?.code === 401) {
|
|
46
52
|
return null;
|
|
@@ -51,15 +57,18 @@ function createTuttiBrowserAuthClient(options = {}) {
|
|
|
51
57
|
return mapUserInfo(payload.data);
|
|
52
58
|
},
|
|
53
59
|
async logout() {
|
|
54
|
-
const response = await fetch(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
const response = await fetch(
|
|
61
|
+
buildAccountUrl(accountBaseUrl, "/auth/v1/logout-web-session"),
|
|
62
|
+
{
|
|
63
|
+
method: "POST",
|
|
64
|
+
credentials: "include",
|
|
65
|
+
headers: {
|
|
66
|
+
Accept: "application/json",
|
|
67
|
+
"Content-Type": "application/json"
|
|
68
|
+
},
|
|
69
|
+
body: JSON.stringify({ appId })
|
|
70
|
+
}
|
|
71
|
+
);
|
|
63
72
|
const payload = await readJsonEnvelope(response);
|
|
64
73
|
if (!response.ok || (payload?.code ?? 0) !== 0) {
|
|
65
74
|
throw readEnvelopeError(response, payload);
|
|
@@ -20,7 +20,9 @@ function buildAccountUrl(accountBaseUrl, path) {
|
|
|
20
20
|
return `${normalizeBaseUrl(accountBaseUrl)}/${path.replace(/^\/+/u, "")}`;
|
|
21
21
|
}
|
|
22
22
|
function readEnvelopeError(response, payload) {
|
|
23
|
-
return new Error(
|
|
23
|
+
return new Error(
|
|
24
|
+
payload?.errmsg ?? payload?.message ?? `Request failed with status ${response.status}`
|
|
25
|
+
);
|
|
24
26
|
}
|
|
25
27
|
function mapUserInfo(data) {
|
|
26
28
|
if (!data) {
|
package/dist/node.d.ts
CHANGED
package/dist/node.js
CHANGED
|
@@ -12,13 +12,15 @@ import {
|
|
|
12
12
|
mapUserInfo,
|
|
13
13
|
readEnvelopeError,
|
|
14
14
|
trimString
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-M2P3SN64.js";
|
|
16
16
|
|
|
17
17
|
// src/node.ts
|
|
18
18
|
import { spawn } from "child_process";
|
|
19
19
|
import { randomUUID } from "crypto";
|
|
20
20
|
import { mkdir, readFile, rename, rm, writeFile } from "fs/promises";
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
createServer
|
|
23
|
+
} from "http";
|
|
22
24
|
import { createServer as createNetServer } from "net";
|
|
23
25
|
import { hostname } from "os";
|
|
24
26
|
import { dirname } from "path";
|
|
@@ -36,8 +38,14 @@ function createTuttiNodeAuthClient(options) {
|
|
|
36
38
|
const accountBaseUrl = options.accountBaseUrl?.trim() || DEFAULT_ACCOUNT_BASE_URL;
|
|
37
39
|
const authLoginUrl = options.authLoginUrl?.trim() || DEFAULT_AUTH_LOGIN_URL;
|
|
38
40
|
const openUrl = options.openUrl ?? openUrlWithDefaultBrowser;
|
|
39
|
-
const idleTimeoutMs = positiveMs(
|
|
40
|
-
|
|
41
|
+
const idleTimeoutMs = positiveMs(
|
|
42
|
+
options.loginIdleTimeoutMs,
|
|
43
|
+
DEFAULT_LOGIN_IDLE_TIMEOUT_MS
|
|
44
|
+
);
|
|
45
|
+
const maxTimeoutMs = Math.max(
|
|
46
|
+
idleTimeoutMs,
|
|
47
|
+
positiveMs(options.loginMaxTimeoutMs, DEFAULT_LOGIN_MAX_TIMEOUT_MS)
|
|
48
|
+
);
|
|
41
49
|
async function readSession() {
|
|
42
50
|
return await readAuthJson(authJsonPath);
|
|
43
51
|
}
|
|
@@ -81,6 +89,7 @@ function createTuttiNodeAuthClient(options) {
|
|
|
81
89
|
accountBaseUrl,
|
|
82
90
|
appId,
|
|
83
91
|
appCallbackUrl,
|
|
92
|
+
authJsonPath,
|
|
84
93
|
attemptId,
|
|
85
94
|
authOrigin: new URL(authLoginUrl).origin,
|
|
86
95
|
bridgeToken,
|
|
@@ -99,14 +108,10 @@ function createTuttiNodeAuthClient(options) {
|
|
|
99
108
|
try {
|
|
100
109
|
await openUrl(loginUrl);
|
|
101
110
|
const transferCode = await completion;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (!user) {
|
|
105
|
-
throw new Error("Failed to load user info after login");
|
|
111
|
+
if (pending.completedResult) {
|
|
112
|
+
return pending.completedResult;
|
|
106
113
|
}
|
|
107
|
-
|
|
108
|
-
await writeAuthJson(authJsonPath, session);
|
|
109
|
-
return { session, user };
|
|
114
|
+
return await completePendingLogin(pending, transferCode);
|
|
110
115
|
} finally {
|
|
111
116
|
await closeServer(bridge.server);
|
|
112
117
|
}
|
|
@@ -156,7 +161,10 @@ async function writeAuthJson(authJsonPath, session) {
|
|
|
156
161
|
};
|
|
157
162
|
const tempPath = `${authJsonPath}.tmp-${process.pid}-${Date.now()}-${randomUUID()}`;
|
|
158
163
|
try {
|
|
159
|
-
await writeFile(tempPath, JSON.stringify(payload, null, 2), {
|
|
164
|
+
await writeFile(tempPath, JSON.stringify(payload, null, 2), {
|
|
165
|
+
encoding: "utf8",
|
|
166
|
+
mode: 384
|
|
167
|
+
});
|
|
160
168
|
await rename(tempPath, authJsonPath);
|
|
161
169
|
} catch (error) {
|
|
162
170
|
await rm(tempPath, { force: true }).catch(() => void 0);
|
|
@@ -168,7 +176,9 @@ function encodeBridgeState(state) {
|
|
|
168
176
|
}
|
|
169
177
|
function decodeBridgeState(rawState) {
|
|
170
178
|
try {
|
|
171
|
-
const parsed = JSON.parse(
|
|
179
|
+
const parsed = JSON.parse(
|
|
180
|
+
Buffer.from(rawState, "base64url").toString("utf8")
|
|
181
|
+
);
|
|
172
182
|
if (parsed.v !== 1 || parsed.flow !== "desktop_bridge" || !trimString(parsed.attemptId) || !trimString(parsed.localServerOrigin) || !trimString(parsed.bridgeToken)) {
|
|
173
183
|
return null;
|
|
174
184
|
}
|
|
@@ -218,16 +228,82 @@ async function createLoginBridgeServer(input, port) {
|
|
|
218
228
|
if (req.method === "GET" && url.pathname === "/oauth/health") {
|
|
219
229
|
const matched = !input.completed && Date.now() <= input.expiresAt && Date.now() <= input.maxExpiresAt && url.searchParams.get("attempt_id") === input.attemptId && url.searchParams.get("token") === input.bridgeToken;
|
|
220
230
|
if (!matched) {
|
|
221
|
-
sendJson(res, 401, {
|
|
231
|
+
sendJson(res, 401, {
|
|
232
|
+
ok: false,
|
|
233
|
+
error: {
|
|
234
|
+
code: "INVALID_BRIDGE_ATTEMPT",
|
|
235
|
+
message: "Desktop login attempt is unavailable."
|
|
236
|
+
}
|
|
237
|
+
});
|
|
222
238
|
return;
|
|
223
239
|
}
|
|
224
|
-
input.expiresAt = Math.min(
|
|
225
|
-
|
|
240
|
+
input.expiresAt = Math.min(
|
|
241
|
+
input.maxExpiresAt,
|
|
242
|
+
Date.now() + input.idleTimeoutMs
|
|
243
|
+
);
|
|
244
|
+
sendJson(res, 200, {
|
|
245
|
+
ok: true,
|
|
246
|
+
data: {
|
|
247
|
+
attemptId: input.attemptId,
|
|
248
|
+
status: "ready",
|
|
249
|
+
expiresAt: input.expiresAt
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (req.method === "GET" && url.pathname === "/oauth/callback") {
|
|
255
|
+
if (!isAllowedLoopbackHost(req, port)) {
|
|
256
|
+
res.writeHead(403);
|
|
257
|
+
res.end();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const callbackError = trimString(url.searchParams.get("error"));
|
|
261
|
+
const callbackState = trimString(url.searchParams.get("state"));
|
|
262
|
+
const transferCode = trimString(url.searchParams.get("transfer_code"));
|
|
263
|
+
if (!stateMatches(input, callbackState)) {
|
|
264
|
+
const error = new Error("Invalid state");
|
|
265
|
+
complete(() => rejectCompletion(error));
|
|
266
|
+
sendRedirect(res, buildBridgeResultUrl(input, "error", "invalidState"));
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (callbackError) {
|
|
270
|
+
const error = new Error(callbackError);
|
|
271
|
+
complete(() => rejectCompletion(error));
|
|
272
|
+
sendRedirect(
|
|
273
|
+
res,
|
|
274
|
+
buildBridgeResultUrl(input, "error", "providerError")
|
|
275
|
+
);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (!transferCode) {
|
|
279
|
+
const error = new Error("Missing transfer_code");
|
|
280
|
+
complete(() => rejectCompletion(error));
|
|
281
|
+
sendRedirect(
|
|
282
|
+
res,
|
|
283
|
+
buildBridgeResultUrl(input, "error", "missingTransferCode")
|
|
284
|
+
);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
input.completedResult = await completePendingLogin(input, transferCode);
|
|
289
|
+
input.completed = true;
|
|
290
|
+
complete(() => resolveCompletion(transferCode));
|
|
291
|
+
sendRedirect(res, buildBridgeResultUrl(input, "success"));
|
|
292
|
+
} catch (error) {
|
|
293
|
+
complete(() => rejectCompletion(error));
|
|
294
|
+
sendRedirect(res, buildBridgeResultUrl(input, "error", "redeemFailed"));
|
|
295
|
+
}
|
|
226
296
|
return;
|
|
227
297
|
}
|
|
228
298
|
if (req.method === "POST" && url.pathname === "/oauth/complete") {
|
|
229
299
|
if (!isAllowedBridgeOrigin(req, input) || !isAllowedLoopbackHost(req, port)) {
|
|
230
|
-
sendJson(res, 403, {
|
|
300
|
+
sendJson(res, 403, {
|
|
301
|
+
ok: false,
|
|
302
|
+
error: {
|
|
303
|
+
code: "FORBIDDEN_BRIDGE_ORIGIN",
|
|
304
|
+
message: "Bridge origin is not allowed."
|
|
305
|
+
}
|
|
306
|
+
});
|
|
231
307
|
return;
|
|
232
308
|
}
|
|
233
309
|
const payload = await readJsonBody(req).catch(() => ({}));
|
|
@@ -236,19 +312,28 @@ async function createLoginBridgeServer(input, port) {
|
|
|
236
312
|
const transferCode = trimString(payload.transfer_code) || trimString(payload.transferCode);
|
|
237
313
|
if (!stateMatches(input, callbackState)) {
|
|
238
314
|
const error = new Error("Invalid state");
|
|
239
|
-
sendJson(res, 400, {
|
|
315
|
+
sendJson(res, 400, {
|
|
316
|
+
ok: false,
|
|
317
|
+
error: { code: "INVALID_STATE", message: error.message }
|
|
318
|
+
});
|
|
240
319
|
complete(() => rejectCompletion(error));
|
|
241
320
|
return;
|
|
242
321
|
}
|
|
243
322
|
if (callbackError) {
|
|
244
323
|
const error = new Error(callbackError);
|
|
245
|
-
sendJson(res, 400, {
|
|
324
|
+
sendJson(res, 400, {
|
|
325
|
+
ok: false,
|
|
326
|
+
error: { code: "PROVIDER_CALLBACK_ERROR", message: error.message }
|
|
327
|
+
});
|
|
246
328
|
complete(() => rejectCompletion(error));
|
|
247
329
|
return;
|
|
248
330
|
}
|
|
249
331
|
if (!transferCode) {
|
|
250
332
|
const error = new Error("Missing transfer_code");
|
|
251
|
-
sendJson(res, 400, {
|
|
333
|
+
sendJson(res, 400, {
|
|
334
|
+
ok: false,
|
|
335
|
+
error: { code: "MISSING_TRANSFER_CODE", message: error.message }
|
|
336
|
+
});
|
|
252
337
|
complete(() => rejectCompletion(error));
|
|
253
338
|
return;
|
|
254
339
|
}
|
|
@@ -257,7 +342,10 @@ async function createLoginBridgeServer(input, port) {
|
|
|
257
342
|
complete(() => resolveCompletion(transferCode));
|
|
258
343
|
return;
|
|
259
344
|
}
|
|
260
|
-
sendJson(res, 404, {
|
|
345
|
+
sendJson(res, 404, {
|
|
346
|
+
ok: false,
|
|
347
|
+
error: { code: "NOT_FOUND", message: "Not found" }
|
|
348
|
+
});
|
|
261
349
|
});
|
|
262
350
|
await new Promise((resolve, reject) => {
|
|
263
351
|
server.once("error", reject);
|
|
@@ -265,6 +353,19 @@ async function createLoginBridgeServer(input, port) {
|
|
|
265
353
|
});
|
|
266
354
|
return { server, waitForCompletion: waitForCompletion2 };
|
|
267
355
|
}
|
|
356
|
+
async function completePendingLogin(input, transferCode) {
|
|
357
|
+
const sessionId = await redeemDesktopTransferCode(input, transferCode);
|
|
358
|
+
const user = await fetchUserInfo(
|
|
359
|
+
input.accountBaseUrl,
|
|
360
|
+
buildSessionCookie(sessionId)
|
|
361
|
+
);
|
|
362
|
+
if (!user) {
|
|
363
|
+
throw new Error("Failed to load user info after login");
|
|
364
|
+
}
|
|
365
|
+
const session = sessionFromUser(sessionId, user);
|
|
366
|
+
await writeAuthJson(input.authJsonPath, session);
|
|
367
|
+
return { session, user };
|
|
368
|
+
}
|
|
268
369
|
async function waitForCompletion(input, completion) {
|
|
269
370
|
while (true) {
|
|
270
371
|
const remainingMs = Math.min(input.expiresAt, input.maxExpiresAt) - Date.now();
|
|
@@ -281,15 +382,18 @@ async function waitForCompletion(input, completion) {
|
|
|
281
382
|
}
|
|
282
383
|
}
|
|
283
384
|
async function fetchUserInfo(accountBaseUrl, cookie) {
|
|
284
|
-
const response = await fetch(
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
385
|
+
const response = await fetch(
|
|
386
|
+
buildAccountUrl(accountBaseUrl, "/user/v1/user_info"),
|
|
387
|
+
{
|
|
388
|
+
method: "POST",
|
|
389
|
+
headers: {
|
|
390
|
+
Accept: "application/json",
|
|
391
|
+
"Content-Type": "application/json",
|
|
392
|
+
Cookie: cookie
|
|
393
|
+
},
|
|
394
|
+
body: JSON.stringify({})
|
|
395
|
+
}
|
|
396
|
+
);
|
|
293
397
|
const payload = await response.json().catch(() => null);
|
|
294
398
|
if (response.status === 401 || payload?.code === 401) {
|
|
295
399
|
return null;
|
|
@@ -300,20 +404,26 @@ async function fetchUserInfo(accountBaseUrl, cookie) {
|
|
|
300
404
|
return mapUserInfo(payload.data);
|
|
301
405
|
}
|
|
302
406
|
async function redeemDesktopTransferCode(input, transferCode) {
|
|
303
|
-
const response = await fetch(
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
407
|
+
const response = await fetch(
|
|
408
|
+
buildAccountUrl(
|
|
409
|
+
input.accountBaseUrl,
|
|
410
|
+
"/auth/v1/redeem_desktop_transfer_code"
|
|
411
|
+
),
|
|
412
|
+
{
|
|
413
|
+
method: "POST",
|
|
414
|
+
headers: {
|
|
415
|
+
Accept: "application/json",
|
|
416
|
+
"Content-Type": "application/json"
|
|
417
|
+
},
|
|
418
|
+
body: JSON.stringify({
|
|
419
|
+
transfer_code: transferCode,
|
|
420
|
+
attempt_id: input.attemptId,
|
|
421
|
+
bridge_token: input.bridgeToken,
|
|
422
|
+
app_id: input.appId,
|
|
423
|
+
device_id: input.deviceId
|
|
424
|
+
})
|
|
425
|
+
}
|
|
426
|
+
);
|
|
317
427
|
const payload = await response.json().catch(() => null);
|
|
318
428
|
const sessionId = trimString(payload?.data?.sessionId) || trimString(payload?.data?.session_id);
|
|
319
429
|
if (!response.ok || payload?.code !== 0 || !sessionId) {
|
|
@@ -322,15 +432,18 @@ async function redeemDesktopTransferCode(input, transferCode) {
|
|
|
322
432
|
return sessionId;
|
|
323
433
|
}
|
|
324
434
|
async function logoutSession(accountBaseUrl, appId, cookie) {
|
|
325
|
-
const response = await fetch(
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
435
|
+
const response = await fetch(
|
|
436
|
+
buildAccountUrl(accountBaseUrl, "/auth/v1/logout-web-session"),
|
|
437
|
+
{
|
|
438
|
+
method: "POST",
|
|
439
|
+
headers: {
|
|
440
|
+
Accept: "application/json",
|
|
441
|
+
"Content-Type": "application/json",
|
|
442
|
+
Cookie: cookie
|
|
443
|
+
},
|
|
444
|
+
body: JSON.stringify({ appId })
|
|
445
|
+
}
|
|
446
|
+
);
|
|
334
447
|
const payload = await response.json().catch(() => null);
|
|
335
448
|
if (!response.ok || (payload?.code ?? 0) !== 0) {
|
|
336
449
|
throw readEnvelopeError(response, payload);
|
|
@@ -395,17 +508,66 @@ function isAllowedLoopbackHost(req, port) {
|
|
|
395
508
|
function sendJson(res, status, payload) {
|
|
396
509
|
res.writeHead(status, {
|
|
397
510
|
"Access-Control-Allow-Origin": "*",
|
|
511
|
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
512
|
+
"Access-Control-Allow-Headers": "Content-Type",
|
|
513
|
+
"Access-Control-Allow-Private-Network": "true",
|
|
398
514
|
"Content-Type": "application/json; charset=utf-8"
|
|
399
515
|
});
|
|
400
516
|
res.end(JSON.stringify(payload));
|
|
401
517
|
}
|
|
518
|
+
function sendRedirect(res, location) {
|
|
519
|
+
res.writeHead(302, { Location: location });
|
|
520
|
+
res.end();
|
|
521
|
+
}
|
|
522
|
+
function buildBridgeResultUrl(input, status, safeErrorCode) {
|
|
523
|
+
const url = new URL("/auth/login/callback", input.authOrigin);
|
|
524
|
+
url.searchParams.set("desktopBridgeStatus", status);
|
|
525
|
+
if (safeErrorCode) {
|
|
526
|
+
url.searchParams.set("desktopBridgeError", safeErrorCode);
|
|
527
|
+
}
|
|
528
|
+
const openAppUrl = buildSafeOpenAppUrl(
|
|
529
|
+
input.appCallbackUrl,
|
|
530
|
+
status,
|
|
531
|
+
safeErrorCode
|
|
532
|
+
);
|
|
533
|
+
if (openAppUrl) {
|
|
534
|
+
url.searchParams.set("openAppUrl", openAppUrl);
|
|
535
|
+
}
|
|
536
|
+
return url.toString();
|
|
537
|
+
}
|
|
538
|
+
function buildSafeOpenAppUrl(rawUrl, status, safeErrorCode) {
|
|
539
|
+
try {
|
|
540
|
+
const url = new URL(rawUrl.trim());
|
|
541
|
+
if (!isAllowedAppCallbackProtocol(url.protocol)) {
|
|
542
|
+
return null;
|
|
543
|
+
}
|
|
544
|
+
url.search = "";
|
|
545
|
+
url.hash = "";
|
|
546
|
+
url.searchParams.set("desktopBridgeStatus", status);
|
|
547
|
+
if (safeErrorCode) {
|
|
548
|
+
url.searchParams.set("desktopBridgeError", safeErrorCode);
|
|
549
|
+
}
|
|
550
|
+
return url.toString();
|
|
551
|
+
} catch {
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
function isAllowedAppCallbackProtocol(protocol) {
|
|
556
|
+
const legacyProtocol = `${DEFAULT_APP_ID}:`;
|
|
557
|
+
const legacyDevProtocol = `${DEFAULT_APP_ID}-dev:`;
|
|
558
|
+
return protocol === "tutti:" || protocol === "tutti-dev:" || protocol === legacyProtocol || protocol === legacyDevProtocol;
|
|
559
|
+
}
|
|
402
560
|
function closeServer(server) {
|
|
403
561
|
return new Promise((resolve) => server.close(() => resolve()));
|
|
404
562
|
}
|
|
405
563
|
function openUrlWithDefaultBrowser(url) {
|
|
406
564
|
const command = process.platform === "darwin" ? { file: "open", args: [url] } : process.platform === "win32" ? { file: "cmd", args: ["/c", "start", "", url] } : { file: "xdg-open", args: [url] };
|
|
407
565
|
return new Promise((resolve, reject) => {
|
|
408
|
-
const child = spawn(command.file, command.args, {
|
|
566
|
+
const child = spawn(command.file, command.args, {
|
|
567
|
+
detached: true,
|
|
568
|
+
stdio: "ignore",
|
|
569
|
+
windowsHide: true
|
|
570
|
+
});
|
|
409
571
|
child.once("error", reject);
|
|
410
572
|
child.once("spawn", () => {
|
|
411
573
|
child.unref();
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tutti-os/auth-bridge",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.49",
|
|
4
4
|
"private": false,
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/tutti-os/tutti.git",
|
|
8
|
+
"directory": "packages/auth/bridge"
|
|
9
|
+
},
|
|
5
10
|
"type": "module",
|
|
6
11
|
"types": "./dist/browser.d.ts",
|
|
7
12
|
"exports": {
|