soulsync 1.0.19 → 1.0.21
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/index.js +3 -5
- package/package.json +1 -1
- package/src/oauth-server.js +23 -17
package/index.js
CHANGED
|
@@ -91,7 +91,7 @@ async function startOAuthLocal() {
|
|
|
91
91
|
const { createOAuthServer } = require('./src/oauth-server');
|
|
92
92
|
|
|
93
93
|
try {
|
|
94
|
-
const { server, port } = await createOAuthServer();
|
|
94
|
+
const { server, port, waitForToken } = await createOAuthServer();
|
|
95
95
|
const callbackUrl = `http://localhost:${port}/callback`;
|
|
96
96
|
const state = crypto.randomBytes(16).toString('hex');
|
|
97
97
|
const authUrl = `${getCloudUrl()}/auth/oauth/start?port=${port}&callback=${encodeURIComponent(callbackUrl)}&state=${state}`;
|
|
@@ -104,10 +104,8 @@ async function startOAuthLocal() {
|
|
|
104
104
|
await open(authUrl);
|
|
105
105
|
|
|
106
106
|
console.log('[SoulSync] Waiting for authorization...');
|
|
107
|
-
|
|
108
|
-
const { token } = await
|
|
109
|
-
server.on('close', () => reject(new Error('Server closed without receiving token')));
|
|
110
|
-
});
|
|
107
|
+
|
|
108
|
+
const { token } = await waitForToken();
|
|
111
109
|
|
|
112
110
|
const deviceId = crypto.randomUUID();
|
|
113
111
|
const deviceName = getDeviceName('local');
|
package/package.json
CHANGED
package/src/oauth-server.js
CHANGED
|
@@ -3,44 +3,50 @@ const url = require('url');
|
|
|
3
3
|
|
|
4
4
|
function createOAuthServer() {
|
|
5
5
|
return new Promise((resolve, reject) => {
|
|
6
|
+
let tokenResolver = null;
|
|
7
|
+
|
|
6
8
|
const server = http.createServer((req, res) => {
|
|
7
9
|
const parsedUrl = url.parse(req.url, true);
|
|
8
|
-
|
|
10
|
+
|
|
9
11
|
if (parsedUrl.pathname === '/callback') {
|
|
10
12
|
const { token, state, error } = parsedUrl.query;
|
|
11
|
-
|
|
13
|
+
|
|
12
14
|
if (error) {
|
|
13
|
-
res.writeHead(400, { 'Content-Type': 'text/html' });
|
|
15
|
+
res.writeHead(400, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
14
16
|
res.end('<html><body><h1>授权失败</h1><p>请关闭此窗口并重试。</p></body></html>');
|
|
15
|
-
|
|
16
|
-
reject(new Error(error));
|
|
17
|
+
if (tokenResolver) tokenResolver.reject(new Error(error));
|
|
17
18
|
return;
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
+
|
|
20
21
|
if (token) {
|
|
21
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
22
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
22
23
|
res.end('<html><body><h1>授权成功!</h1><p>请关闭此窗口并返回终端。</p></body></html>');
|
|
23
|
-
|
|
24
|
-
resolve({ token, state });
|
|
24
|
+
if (tokenResolver) tokenResolver.resolve({ token, state });
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
res.writeHead(400, { 'Content-Type': 'text/html' });
|
|
27
|
+
|
|
28
|
+
res.writeHead(400, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
29
29
|
res.end('<html><body><h1>无效响应</h1></body></html>');
|
|
30
|
-
|
|
31
|
-
reject(new Error('No token received'));
|
|
30
|
+
if (tokenResolver) tokenResolver.reject(new Error('No token received'));
|
|
32
31
|
return;
|
|
33
32
|
}
|
|
34
|
-
|
|
33
|
+
|
|
35
34
|
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
36
35
|
res.end('Not Found');
|
|
37
36
|
});
|
|
38
|
-
|
|
37
|
+
|
|
39
38
|
server.listen(0, () => {
|
|
40
39
|
const port = server.address().port;
|
|
41
|
-
|
|
40
|
+
|
|
41
|
+
resolve({
|
|
42
|
+
server,
|
|
43
|
+
port,
|
|
44
|
+
waitForToken: () => new Promise((res, rej) => {
|
|
45
|
+
tokenResolver = { resolve: res, reject: rej };
|
|
46
|
+
})
|
|
47
|
+
});
|
|
42
48
|
});
|
|
43
|
-
|
|
49
|
+
|
|
44
50
|
server.on('error', reject);
|
|
45
51
|
});
|
|
46
52
|
}
|