auth-server-helper/mock_server.js

46 lines
925 B
JavaScript

/* eslint-disable no-magic-numbers */
// @ts-nocheck
'use strict';
const express = require ('express');
const auth = require ('./index');
const consts = require ('@scode/consts');
const crypto = require ('@scode/crypto-helper');
const password_helper = require ('@scode/password-helper');
/**
* start the server
*/
async function start_server () {
const app = express ();
const name = 'testuser';
const salt = crypto.create_salt ();
const password = await password_helper.hash (
crypto.hash_sha512 ('foo', salt)
);
const user = { name, salt, password };
app.use (auth ((user_name) => {
if (user.name === user_name)
return user;
return null;
}, [
/noauthreg/u,
{ method: 'POST', regex: /noauthobj/u }
]));
app.use ((req, res) => {
res.status (consts.http.status_ok)
.end ('foo');
});
app.listen (3000);
return app;
}
module.exports = { start_server };