2020-03-04 13:53:23 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
2020-03-04 21:02:59 +01:00
|
|
|
* This file is part of auth-client-helper which is released under MIT.
|
|
|
|
* See file 'LICENSE' for full license details.
|
|
|
|
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
2020-03-04 13:53:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint-disable no-magic-numbers */
|
|
|
|
// @ts-nocheck
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const crypto = require ('@scode/crypto-helper');
|
|
|
|
const http_consts = require ('@scode/consts');
|
|
|
|
const fetch = require ('node-fetch');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* login
|
|
|
|
*
|
|
|
|
* @param {string} user username
|
|
|
|
* @param {string} password plain text password
|
|
|
|
* @param {string} host host to login on
|
|
|
|
*/
|
|
|
|
async function login (user, password, host = '') {
|
|
|
|
const salt_str = await fetch (`${host}/auth`, { headers: { user } })
|
2020-03-06 08:25:03 +01:00
|
|
|
.then (async (res) => {
|
2020-03-04 13:53:23 +01:00
|
|
|
if (res.status !== http_consts.status_ok)
|
2020-03-06 08:25:03 +01:00
|
|
|
return '');
|
2020-03-04 13:53:23 +01:00
|
|
|
|
2020-03-06 08:25:03 +01:00
|
|
|
return await res.text ();
|
2020-03-04 13:53:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (salt_str === '')
|
|
|
|
throw new Error ('user or password invalid');
|
|
|
|
|
|
|
|
const key = crypto.hash_sha512 (password, salt_str);
|
|
|
|
|
|
|
|
return fetch (`${host}/auth`, { headers: { user, key } })
|
|
|
|
.then ((res) => {
|
|
|
|
if (res.status !== http_consts.status_ok)
|
|
|
|
throw new Error ('user or password invalid');
|
|
|
|
return res.text ();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { login };
|