soulsync 1.0.18 → 1.0.20
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 +7 -6
- package/package.json +1 -1
- package/src/oauth-server.js +23 -17
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const https = require('https');
|
|
|
5
5
|
const http = require('http');
|
|
6
6
|
const crypto = require('crypto');
|
|
7
7
|
const os = require('os');
|
|
8
|
+
const { createRequire } = require('module');
|
|
8
9
|
|
|
9
10
|
const { getDeviceName, loadConfig, saveConfig, isAuthenticated } = require('./src/config');
|
|
10
11
|
|
|
@@ -90,21 +91,21 @@ async function startOAuthLocal() {
|
|
|
90
91
|
const { createOAuthServer } = require('./src/oauth-server');
|
|
91
92
|
|
|
92
93
|
try {
|
|
93
|
-
const { server, port } = await createOAuthServer();
|
|
94
|
+
const { server, port, waitForToken } = await createOAuthServer();
|
|
94
95
|
const callbackUrl = `http://localhost:${port}/callback`;
|
|
95
96
|
const state = crypto.randomBytes(16).toString('hex');
|
|
96
97
|
const authUrl = `${getCloudUrl()}/auth/oauth/start?port=${port}&callback=${encodeURIComponent(callbackUrl)}&state=${state}`;
|
|
97
98
|
|
|
98
99
|
console.log('[SoulSync] Opening browser for authorization...');
|
|
99
100
|
|
|
100
|
-
const
|
|
101
|
+
const require2 = createRequire(__filename);
|
|
102
|
+
const openPath = require2.resolve('open');
|
|
103
|
+
const open = (await import(openPath)).default;
|
|
101
104
|
await open(authUrl);
|
|
102
105
|
|
|
103
106
|
console.log('[SoulSync] Waiting for authorization...');
|
|
104
|
-
|
|
105
|
-
const { token } = await
|
|
106
|
-
server.on('close', () => reject(new Error('Server closed without receiving token')));
|
|
107
|
-
});
|
|
107
|
+
|
|
108
|
+
const { token } = await waitForToken();
|
|
108
109
|
|
|
109
110
|
const deviceId = crypto.randomUUID();
|
|
110
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
|
}
|