Skip to content

Commit

Permalink
server/cli: fix login issues
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jan 22, 2024
1 parent 9be913a commit 8a64075
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/cli/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"preLaunchTask": "npm: build",
"args": [
"shell",
"login",
],
"sourceMaps": true,
"resolveSourceMapLocations": [
Expand Down
24 changes: 2 additions & 22 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrypted",
"version": "1.3.8",
"version": "1.3.10",
"description": "",
"main": "./dist/packages/cli/src/main.js",
"bin": {
Expand All @@ -19,7 +19,6 @@
"@scrypted/client": "^1.3.3",
"@scrypted/types": "^0.2.99",
"engine.io-client": "^6.5.3",
"http-auth-utils": "^6.0.0",
"readline-sync": "^1.4.10",
"semver": "^7.5.4",
"tslib": "^2.6.2"
Expand Down
42 changes: 22 additions & 20 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs from 'fs';
import path from 'path';
import readline from 'readline-sync';
import semver from 'semver';
import { authHttpFetch } from '../../../common/src/http-auth-fetch';
import { httpFetch } from '../../../server/src/fetch/http-fetch';
import { installServe, serveMain } from './service';
import { connectShell } from './shell';

Expand Down Expand Up @@ -40,6 +40,12 @@ interface LoginFile {
[host: string]: Login;
}

function basicAuthHeaders(username: string, password: string) {
const headers = new Headers();
headers.set('Authorization', `Basic ${Buffer.from(username + ":" + password).toString('base64')}`);
return headers;
}

async function doLogin(host: string) {
host = toIpAndPort(host);

Expand All @@ -49,16 +55,15 @@ async function doLogin(host: string) {
});

const url = `https://${host}/login`;
const response = await authHttpFetch({
const response = await httpFetch({
method: 'GET',
credential: {
username,
password,
},
headers: basicAuthHeaders(username, password),
url,
rejectUnauthorized: false,
responseType: 'json',
});
if (response.body.error)
throw new Error(response.body.error);

fs.mkdirSync(scryptedHome, {
recursive: true,
Expand All @@ -76,13 +81,10 @@ async function doLogin(host: string) {

login[host] = response.body;
fs.writeFileSync(loginPath, JSON.stringify(login));
return login;
return login[host];
}

async function getOrDoLogin(host: string): Promise<{
username: string,
token: string,
}> {
async function getOrDoLogin(host: string): Promise<Login> {
let login: LoginFile;
try {
login = JSON.parse(fs.readFileSync(loginPath).toString());
Expand All @@ -91,11 +93,12 @@ async function getOrDoLogin(host: string): Promise<{

if (!login[host].username || !login[host].token)
throw new Error();

return login[host];
}
catch (e) {
login = await doLogin(host);
return doLogin(host);
}
return login[host];
}

async function runCommand() {
Expand Down Expand Up @@ -145,8 +148,8 @@ async function main() {
}
else if (process.argv[2] === 'login') {
const ip = process.argv[3] || '127.0.0.1';
const token = await doLogin(ip);
console.log('login successful. token:', token);
const login = await doLogin(ip);
console.log('login successful. token:', login.token);
}
else if (process.argv[2] === 'command') {
const { sdk, pendingResult } = await runCommand();
Expand Down Expand Up @@ -198,16 +201,15 @@ async function main() {

const login = await getOrDoLogin(ip);
const url = `https://${ip}/web/component/script/install/${pkg}`;
const response = await authHttpFetch({
const response = await httpFetch({
method: 'POST',
credential: {
username: login.username,
password: login.token,
},
headers: basicAuthHeaders(login.username, login.token),
url,
rejectUnauthorized: false,
responseType: 'json',
});
if (response.body.error)
throw new Error(response.body.error);

console.log('install successful. id:', response.body.id);
}
Expand Down
4 changes: 2 additions & 2 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions server/src/scrypted-server-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ async function start(mainFilename: string, options?: {
realm: 'Scrypted',
}, async (username, password, callback) => {
const user = await db.tryGet(ScryptedUser, username);
if (!user) {
callback(false);
return;
}

const salted = user.salt + password;
const hash = crypto.createHash('sha256');
Expand Down

0 comments on commit 8a64075

Please sign in to comment.