You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
const scryptForHumans = require("scrypt-for-humans");
|
|
|
|
const errors = require("../errors");
|
|
const extractLocalPart = require("../extract-local-part");
|
|
const createDatabaseModule = require("../db-module");
|
|
|
|
module.exports = function ({ knex }) {
|
|
return createDatabaseModule({
|
|
create: function ({ username, password, type }, tx = knex) {
|
|
return Promise.try(() => {
|
|
if (password != null) {
|
|
return scryptForHumans.hash(password);
|
|
}
|
|
}).then((hash) => {
|
|
return tx("users").insert({
|
|
username: username,
|
|
password_hash: hash,
|
|
type: type
|
|
}).returning("*");
|
|
});
|
|
},
|
|
byUsername: function ({ username }, tx = knex) {
|
|
return Promise.try(() => {
|
|
/* TODO: Validate that, if a fully-qualified ID is passed in, it's for the correct homeserver? */
|
|
return tx("users").first().where({ username: extractLocalPart(username) });
|
|
}).then((user) => {
|
|
if (user != null) {
|
|
return user;
|
|
} else {
|
|
throw new errors.InvalidUsername("No such user exists");
|
|
/* FIXME: Map to 403 error in login route */
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}; |