cli: auth fixes

This commit is contained in:
Koushik Dutta
2021-09-23 13:58:22 -07:00
parent d9109d57f1
commit 9c27ed5ecb
9 changed files with 26 additions and 81 deletions

1
cli/.gitignore vendored
View File

@@ -1 +1,2 @@
node_modules/
dist

View File

@@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"console": "externalTerminal",
"console": "integratedTerminal",
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",

3
cli/dist/main.d.ts vendored
View File

@@ -1,3 +0,0 @@
#!/usr/bin/env node
export {};
//# sourceMappingURL=main.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":""}

68
cli/dist/main.js vendored
View File

@@ -1,68 +0,0 @@
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
const path_1 = (0, tslib_1.__importDefault)(require("path"));
const axios_1 = (0, tslib_1.__importDefault)(require("axios"));
const readline_sync_1 = (0, tslib_1.__importDefault)(require("readline-sync"));
const https_1 = (0, tslib_1.__importDefault)(require("https"));
const mkdirp_1 = (0, tslib_1.__importDefault)(require("mkdirp"));
function getUserHome() {
const ret = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
if (!ret)
throw new Error('Neither USERPROFILE or HOME are defined.');
return ret;
}
const axiosConfig = {
httpsAgent: new https_1.default.Agent({
rejectUnauthorized: false
})
};
function main() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
if (process.argv[2] === 'install') {
const ip = process.argv[4] || '127.0.0.1';
const pkg = process.argv[3];
if (!pkg) {
console.log('usage: npx scrypted install npm-package-name [ip]');
process.exit(1);
}
const scryptedHome = path_1.default.join(getUserHome(), '.scrypted');
const loginPath = path_1.default.join(scryptedHome, 'login.json');
let login;
try {
login = JSON.parse(fs_1.default.readFileSync(loginPath).toString());
}
catch (e) {
const username = readline_sync_1.default.question('username: ');
const password = readline_sync_1.default.question('password: ', {
hideEchoBack: true,
});
const url = `https://${ip}:9443/login`;
const authedConfig = Object.assign({
method: 'GET',
auth: {
username,
password,
},
url,
}, axiosConfig);
const response = yield (0, axios_1.default)(authedConfig);
mkdirp_1.default.sync(scryptedHome);
login = login || {};
login[ip] = response.data;
fs_1.default.writeFileSync(loginPath, JSON.stringify(loginPath));
}
const url = `https://${ip}:9443/web/component/script/install/${pkg}`;
const response = yield axios_1.default.post(url, undefined, axiosConfig);
console.log('install successful. id:', response.data.id);
}
else {
console.log('usage: npx scrypted install npm-package-name');
process.exit(1);
}
});
}
main();
//# sourceMappingURL=main.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;AAEA,yDAAoB;AACpB,6DAAwB;AACxB,+DAAkD;AAElD,+EAAuD;AACvD,+DAA0B;AAC1B,iEAA4B;AAE5B,SAAS,WAAW;IAChB,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAChF,IAAI,CAAC,GAAG;QACJ,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAChE,OAAO,GAAG,CAAC;AACf,CAAC;AAED,MAAM,WAAW,GAAuB;IACpC,UAAU,EAAE,IAAI,eAAK,CAAC,KAAK,CAAC;QACxB,kBAAkB,EAAE,KAAK;KAC5B,CAAC;CACL,CAAA;AAED,SAAe,IAAI;;QACf,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;YAC/B,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,WAAW,CAAC;YAC1C,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAE5B,IAAI,CA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AG,EAAE,CAAC;YACrE,MAAM,QAAQ,GAAG,MAAM,eAAK,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,WAAW,CAAC,CAAC;YAC/D,OAAO,CAAC,GAAG,CAAC,yBAAyB,EAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SAC5D;aACK;YACD,OAAO,CAAC,GAAG,CAAC,8CAA8C,CAAC,CAAC;YAC5D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;SACnB;IACL,CAAC;CAAA;AAED,IAAI,EAAE,CAAC"}

4
cli/package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "scrypted",
"version": "1.0.13",
"version": "1.0.14",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "scrypted",
"version": "1.0.13",
"version": "1.0.14",
"license": "ISC",
"dependencies": {
"@scrypted/sdk": "^0.0.79",

View File

@@ -1,6 +1,6 @@
{
"name": "scrypted",
"version": "1.0.13",
"version": "1.0.14",
"description": "",
"main": "dist/main.js",
"bin": {

View File

@@ -33,13 +33,22 @@ async function main() {
const scryptedHome = path.join(getUserHome(), '.scrypted');
const loginPath = path.join(scryptedHome, 'login.json');
let username: string;
let password: string;
let login: any;
try {
login = JSON.parse(fs.readFileSync(loginPath).toString());
if (typeof login !== 'object')
login = {};
if (!login[ip].username || !login[ip].token)
throw new Error();
username = login[ip].username;
password = login[ip].token;
}
catch (e) {
const username = readline.question('username: ');
const password = readline.question('password: ', {
username = readline.question('username: ');
password = readline.question('password: ', {
hideEchoBack: true,
});
@@ -57,11 +66,19 @@ async function main() {
mkdirp.sync(scryptedHome);
login = login || {};
login[ip] = response.data;
fs.writeFileSync(loginPath, JSON.stringify(loginPath));
fs.writeFileSync(loginPath, JSON.stringify(login));
}
const url = `https://${ip}:9443/web/component/script/install/${pkg}`;
const response = await axios.post(url, undefined, axiosConfig);
const response = await axios.post(url, undefined, Object.assign({
method: 'GET',
auth: {
username: login[ip].username,
password: login[ip].token,
},
url,
}, axiosConfig));
console.log('install successful. id:',response.data.id);
}
else {