Skip to content

Commit

Permalink
Refactor to add strict type comparisons and remove unnecessary else b…
Browse files Browse the repository at this point in the history
…locks (#343)
  • Loading branch information
matt-fidd authored Apr 20, 2024
1 parent 1bbba66 commit 4ae654d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
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' };
}

// 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

0 comments on commit 4ae654d

Please sign in to comment.