Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Refactor to add strict type comparisons and remove unnecessary else blocks #343

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/account-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import config from './load-config.js';
import * as uuid from 'uuid';
import * as bcrypt from 'bcrypt';

let _accountDb = null;
let _accountDb;

export default function getAccountDb() {
if (_accountDb == null) {
if (_accountDb === undefined) {
const dbPath = join(config.serverFiles, 'account.sqlite');
_accountDb = openDatabase(dbPath);
}
Expand All @@ -26,17 +26,17 @@ export function needsBootstrap() {
}

export function bootstrap(password) {
let accountDb = getAccountDb();
if (password === undefined || password === '') {
return { error: 'invalid-password' };
}

let accountDb = getAccountDb();
let rows = accountDb.all('SELECT * FROM auth');

if (rows.length !== 0) {
return { error: 'already-bootstrapped' };
}

if (password == null || password === '') {
return { error: 'invalid-password' };
}

// Hash the password. There's really not a strong need for this
// since this is a self-hosted instance owned by the user.
// However, just in case we do it.
Expand All @@ -45,6 +45,7 @@ export function bootstrap(password) {

let token = uuid.v4();
accountDb.mutate('INSERT INTO sessions (token) VALUES (?)', [token]);

return { token };
}

Expand All @@ -58,31 +59,33 @@ export function login(password) {

let confirmed = row && bcrypt.compareSync(password, row.password);

if (confirmed) {
// Right now, tokens are permanent and there's just one in the
// system. In the future this should probably evolve to be a
// "session" that times out after a long time or something, and
// maybe each device has a different token
let row = accountDb.first('SELECT * FROM sessions');
return { token: row.token };
} else {
return null;
if (!confirmed) {
return { error: 'invalid-password' };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only functional change in this PR, if the password is incorrect the server will return invalid-password, not null.

}

// Right now, tokens are permanent and there's just one in the
// system. In the future this should probably evolve to be a
// "session" that times out after a long time or something, and
// maybe each device has a different token
let sessionRow = accountDb.first('SELECT * FROM sessions');
return { token: sessionRow.token };
}

export function changePassword(newPassword) {
let accountDb = getAccountDb();

if (newPassword == null || newPassword === '') {
if (newPassword === undefined || newPassword === '') {
return { error: 'invalid-password' };
}

let accountDb = getAccountDb();

let hashed = hashPassword(newPassword);
let token = uuid.v4();

// Note that this doesn't have a WHERE. This table only ever has 1
// row (maybe that will change in the future? if so this will not work)
accountDb.mutate('UPDATE auth SET password = ?', [hashed]);
accountDb.mutate('UPDATE sessions SET token = ?', [token]);

return {};
}

Expand Down
4 changes: 2 additions & 2 deletions src/app-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ app.post('/bootstrap', (req, res) => {
if (error) {
res.status(400).send({ status: 'error', reason: error });
return;
} else {
res.send({ status: 'ok', data: { token } });
}

res.send({ status: 'ok', data: { token } });
});

app.post('/login', (req, res) => {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/343.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [matt-fidd]
---

Refactor to add strict type comparisons and remove unnecessary else blocks