latinfo 0.1.0 → 0.2.0
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/dist/index.js +40 -45
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,8 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const http_1 = __importDefault(require("http"));
|
|
8
9
|
const path_1 = __importDefault(require("path"));
|
|
9
10
|
const os_1 = __importDefault(require("os"));
|
|
11
|
+
const child_process_1 = require("child_process");
|
|
10
12
|
const API_URL = process.env.LATINFO_API_URL || 'https://api.latinfo.dev';
|
|
11
13
|
const GITHUB_CLIENT_ID = 'Ov23li5fcQaiCsVtaMKK';
|
|
12
14
|
const CONFIG_DIR = path_1.default.join(os_1.default.homedir(), '.latinfo');
|
|
@@ -29,54 +31,47 @@ function deleteConfig() {
|
|
|
29
31
|
}
|
|
30
32
|
catch { }
|
|
31
33
|
}
|
|
32
|
-
// --- GitHub
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
body: JSON.stringify({
|
|
55
|
-
client_id: GITHUB_CLIENT_ID,
|
|
56
|
-
device_code: codeData.device_code,
|
|
57
|
-
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
|
|
58
|
-
}),
|
|
34
|
+
// --- GitHub Authorization Code Flow ---
|
|
35
|
+
function openBrowser(url) {
|
|
36
|
+
const cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
|
|
37
|
+
(0, child_process_1.exec)(`${cmd} "${url}"`);
|
|
38
|
+
}
|
|
39
|
+
async function waitForCallback(port) {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
const server = http_1.default.createServer((req, res) => {
|
|
42
|
+
const url = new URL(req.url || '', `http://localhost:${port}`);
|
|
43
|
+
const code = url.searchParams.get('code');
|
|
44
|
+
if (code) {
|
|
45
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
46
|
+
res.end('<h2>Listo. Puedes cerrar esta ventana.</h2>');
|
|
47
|
+
server.close();
|
|
48
|
+
resolve(code);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
res.writeHead(400);
|
|
52
|
+
res.end('Missing code');
|
|
53
|
+
server.close();
|
|
54
|
+
reject(new Error('No se recibió código de GitHub'));
|
|
55
|
+
}
|
|
59
56
|
});
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
// 3. Exchange for Latinfo API key
|
|
57
|
+
server.listen(port, () => { });
|
|
58
|
+
setTimeout(() => { server.close(); reject(new Error('Timeout esperando autorización')); }, 120_000);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function login() {
|
|
62
|
+
const port = 8400;
|
|
63
|
+
const redirectUri = `http://localhost:${port}/callback`;
|
|
64
|
+
const scope = 'read:user,user:email';
|
|
65
|
+
const authUrl = `https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}`;
|
|
66
|
+
console.log('Abriendo GitHub...');
|
|
67
|
+
openBrowser(authUrl);
|
|
68
|
+
// 1. Wait for GitHub to redirect back with code
|
|
69
|
+
const code = await waitForCallback(port);
|
|
70
|
+
// 2. Exchange code for access token (server-side, needs client_secret)
|
|
76
71
|
const authRes = await fetch(`${API_URL}/auth/github`, {
|
|
77
72
|
method: 'POST',
|
|
78
73
|
headers: { 'Content-Type': 'application/json' },
|
|
79
|
-
body: JSON.stringify({
|
|
74
|
+
body: JSON.stringify({ code, redirect_uri: redirectUri }),
|
|
80
75
|
});
|
|
81
76
|
if (!authRes.ok) {
|
|
82
77
|
console.error('Error obteniendo API key:', await authRes.text());
|
|
@@ -84,7 +79,7 @@ async function login() {
|
|
|
84
79
|
}
|
|
85
80
|
const authData = await authRes.json();
|
|
86
81
|
saveConfig({ api_key: authData.api_key, github_username: authData.github_username });
|
|
87
|
-
console.log(
|
|
82
|
+
console.log(`Logueado como ${authData.github_username}`);
|
|
88
83
|
}
|
|
89
84
|
// --- Commands ---
|
|
90
85
|
async function ruc(rucNumber) {
|