Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store usernames in lowercase #119

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/actions/adduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module.exports = async function(env, options) {
}

try {
await app.userDb.addUser(username, password, userIsAdmin);
console.log(`Added new user ${username}`);
const user = await app.userDb.addUser(username, password, userIsAdmin);
console.log(`Added new user ${user.username}`);
prawnsalad marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
l.error('Error adding new user:', err.message);
console.log('There was an error adding the new user');
Expand Down
20 changes: 12 additions & 8 deletions src/worker/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ class Users {
}

async authUserNetwork(username, password, network) {
const lcUsername = username.toLowerCase();
let ret = { network: null, user: null };

if (!Helpers.validUsername(username)) {
if (!Helpers.validUsername(lcUsername)) {
return ret;
}

try {
let isUserToken = false;
let query = this.db.dbUsers('user_networks')
.innerJoin('users', 'users.id', 'user_networks.user_id')
.where('users.username', 'LIKE', username)
.where('users.username', 'LIKE', lcUsername)
.where('user_networks.name', 'LIKE', network)
.select('user_networks.*', 'users.password as _pass', 'users.admin as user_admin');

Expand Down Expand Up @@ -58,14 +59,15 @@ class Users {
}

async authUser(username, password, userHost) {
if (!Helpers.validUsername(username)) {
const lcUsername = username.toLowerCase();
if (!Helpers.validUsername(lcUsername)) {
return null;
}

let isUserToken = false;
let query = this.db.dbUsers('users')
.select('users.*')
.where('username', 'LIKE', username)
.where('username', 'LIKE', lcUsername)
.where('locked', '!=', true);

if (tokens.isUserToken(password)) {
Expand Down Expand Up @@ -172,20 +174,22 @@ class Users {
}

async getUser(username) {
if (!Helpers.validUsername(username)) {
const lcUsername = username.toLowerCase();
if (!Helpers.validUsername(lcUsername)) {
return null;
}

return this.db.factories.User.query().where('username', 'LIKE', username).first();
return this.db.factories.User.query().where('username', 'LIKE', lcUsername).first();
}

async addUser(username, password, isAdmin) {
if (!Helpers.validUsername(username)) {
const lcUsername = username.toLowerCase();
if (!Helpers.validUsername(lcUsername)) {
throw new Error('Invalid username');
}

let user = this.db.factories.User();
user.username = username;
user.username = lcUsername;
user.password = password;
user.created_at = Helpers.now();
if (isAdmin === true) {
Expand Down