This repository has been archived on 2021-04-12. You can view files and clone it, but cannot push or open issues or pull requests.

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-03-04 13:53:23 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
2020-03-25 16:59:18 +01:00
* This file is part of Auth-Client-Helper which is released under MIT.
2020-03-04 21:02:59 +01:00
* See file 'LICENSE' for full license details.
2020-05-06 07:50:40 +02:00
* Created by Timo Hocker <timo@sapphirecode.ovh>, March 2020
2020-03-04 13:53:23 +01:00
*/
/* eslint-disable no-magic-numbers */
// @ts-nocheck
'use strict';
2020-05-06 07:50:40 +02:00
const crypto = require ('@sapphirecode/crypto-helper');
const consts = require ('@sapphirecode/consts');
2020-03-04 13:53:23 +01:00
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-07 18:20:37 +01:00
if (res.status !== consts.http.status_ok)
2020-03-06 17:21:58 +01:00
return '';
2020-03-04 13:53:23 +01:00
2020-03-06 17:23:44 +01:00
const session_id = await res.text ();
return session_id;
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) => {
2020-03-07 18:20:37 +01:00
if (res.status !== consts.http.status_ok)
2020-03-04 13:53:23 +01:00
throw new Error ('user or password invalid');
return res.text ();
});
}
module.exports = { login };