71 lines
1.6 KiB
JavaScript
71 lines
1.6 KiB
JavaScript
/* eslint-disable no-magic-numbers */
|
|
// @ts-nocheck
|
|
'use strict';
|
|
|
|
const express = require ('express');
|
|
const cookie_parser = require ('cookie-parser');
|
|
const auth = require ('./index');
|
|
const knex = require ('knex');
|
|
const path = require ('path');
|
|
const consts = require ('@scode/consts');
|
|
const crypto = require ('@scode/crypto-helper');
|
|
const fs = require ('fs-extra');
|
|
const password_helper = require ('@scode/password-helper');
|
|
|
|
|
|
/**
|
|
* start the server
|
|
*/
|
|
async function start_server () {
|
|
const db_path = path.join (__dirname, 'db.sqlite');
|
|
|
|
if (await fs.exists (db_path))
|
|
await fs.unlink (db_path);
|
|
|
|
const db = knex ({
|
|
client: 'sqlite',
|
|
connection: { filename: db_path },
|
|
useNullAsDefault: true
|
|
});
|
|
|
|
await db.schema.createTable ('users', (table) => {
|
|
table.increments ('id');
|
|
table.string ('name');
|
|
table.string ('password');
|
|
table.string ('salt');
|
|
});
|
|
|
|
const app = express ();
|
|
|
|
const name = 'testuser';
|
|
const salt = crypto.create_salt ();
|
|
const password = await password_helper.hash (
|
|
crypto.hash_sha512 ('foo', salt)
|
|
);
|
|
|
|
await db ('users')
|
|
.insert ({ name, password, salt });
|
|
|
|
app.use (cookie_parser ());
|
|
app.use (auth (async (user) => {
|
|
const users = await db ('users')
|
|
.select ('id', 'password', 'salt')
|
|
.where ({ name: user });
|
|
if (users.length === 1)
|
|
return users[0];
|
|
|
|
return null;
|
|
}));
|
|
|
|
app.get ('/', (req, res) => {
|
|
res.status (consts.http_consts.status_ok)
|
|
.end ('foo');
|
|
});
|
|
|
|
app.listen (3000);
|
|
|
|
return app;
|
|
}
|
|
|
|
module.exports = { start_server };
|