moontraze 1.0.7 → 1.0.8
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/lib/auth.js +24 -93
- package/package.json +1 -1
package/lib/auth.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
const prompts = require('prompts');
|
|
2
2
|
const chalk = require('chalk');
|
|
3
|
-
const http = require('http');
|
|
4
|
-
const { URL } = require('url');
|
|
5
3
|
const { getConfig, setConfig } = require('./config');
|
|
6
4
|
|
|
7
5
|
async function login(tokenArg, options = {}) {
|
|
@@ -10,10 +8,8 @@ async function login(tokenArg, options = {}) {
|
|
|
10
8
|
process.env.MOON_API ||
|
|
11
9
|
getConfig().apiUrl ||
|
|
12
10
|
'https://api.moontraze.com';
|
|
13
|
-
|
|
14
11
|
const label = process.env.MOON_LABEL || process.env.MOON_PLATFORM || 'Moontraze';
|
|
15
12
|
|
|
16
|
-
// --token (hidden power user)
|
|
17
13
|
if (tokenArg && String(tokenArg).trim()) {
|
|
18
14
|
return saveAndValidateToken(String(tokenArg).trim(), { apiUrl, label });
|
|
19
15
|
}
|
|
@@ -72,16 +68,14 @@ async function loginWithEmail({ apiUrl, label }) {
|
|
|
72
68
|
});
|
|
73
69
|
|
|
74
70
|
const data = await res.json().catch(() => ({}));
|
|
75
|
-
|
|
76
71
|
if (!res.ok) throw new Error(data.error || 'Invalid email or password');
|
|
77
72
|
if (!data.token) throw new Error('Login succeeded but no token returned');
|
|
78
73
|
|
|
79
74
|
setConfig({ token: data.token, apiUrl });
|
|
80
|
-
|
|
81
75
|
console.log('');
|
|
82
76
|
console.log(chalk.green('✓ Logged in'));
|
|
83
|
-
if (data.user?.email) console.log(chalk.gray(`
|
|
84
|
-
console.log(chalk.gray(`
|
|
77
|
+
if (data.user?.email) console.log(chalk.gray(` ${data.user.email}`));
|
|
78
|
+
console.log(chalk.gray(` Config: ~/.moontraze/config.json`));
|
|
85
79
|
console.log('');
|
|
86
80
|
}
|
|
87
81
|
|
|
@@ -90,22 +84,21 @@ async function loginWithMoonID({ apiUrl, label }) {
|
|
|
90
84
|
const nonce = Math.random().toString(36).substring(2, 12);
|
|
91
85
|
const state = `${nonce}.login`;
|
|
92
86
|
|
|
93
|
-
|
|
87
|
+
// ★ Same flow as website (working)
|
|
88
|
+
const startRes = await fetch(`${apiUrl}/api/platform/moonid/web-start`, {
|
|
94
89
|
method: 'POST',
|
|
95
90
|
headers: { 'Content-Type': 'application/json' },
|
|
96
|
-
body: JSON.stringify({ state }),
|
|
91
|
+
body: JSON.stringify({ state, action: 'login' }),
|
|
97
92
|
});
|
|
98
93
|
const startData = await startRes.json().catch(() => ({}));
|
|
99
94
|
if (!startRes.ok) {
|
|
100
|
-
throw new Error(startData.error || 'Could not start MoonID
|
|
95
|
+
throw new Error(startData.error || 'Could not start MoonID session');
|
|
101
96
|
}
|
|
102
97
|
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// App POST yahan karegi (web jaisa)
|
|
108
|
-
const notifyUrl = `${apiUrl}/api/platform/moonid/cli-complete`;
|
|
98
|
+
const notifyUrl = `${apiUrl}/api/platform/moonid/web-complete`;
|
|
99
|
+
const redirectUri = `${apiUrl}/api/platform/moonid/done?state=${encodeURIComponent(
|
|
100
|
+
state
|
|
101
|
+
)}`;
|
|
109
102
|
|
|
110
103
|
const appId = 'com.moonstorage.app';
|
|
111
104
|
const deepLink =
|
|
@@ -133,13 +126,13 @@ async function loginWithMoonID({ apiUrl, label }) {
|
|
|
133
126
|
console.log(chalk.gray('Waiting for approval... (Ctrl+C to cancel)'));
|
|
134
127
|
console.log('');
|
|
135
128
|
|
|
136
|
-
const token = await
|
|
129
|
+
const token = await pollWebStatus({ apiUrl, state, timeoutMs: 180000 });
|
|
137
130
|
if (!token) throw new Error('MoonID login timed out or denied');
|
|
138
131
|
|
|
139
132
|
await saveAndValidateToken(token, { apiUrl, label });
|
|
140
133
|
}
|
|
141
134
|
|
|
142
|
-
async function
|
|
135
|
+
async function pollWebStatus({ apiUrl, state, timeoutMs }) {
|
|
143
136
|
const fetch = require('node-fetch');
|
|
144
137
|
const start = Date.now();
|
|
145
138
|
const interval = 2000;
|
|
@@ -148,19 +141,25 @@ async function pollCliStatus({ apiUrl, state, timeoutMs }) {
|
|
|
148
141
|
await new Promise((r) => setTimeout(r, interval));
|
|
149
142
|
try {
|
|
150
143
|
const res = await fetch(
|
|
151
|
-
`${apiUrl}/api/platform/moonid/
|
|
144
|
+
`${apiUrl}/api/platform/moonid/status?state=${encodeURIComponent(state)}`
|
|
152
145
|
);
|
|
153
146
|
const data = await res.json().catch(() => ({}));
|
|
154
147
|
|
|
155
|
-
|
|
148
|
+
// web endpoint returns status: "success"
|
|
149
|
+
if (
|
|
150
|
+
(data.status === 'success' || data.status === 'done') &&
|
|
151
|
+
data.token
|
|
152
|
+
) {
|
|
156
153
|
return data.token;
|
|
157
154
|
}
|
|
158
|
-
if (data.status === '
|
|
155
|
+
if (data.status === 'error' || data.status === 'denied') {
|
|
159
156
|
throw new Error(data.error || 'MoonID login denied');
|
|
160
157
|
}
|
|
161
|
-
// pending / unknown → keep waiting
|
|
162
158
|
} catch (e) {
|
|
163
|
-
if (
|
|
159
|
+
if (
|
|
160
|
+
e.message &&
|
|
161
|
+
/denied|failed|not found|mismatch|expired|signup/i.test(e.message)
|
|
162
|
+
) {
|
|
164
163
|
throw e;
|
|
165
164
|
}
|
|
166
165
|
}
|
|
@@ -168,86 +167,18 @@ async function pollCliStatus({ apiUrl, state, timeoutMs }) {
|
|
|
168
167
|
return null;
|
|
169
168
|
}
|
|
170
169
|
|
|
171
|
-
function waitForMoonIDCallback({ port, state, timeoutMs }) {
|
|
172
|
-
return new Promise((resolve) => {
|
|
173
|
-
const server = http.createServer((req, res) => {
|
|
174
|
-
try {
|
|
175
|
-
const u = new URL(req.url, `http://127.0.0.1:${port}`);
|
|
176
|
-
if (u.pathname !== '/callback') {
|
|
177
|
-
res.writeHead(404);
|
|
178
|
-
res.end('Not found');
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const returnedState = u.searchParams.get('state');
|
|
183
|
-
const token =
|
|
184
|
-
u.searchParams.get('token') ||
|
|
185
|
-
u.searchParams.get('access_token') ||
|
|
186
|
-
u.searchParams.get('jwt');
|
|
187
|
-
const error = u.searchParams.get('error');
|
|
188
|
-
|
|
189
|
-
if (error) {
|
|
190
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
191
|
-
res.end(
|
|
192
|
-
'<html><body style="font-family:sans-serif;text-align:center;padding:40px"><h2>Login denied</h2><p>You can close this tab.</p></body></html>'
|
|
193
|
-
);
|
|
194
|
-
server.close();
|
|
195
|
-
resolve(null);
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (returnedState && returnedState !== state) {
|
|
200
|
-
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
|
201
|
-
res.end('Invalid state');
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (!token) {
|
|
206
|
-
res.writeHead(400, { 'Content-Type': 'text/html' });
|
|
207
|
-
res.end(
|
|
208
|
-
'<html><body style="font-family:sans-serif;text-align:center;padding:40px"><h2>No token received</h2></body></html>'
|
|
209
|
-
);
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
214
|
-
res.end(
|
|
215
|
-
'<html><body style="font-family:sans-serif;text-align:center;padding:40px"><h2>✓ Login successful</h2><p>You can close this tab.</p></body></html>'
|
|
216
|
-
);
|
|
217
|
-
server.close();
|
|
218
|
-
resolve(token);
|
|
219
|
-
} catch {
|
|
220
|
-
res.writeHead(500);
|
|
221
|
-
res.end('Error');
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
server.listen(port, '127.0.0.1');
|
|
226
|
-
const timer = setTimeout(() => {
|
|
227
|
-
try {
|
|
228
|
-
server.close();
|
|
229
|
-
} catch (_) {}
|
|
230
|
-
resolve(null);
|
|
231
|
-
}, timeoutMs);
|
|
232
|
-
server.on('close', () => clearTimeout(timer));
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
|
|
236
170
|
async function saveAndValidateToken(token, { apiUrl, label }) {
|
|
237
171
|
const fetch = require('node-fetch');
|
|
238
172
|
const res = await fetch(`${apiUrl}/api/hosting/quota`, {
|
|
239
173
|
headers: { Authorization: `Bearer ${token}` },
|
|
240
174
|
});
|
|
241
|
-
|
|
242
175
|
if (res.status === 401) {
|
|
243
176
|
throw new Error('Invalid or expired token');
|
|
244
177
|
}
|
|
245
|
-
|
|
246
178
|
setConfig({ token, apiUrl });
|
|
247
|
-
|
|
248
179
|
console.log('');
|
|
249
180
|
console.log(chalk.green('✓ Logged in'));
|
|
250
|
-
console.log(chalk.gray(`
|
|
181
|
+
console.log(chalk.gray(` Config: ~/.moontraze/config.json`));
|
|
251
182
|
console.log('');
|
|
252
183
|
}
|
|
253
184
|
|