auth-server-helper/mock_server.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-03-25 16:58:43 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
2020-05-17 17:37:41 +02:00
* This file is part of auth-server-helper which is released under MIT.
2020-03-25 16:58:43 +01:00
* See file 'LICENSE' for full license details.
2020-05-17 17:37:41 +02:00
* Created by Timo Hocker <timo@scode.ovh>, May 2020
2020-03-25 16:58:43 +01:00
*/
2020-03-06 12:06:10 +01:00
/* eslint-disable no-magic-numbers */
// @ts-nocheck
'use strict';
const express = require ('express');
const auth = require ('./index');
2020-05-06 07:50:42 +02:00
const consts = require ('@sapphirecode/consts');
const crypto = require ('@sapphirecode/crypto-helper');
const password_helper = require ('@sapphirecode/password-helper');
2020-03-06 12:06:10 +01:00
/**
* start the server
*/
async function start_server () {
const app = express ();
2020-03-07 17:56:41 +01:00
const name = 'testuser';
2020-03-06 12:06:10 +01:00
const salt = crypto.create_salt ();
2020-03-07 17:56:41 +01:00
const password = await password_helper.hash (
crypto.hash_sha512 ('foo', salt)
);
2020-03-08 14:03:18 +01:00
const user = { name, salt, password };
2020-03-06 12:06:10 +01:00
2020-03-08 14:03:18 +01:00
app.use (auth ((user_name) => {
if (user.name === user_name)
return user;
2020-03-07 17:56:41 +01:00
return null;
2020-03-13 20:08:38 +01:00
}, [
/noauthreg/u,
{ method: 'POST', regex: /noauthobj/u }
]));
2020-03-06 12:06:10 +01:00
2020-03-13 20:08:38 +01:00
app.use ((req, res) => {
2020-03-08 14:03:18 +01:00
res.status (consts.http.status_ok)
2020-03-06 12:06:10 +01:00
.end ('foo');
});
app.listen (3000);
return app;
}
module.exports = { start_server };