init
This commit is contained in:
commit
a99162320b
17
.eslintrc.js
Normal file
17
.eslintrc.js
Normal file
@ -0,0 +1,17 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
commonjs: true,
|
||||
es6: true,
|
||||
node: true
|
||||
},
|
||||
extends: [
|
||||
'@scode'
|
||||
],
|
||||
globals: {
|
||||
Atomics: 'readonly',
|
||||
SharedArrayBuffer: 'readonly'
|
||||
},
|
||||
parserOptions: {
|
||||
ecmaVersion: 2018
|
||||
}
|
||||
}
|
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
/.nyc_output/
|
||||
/coverage/
|
44
index.js
Normal file
44
index.js
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||
* Created by Timo Hocker <timo@scode.ovh>, January 2020
|
||||
*/
|
||||
|
||||
/* 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 } })
|
||||
.then ((res) => {
|
||||
if (res.status !== http_consts.status_ok)
|
||||
return new Promise ((resolve) => resolve (''));
|
||||
|
||||
return res.text ();
|
||||
});
|
||||
|
||||
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 };
|
21
package.json
Normal file
21
package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "auth-client-helper",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"author": "Timo Hocker <t-hocker@web.de>",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@scode/eslint-config": "^1.2.25",
|
||||
"ava": "^3.5.0",
|
||||
"eslint": "^6.8.0",
|
||||
"nyc": "^15.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"test": "nyc ava"
|
||||
},
|
||||
"dependencies": {
|
||||
"@scode/consts": "^1.0.1",
|
||||
"@scode/crypto-helper": "^1.0.2"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user