Fixed database abstraction naming and changed a lot of lets to consts and /unauth/permanote is broken
This commit is contained in:
@@ -4,7 +4,7 @@ import {Notes} from '../models/notes/notes';
|
|||||||
|
|
||||||
export class GroupController extends ControllerHandler {
|
export class GroupController extends ControllerHandler {
|
||||||
static async newGroup(req, res, next) {
|
static async newGroup(req, res, next) {
|
||||||
let errors = new API.errors(res);
|
const errors = new API.errors(res);
|
||||||
|
|
||||||
|
|
||||||
next()
|
next()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {User} from '../models/user/user';
|
|||||||
|
|
||||||
export class LoginController extends ControllerHandler {
|
export class LoginController extends ControllerHandler {
|
||||||
static async authenticate(req, res, next) {
|
static async authenticate(req, res, next) {
|
||||||
let errors = new API.errors(res);
|
const errors = new API.errors(res);
|
||||||
|
|
||||||
let ip = req.connection.remoteAddress;
|
let ip = req.connection.remoteAddress;
|
||||||
if (ip.startsWith('::ffff:')) ip = ip.substring(7);
|
if (ip.startsWith('::ffff:')) ip = ip.substring(7);
|
||||||
@@ -25,11 +25,11 @@ export class LoginController extends ControllerHandler {
|
|||||||
|
|
||||||
let user;
|
let user;
|
||||||
if (!username /*If they're loging in with email*/) {
|
if (!username /*If they're loging in with email*/) {
|
||||||
user = await Database.users.getUser('email', email);
|
user = await Database.Users.getUser('email', email);
|
||||||
if (user == -1) errors.addError(422, 'Unprocessable entity', 'There is no user with that email');
|
if (user == -1) errors.addError(422, 'Unprocessable entity', 'There is no user with that email');
|
||||||
username = user.username;
|
username = user.username;
|
||||||
} else {
|
} else {
|
||||||
user = await Database.users.getUser('username', username);
|
user = await Database.Users.getUser('username', username);
|
||||||
if (user == -1) errors.addError(422, 'Unprocessable entity', 'There is no user with that username');
|
if (user == -1) errors.addError(422, 'Unprocessable entity', 'There is no user with that username');
|
||||||
email = user.email;
|
email = user.email;
|
||||||
}
|
}
|
||||||
@@ -40,23 +40,22 @@ export class LoginController extends ControllerHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let match = await User.Password.compare(password, user.password);
|
const match = await User.Password.compare(password, user.password);
|
||||||
|
|
||||||
if (!match) {
|
if (!match) {
|
||||||
errors.addError(401, 'Unauthorized', 'Incorrect password for user').endpoint();
|
errors.addError(401, 'Unauthorized', 'Incorrect password for user').endpoint();
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = new API.user(res, user.id, username, email, new Date(parseInt(user.lastupdated)).toLocaleString());
|
const response = new API.user(res, user.id, username, email, new Date(parseInt(user.lastupdated)).toLocaleString());
|
||||||
let token = await Database.auth.getTokenByID(user.id);
|
let token = await Database.Authorization.getTokenByID(user.id);
|
||||||
|
|
||||||
if (token == -1) {
|
if (token == -1) {
|
||||||
let encryptedPass = await User.Password.gen(password);
|
const encryptedPass = await User.Password.gen(password);
|
||||||
password = null; // Cleaning password from memory
|
password = null; // Cleaning password from memory
|
||||||
let status = response.getStatus;
|
const status = response.getStatus;
|
||||||
token = User.Token.gen(status, user.id, encryptedPass);
|
token = User.Token.gen(status, user.id, encryptedPass);
|
||||||
Database.auth.newToken(user.id, token, encryptedPass);
|
Database.Authorization.newToken(user.id, token, encryptedPass);
|
||||||
}
|
}
|
||||||
response.Token = token.token;
|
response.Token = token.token;
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
import {MiddleWare} from './index';
|
import {MiddleWare} from './index';
|
||||||
import {API} from '../api/api';
|
import {API} from '../api/api';
|
||||||
import {Logger} from '../../models/logger'
|
import {Logger} from '../../models/logger'
|
||||||
import {AuthModel} from '../../models/auth/authModel';
|
import {Auth} from '../../models/auth/authModel';
|
||||||
|
|
||||||
export class AuthMiddleWare extends MiddleWare {
|
export class AuthMiddleWare extends MiddleWare {
|
||||||
static async authUser(req, res, next) {
|
static async authUser(req, res, next) {
|
||||||
let errors = new API.errors(res);
|
const errors = new API.errors(res);
|
||||||
|
|
||||||
if (!req.headers.authorization) {
|
if (!req.headers.authorization) {
|
||||||
errors.addError(403, 'Forbidden', 'You cannot access this resource without authorization').endpoint();
|
errors.addError(403, 'Forbidden', 'You cannot access this resource without authorization').endpoint();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let token = req.headers.authorization;
|
const token = req.headers.authorization;
|
||||||
let user = await AuthModel.getUserFromToken(token);
|
const user = await Auth.getUserFromToken(token);
|
||||||
if (user == -1) {
|
if (user == -1) {
|
||||||
errors.addError(403, 'Forbidden', 'You cannot access this resource without authorization').endpoint();
|
errors.addError(403, 'Forbidden', 'You cannot access this resource without authorization').endpoint();
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
export class ErrorHandler {
|
export class ErrorHandler {
|
||||||
static async newError(err, req, res, next) {
|
static async newError(err, req, res, next) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ let buckets = {}
|
|||||||
|
|
||||||
export class RateLimits extends MiddleWare {
|
export class RateLimits extends MiddleWare {
|
||||||
static async request(req, res, next) {
|
static async request(req, res, next) {
|
||||||
let ip = req.connection.remoteAddress;
|
const ip = req.connection.remoteAddress;
|
||||||
MiddleWare.analytics(req, res, next);
|
MiddleWare.analytics(req, res, next);
|
||||||
|
|
||||||
if (!buckets[ip]) {
|
if (!buckets[ip]) {
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ export class NoteController extends ControllerHandler {
|
|||||||
static async newNote(req, res, next) {
|
static async newNote(req, res, next) {
|
||||||
const errors = new API.errors(res);
|
const errors = new API.errors(res);
|
||||||
|
|
||||||
let content = req.body.text || null;
|
const content = req.body.text || null;
|
||||||
let creatorid = req.user.id || undefined;
|
const creatorid = req.user.id || undefined;
|
||||||
let group = req.body.parentgroup || undefined;
|
const group = req.body.parentgroup || undefined;
|
||||||
let order = req.body.order || undefined;
|
let order = req.body.order || undefined;
|
||||||
|
|
||||||
let user = req.user || undefined;
|
const user = req.user || undefined;
|
||||||
|
|
||||||
if (!creatorid || !user) {
|
if (!creatorid || !user) {
|
||||||
errors.addError(403, 'Forbidden').endpoint();
|
errors.addError(403, 'Forbidden').endpoint();
|
||||||
@@ -25,13 +25,13 @@ export class NoteController extends ControllerHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = await Notes.genID();
|
const id = await Notes.genID();
|
||||||
|
|
||||||
let success;
|
let success;
|
||||||
if (!group) {
|
if (!group) {
|
||||||
success = await Notes.newNote(id, content, creatorid, order);
|
success = await Notes.newNote(id, content, creatorid, order);
|
||||||
} else {
|
} else {
|
||||||
let doesExist = await Notes.doesGroupExist(user.id, parentgroup);
|
const doesExist = await Notes.doesGroupExist(user.id, parentgroup);
|
||||||
if (!doesExist) {
|
if (!doesExist) {
|
||||||
errors.addError(422, 'Unprocessable entity', 'You are trying to create a note for a group that does not exist').endpoint();
|
errors.addError(422, 'Unprocessable entity', 'You are trying to create a note for a group that does not exist').endpoint();
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -6,24 +6,21 @@ import {PermaLink} from '../models/permalinks/permalink';
|
|||||||
|
|
||||||
export class PermaLinkController extends ControllerHandler {
|
export class PermaLinkController extends ControllerHandler {
|
||||||
static async unauthentacatedPermaLink(req, res, next) {
|
static async unauthentacatedPermaLink(req, res, next) {
|
||||||
let errors = new API.errors(res);
|
const errors = new API.errors(res);
|
||||||
|
|
||||||
let text = req.body.content || undefined;
|
const content = req.body.content || undefined;
|
||||||
if (!content) {
|
if (!content) {
|
||||||
errors.addError(422, 'Unprocessable entity', 'There is no text');
|
errors.addError(422, 'Unprocessable entity', 'There is no text').endpoint();
|
||||||
errors.endpoint();
|
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let uid = await PermaLink.genUID() || new Date().getTime();
|
const uid = await PermaLink.genUID() || new Date().getTime();
|
||||||
let endpoint = await PermaLink.genEndpoint();
|
const endpoint = await PermaLink.genEndpoint();
|
||||||
|
|
||||||
let success = await Database.permalink.newNote(uid, endpoint, content);
|
|
||||||
|
|
||||||
|
const success = await Database.PermaNotes.newNote(uid, endpoint, content);
|
||||||
if (success == -1) {
|
if (success == -1) {
|
||||||
errors.addError(500, 'Internal server error');
|
errors.addError(500, 'Internal server error').endpoint();
|
||||||
errors.endpoint();
|
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -33,14 +30,14 @@ export class PermaLinkController extends ControllerHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getNote(req, res, next) {
|
static async getNote(req, res, next) {
|
||||||
let endpoint = req.params.endpoint || undefined;
|
const endpoint = req.params.endpoint || undefined;
|
||||||
|
|
||||||
if (!endpoint) {
|
if (!endpoint) {
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = await Database.permalink.getNoteByEndpoint(endpoint);
|
const data = await Database.PermaNotes.getNoteByEndpoint(endpoint);
|
||||||
if (data == -1) {
|
if (data == -1) {
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import {User} from '../models/user/user';
|
|||||||
|
|
||||||
export class UserController extends ControllerHandler {
|
export class UserController extends ControllerHandler {
|
||||||
static async newUser(req, res, next) {
|
static async newUser(req, res, next) {
|
||||||
let errors = new API.errors(res);
|
const errors = new API.errors(res);
|
||||||
|
|
||||||
let ip = req.connection.remoteAddress;
|
let ip = req.connection.remoteAddress;
|
||||||
if (ip.startsWith('::ffff:')) ip = ip.substring(7);
|
if (ip.startsWith('::ffff:')) ip = ip.substring(7);
|
||||||
|
|
||||||
let username = req.body.username || undefined;
|
const username = req.body.username || undefined;
|
||||||
let email = req.body.email || undefined;
|
const email = req.body.email || undefined;
|
||||||
let password = req.body.password || undefined;
|
let password = req.body.password || undefined;
|
||||||
|
|
||||||
if (!username || !email || !password) errors.addError(422, 'Unprocessaable entity', 'Missing username, email or password in body of request');
|
if (!username || !email || !password) errors.addError(422, 'Unprocessaable entity', 'Missing username, email or password in body of request');
|
||||||
@@ -24,8 +24,8 @@ export class UserController extends ControllerHandler {
|
|||||||
if (!UserController.isPasswordValid(password)) errors.addError(422, 'Unprocessaable entity', 'Invalid password has spaces');
|
if (!UserController.isPasswordValid(password)) errors.addError(422, 'Unprocessaable entity', 'Invalid password has spaces');
|
||||||
if (password.length < 7) errors.addError(422, 'Unprocessaable entity', 'Invalid password less than 7 charicters');
|
if (password.length < 7) errors.addError(422, 'Unprocessaable entity', 'Invalid password less than 7 charicters');
|
||||||
|
|
||||||
if (await Database.users.getUser('username', username) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that username allready exists');
|
if (await Database.Users.getUser('username', username) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that username allready exists');
|
||||||
if (await Database.users.getUser('email', email) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that email allready exists');
|
if (await Database.Users.getUser('email', email) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that email allready exists');
|
||||||
|
|
||||||
if (errors.count() > 0) {
|
if (errors.count() > 0) {
|
||||||
errors.endpoint();
|
errors.endpoint();
|
||||||
@@ -33,20 +33,20 @@ export class UserController extends ControllerHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = new API.user(res, id, username, email, new Date().toLocaleString());
|
const response = new API.user(res, id, username, email, new Date().toLocaleString());
|
||||||
|
|
||||||
let encryptedPass = await User.Password.gen(password);
|
const encryptedPass = await User.Password.gen(password);
|
||||||
password = null; // Cleaning password from memory
|
password = null; // Cleaning password from memory
|
||||||
|
|
||||||
let status = response.getStatus;
|
const status = response.getStatus;
|
||||||
|
|
||||||
let id = new Date().getTime();
|
const id = new Date().getTime();
|
||||||
let token = await User.Token.gen(status, id, encryptedPass);
|
const token = await User.Token.gen(status, id, encryptedPass);
|
||||||
await Database.auth.newToken(id, token, encryptedPass);
|
await Database.Authorization.newToken(id, token, encryptedPass);
|
||||||
response.Token = token;
|
response.Token = token;
|
||||||
|
|
||||||
let user = new User(id, username, encryptedPass, email, ip, 1234);
|
const user = new User(id, username, encryptedPass, email, ip, 1234); // Authcode is placeholder for email authentication
|
||||||
let success = await user.insert();
|
const success = await user.insert();
|
||||||
if (success == -1) {
|
if (success == -1) {
|
||||||
errors.addError(500, 'Internal server error').endpoint();
|
errors.addError(500, 'Internal server error').endpoint();
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ async function init() {
|
|||||||
// Logger.debug(JSON.stringify(await Database.users.listAll(), null, 4));
|
// Logger.debug(JSON.stringify(await Database.users.listAll(), null, 4));
|
||||||
// await new User(1234135, 'plane000', 'adifl', 'playsplane@gmail.com', '127.0.0.1', new Date().getTime(), false, 'SGASGD', -1).insert();
|
// await new User(1234135, 'plane000', 'adifl', 'playsplane@gmail.com', '127.0.0.1', new Date().getTime(), false, 'SGASGD', -1).insert();
|
||||||
|
|
||||||
Logger.debug(JSON.stringify(await Database.note.listAll(), null, 4));;
|
Logger.debug(JSON.stringify(await Database.Notes.listAll(), null, 4));;
|
||||||
|
|
||||||
// Logger.database('Database Log');
|
// Logger.database('Database Log');
|
||||||
// Logger.middleware('GET request to /');
|
// Logger.middleware('GET request to /');
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {Database} from '../database/database';
|
import {Database} from '../database/database';
|
||||||
|
|
||||||
export class AuthModel {
|
export class Auth {
|
||||||
static async getUserFromToken(token) {
|
static async getUserFromToken(token) {
|
||||||
let id = await Database.auth.getIDByToken(token);
|
const id = await Database.auth.getIDByToken(token);
|
||||||
if (id == -1) return id;
|
if (id == -1) return id;
|
||||||
return await Database.users.getUserByID(id.id);
|
return await Database.users.getUserByID(id.id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,30 +5,20 @@ import {Logger} from '../logger';
|
|||||||
|
|
||||||
export class Database extends BaseDatabase {
|
export class Database extends BaseDatabase {
|
||||||
static async exec(query) {
|
static async exec(query) {
|
||||||
let connection = BaseDatabase.Connection;
|
const connection = super.Connection;
|
||||||
let res;
|
try {
|
||||||
|
const result = await connection.query(query);
|
||||||
let promise = new Promise((resolve, reject) => {
|
Logger.database(JSON.stringify(result, null, 4));
|
||||||
connection
|
return result[0][0].result;
|
||||||
.query(query)
|
}
|
||||||
.then(result => {
|
catch (e) {
|
||||||
Logger.database(JSON.stringify(res, null, 4));
|
Logger.error(`An error occured while querying a database: ${e}`);
|
||||||
res = result[0][0].result;
|
}
|
||||||
resolve();
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
Logger.error('An error occured while querying a database: ' + err);
|
|
||||||
reject()
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await promise;
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Database.users = require('./users').UserTools;
|
Database.Users = require('./users').UserTools;
|
||||||
Database.auth = require('./tokens').TokenTools;
|
Database.Authorization = require('./tokens').TokenTools;
|
||||||
Database.permalink = require('./permalinks').PermaLinkTools;
|
Database.PermaNotes = require('./permalinks').PermaLinkTools;
|
||||||
Database.notegroup = require('./notegroups').NoteGroupTools;
|
Database.NoteGroups = require('./notegroups').NoteGroupTools;
|
||||||
Database.note = require('./notes').NoteTools;
|
Database.Notes = require('./notes').NoteTools;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ export class NoteGroupTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async newGroup(id, creatorid, order) {
|
static async newGroup(id, creatorid, order) {
|
||||||
let Group = BaseDatabase.Group;
|
const Group = super.Group;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let group = await Group.create({
|
let group = await Group.create({
|
||||||
@@ -25,7 +25,7 @@ export class NoteGroupTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async deleteGroup(id) {
|
static async deleteGroup(id) {
|
||||||
let Group = BaseDatabase.Group;
|
const Group = super.Group;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Group.destroy({where: {id: id}});
|
await Group.destroy({where: {id: id}});
|
||||||
@@ -37,7 +37,7 @@ export class NoteGroupTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async renameGroup(id, newName) {
|
static async renameGroup(id, newName) {
|
||||||
let Group = BaseDatabase.Group;
|
const Group = super.Group;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Group.update({name: newName}, {where: {id: id}});
|
await Group.update({name: newName}, {where: {id: id}});
|
||||||
@@ -50,7 +50,7 @@ export class NoteGroupTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async reorderGroup(id, newPosition) {
|
static async reorderGroup(id, newPosition) {
|
||||||
let Group = BaseDatabase.Group;
|
const Group = super.Group;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Group.update({order: newPosition}, {where: {id: id}});
|
await Group.update({order: newPosition}, {where: {id: id}});
|
||||||
@@ -63,7 +63,7 @@ export class NoteGroupTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getGroupByID(id) {
|
static async getGroupByID(id) {
|
||||||
let Group = BaseDatabase.Group;
|
const Group = super.Group;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let group = await Group.findOne({where: {id: id}});
|
let group = await Group.findOne({where: {id: id}});
|
||||||
@@ -76,7 +76,7 @@ export class NoteGroupTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getAllUsersGroups(userid) {
|
static async getAllUsersGroups(userid) {
|
||||||
let Group = BaseDatabase.Group;
|
const Group = super.Group;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let group = Group.findAll({where: {creatorid: userid}});
|
let group = Group.findAll({where: {creatorid: userid}});
|
||||||
@@ -89,7 +89,7 @@ export class NoteGroupTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async updateLastUpdatedTime(id) {
|
static async updateLastUpdatedTime(id) {
|
||||||
let Group = BaseDatabase.Group;
|
const Group = super.Group;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Group.update({lastupdated: new Date().getTime()}, {where: {id: id}});
|
await Group.update({lastupdated: new Date().getTime()}, {where: {id: id}});
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import {Logger} from '../logger';
|
|||||||
|
|
||||||
export class NoteTools extends BaseDatabase {
|
export class NoteTools extends BaseDatabase {
|
||||||
static async listAll() {
|
static async listAll() {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
return Note.findAll();
|
return Note.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
static async newNote(id, content, creatorid, order, parentgroup) {
|
static async newNote(id, content, creatorid, order, parentgroup) {
|
||||||
parentgroup = parentgroup || null;
|
parentgroup = parentgroup || null;
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await Note.create({
|
let note = await Note.create({
|
||||||
@@ -30,7 +30,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async deleteNote(id) {
|
static async deleteNote(id) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Note.destroy({where: {id: id}});
|
await Note.destroy({where: {id: id}});
|
||||||
@@ -42,7 +42,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async makePermaLink(id, endpoint) {
|
static async makePermaLink(id, endpoint) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Note.update({endpoint: endpoint}, {where: {id: id}});
|
await Note.update({endpoint: endpoint}, {where: {id: id}});
|
||||||
@@ -54,7 +54,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getNoteByEndpoint(endpoint) {
|
static async getNoteByEndpoint(endpoint) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await Note.findOne({where: {endpoint: endpoint}});
|
let note = await Note.findOne({where: {endpoint: endpoint}});
|
||||||
@@ -67,7 +67,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async updateNote(id, newContent) {
|
static async updateNote(id, newContent) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Note.update({content: newContent}, {where: {id: id}});
|
await Note.update({content: newContent}, {where: {id: id}});
|
||||||
@@ -80,7 +80,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async updateCatergory(id, newCatergory) {
|
static async updateCatergory(id, newCatergory) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Note.update({catergory: newCatergory}, {where: {id: id}});
|
await Note.update({catergory: newCatergory}, {where: {id: id}});
|
||||||
@@ -93,7 +93,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async reorderNote(id, newOrder) {
|
static async reorderNote(id, newOrder) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Note.update({order: newOrder}, {where: {id: id}});
|
await Note.update({order: newOrder}, {where: {id: id}});
|
||||||
@@ -106,7 +106,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async renameNote(id, newName) {
|
static async renameNote(id, newName) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Note.update({name: newName}, {where: {id: id}});
|
await Note.update({name: newName}, {where: {id: id}});
|
||||||
@@ -119,7 +119,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getNoteByID(id) {
|
static async getNoteByID(id) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await Note.findOne({where: {id: id}});
|
let note = await Note.findOne({where: {id: id}});
|
||||||
@@ -132,7 +132,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getAllUserNotes(userid) {
|
static async getAllUserNotes(userid) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await Note.findAll({where: {creatorid: userid}});
|
let note = await Note.findAll({where: {creatorid: userid}});
|
||||||
@@ -145,7 +145,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getAllGroupedNotes(groupid) {
|
static async getAllGroupedNotes(groupid) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await Note.findAll({where: {parentgroup: groupid}});
|
let note = await Note.findAll({where: {parentgroup: groupid}});
|
||||||
@@ -158,7 +158,7 @@ export class NoteTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async updateLastUpdateTime(id) {
|
static async updateLastUpdateTime(id) {
|
||||||
let Note = BaseDatabase.Note;
|
const Note = super.Note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Note.update({lastupdated: new Date().getTime()}, {where: {id: id}});
|
await Note.update({lastupdated: new Date().getTime()}, {where: {id: id}});
|
||||||
|
|||||||
@@ -5,16 +5,16 @@ import {Logger} from '../logger';
|
|||||||
|
|
||||||
export class PermaLinkTools extends BaseDatabase {
|
export class PermaLinkTools extends BaseDatabase {
|
||||||
static async listAll() {
|
static async listAll() {
|
||||||
let PermaNote = BaseDatabase.PermaNote;
|
const PermaNote = super.PermaNote;
|
||||||
return PermaNote.findAll();
|
return PermaNote.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
static async newNote(uid, endpoint, text, id) {
|
static async newNote(uid, endpoint, text, id) {
|
||||||
let PermaNote = BaseDatabase.PermaNote;
|
const PermaNote = super.PermaNote;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await PermaNote.create({
|
let note = await PermaNote.create({
|
||||||
id: id,
|
id: uid,
|
||||||
endpoint: endpoint,
|
endpoint: endpoint,
|
||||||
text: text,
|
text: text,
|
||||||
creatorid: id
|
creatorid: id
|
||||||
@@ -27,7 +27,7 @@ export class PermaLinkTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getNoteByUID(id) {
|
static async getNoteByUID(id) {
|
||||||
let PermaNote = BaseDatabase.PermaNote;
|
const PermaNote = super.PermaNote;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await PermaNote.findOne({where: {id: id}});
|
let note = await PermaNote.findOne({where: {id: id}});
|
||||||
@@ -40,7 +40,7 @@ export class PermaLinkTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getNoteByEndpoint(endpoint) {
|
static async getNoteByEndpoint(endpoint) {
|
||||||
let PermaNote = BaseDatabase.PermaNote;
|
const PermaNote = super.PermaNote;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let note = await PermaNote.findOne({where: {endpoint: endpoint}});
|
let note = await PermaNote.findOne({where: {endpoint: endpoint}});
|
||||||
|
|||||||
Binary file not shown.
@@ -3,12 +3,12 @@ import {Logger} from '../logger';
|
|||||||
|
|
||||||
export class TokenTools extends BaseDatabase {
|
export class TokenTools extends BaseDatabase {
|
||||||
static async listAll() {
|
static async listAll() {
|
||||||
let Auth = BaseDatabase.Auth;
|
const Auth = super.Auth;
|
||||||
return Auth.findAll();
|
return Auth.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
static async newToken(id, token, passHash) {
|
static async newToken(id, token, passHash) {
|
||||||
let Auth = BaseDatabase.Auth;
|
const Auth = super.Auth;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let auth = await Auth.create({
|
let auth = await Auth.create({
|
||||||
@@ -24,7 +24,7 @@ export class TokenTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async delete(id) {
|
static async delete(id) {
|
||||||
let Auth = BaseDatabase.Auth;
|
const Auth = super.Auth;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Auth.destroy({where: {id: id}});
|
await Auth.destroy({where: {id: id}});
|
||||||
@@ -36,7 +36,7 @@ export class TokenTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getTokenByID(id) {
|
static async getTokenByID(id) {
|
||||||
let Auth = BaseDatabase.Auth;
|
const Auth = super.Auth;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let auth = await Auth.findOne({where: {id: id}});
|
let auth = await Auth.findOne({where: {id: id}});
|
||||||
@@ -49,7 +49,7 @@ export class TokenTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getIDByToken(token) {
|
static async getIDByToken(token) {
|
||||||
let Auth = BaseDatabase.Auth;
|
const Auth = super.Auth;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let auth = await Auth.findOne({where: {token: token}});
|
let auth = await Auth.findOne({where: {token: token}});
|
||||||
@@ -62,7 +62,7 @@ export class TokenTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getTokenByPassHash(hash) {
|
static async getTokenByPassHash(hash) {
|
||||||
let Auth = BaseDatabase.Auth;
|
const Auth = super.Auth;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let auth = await Auth.findOne({where: {passhash: hash}});
|
let auth = await Auth.findOne({where: {passhash: hash}});
|
||||||
@@ -75,7 +75,7 @@ export class TokenTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async updateToken(id, newToken) {
|
static async updateToken(id, newToken) {
|
||||||
let Auth = BaseDatabase.Auth;
|
const Auth = super.Auth;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Auth.update({token: newToken}, {where: {id: id}});
|
await Auth.update({token: newToken}, {where: {id: id}});
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ import {Logger} from '../logger';
|
|||||||
|
|
||||||
export class UserTools extends BaseDatabase {
|
export class UserTools extends BaseDatabase {
|
||||||
static async listAll() {
|
static async listAll() {
|
||||||
let User = BaseDatabase.User;
|
const User = super.User;
|
||||||
return User.findAll();
|
return User.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
static async newUser(id, username, password, email, ip, authcode) {
|
static async newUser(id, username, password, email, ip, authcode) {
|
||||||
let User = BaseDatabase.User;
|
const User = super.User;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let user = await User.create({
|
let user = await User.create({
|
||||||
@@ -32,7 +32,7 @@ export class UserTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async deleteUser(id) {
|
static async deleteUser(id) {
|
||||||
let User = BaseDatabase.User;
|
const User = super.User;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await User.destroy({where: {id: id}});
|
await User.destroy({where: {id: id}});
|
||||||
@@ -44,7 +44,7 @@ export class UserTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getUserByID(id) {
|
static async getUserByID(id) {
|
||||||
let User = BaseDatabase.User;
|
const User = super.User;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let user = await User.findOne({where: {id: id}});
|
let user = await User.findOne({where: {id: id}});
|
||||||
@@ -57,7 +57,7 @@ export class UserTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getUser(column, search) {
|
static async getUser(column, search) {
|
||||||
let User = BaseDatabase.User;
|
const User = super.User;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let user;
|
let user;
|
||||||
@@ -90,7 +90,7 @@ export class UserTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async updateIP(id, newIP) {
|
static async updateIP(id, newIP) {
|
||||||
let User = BaseDatabase.User;
|
const User = super.User;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await User.update({ip: newIP}, {where: {id: id}});
|
await User.update({ip: newIP}, {where: {id: id}});
|
||||||
@@ -102,7 +102,7 @@ export class UserTools extends BaseDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async authUser(id) {
|
static async authUser(id) {
|
||||||
let User = BaseDatabase.User;
|
const User = super.User;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let user = await User.update({verified: true}, {where: {id: id}});
|
let user = await User.update({verified: true}, {where: {id: id}});
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export class PermaLink {
|
|||||||
for (let i = 0; i < 7; i++)
|
for (let i = 0; i < 7; i++)
|
||||||
endpoint += possible[Math.floor(Math.random() * possible.length)];
|
endpoint += possible[Math.floor(Math.random() * possible.length)];
|
||||||
|
|
||||||
if (await Database.permalink.getNoteByEndpoint(endpoint) == -1)
|
if (await Database.PermaNotes.getNoteByEndpoint(endpoint) == -1)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,19 +6,19 @@ import {Logger} from '../logger';
|
|||||||
|
|
||||||
export class Password extends User {
|
export class Password extends User {
|
||||||
static async gen(passwordSecret) {
|
static async gen(passwordSecret) {
|
||||||
let prehash = await sha256(passwordSecret);
|
const prehash = await sha256(passwordSecret);
|
||||||
let toHash = Buffer.from(prehash).toString('base64');
|
const toHash = Buffer.from(prehash).toString('base64');
|
||||||
let salt = await bcrypt.genSaltSync(10);
|
const salt = await bcrypt.genSaltSync(10);
|
||||||
let hash = await bcrypt.hashSync(toHash, salt);
|
const hash = await bcrypt.hashSync(toHash, salt);
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async compare(password, hashToCompare) {
|
static async compare(password, hashToCompare) {
|
||||||
try {
|
try {
|
||||||
let prehash = await sha256(password);
|
const prehash = await sha256(password);
|
||||||
let toHash = Buffer.from(prehash).toString('base64');
|
const toHash = Buffer.from(prehash).toString('base64');
|
||||||
let res = await bcrypt.compareSync(toHash, hashToCompare);
|
const res = await bcrypt.compareSync(toHash, hashToCompare);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import {User} from './user';
|
|||||||
|
|
||||||
export class Token extends User {
|
export class Token extends User {
|
||||||
static async gen(status, clientID, clientSecret) {
|
static async gen(status, clientID, clientSecret) {
|
||||||
let preGen = clientID + ':' + clientSecret;
|
const preGen = clientID + ':' + clientSecret;
|
||||||
|
|
||||||
let token = await jwt.sign(status, preGen);
|
const token = await jwt.sign(status, preGen);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export class User extends BaseUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async insert() {
|
async insert() {
|
||||||
this._instance = await Database.users.newUser(this.id, this.username, this.password, this.email, this.ip, this.authcode)
|
this._instance = await Database.Users.newUser(this.id, this.username, this.password, this.email, this.ip, this.authcode);
|
||||||
if (this._instance == -1) return -1;
|
if (this._instance == -1) return -1;
|
||||||
Logger.debug(`New user [${this.id}] ${this.username}`);
|
Logger.debug(`New user [${this.id}] ${this.username}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {Config} from './config/config';
|
|||||||
|
|
||||||
let app;
|
let app;
|
||||||
let server;
|
let server;
|
||||||
let router
|
let router;
|
||||||
|
|
||||||
export class Server {
|
export class Server {
|
||||||
static get App() {return app}
|
static get App() {return app}
|
||||||
|
|||||||
Reference in New Issue
Block a user