Added the jsonwebtoken package. Implemented a web jwt verify flow. Added missing JSON responses for api auth flows. Added jwt module. Moved bytes.js, logo.js, time.js and types.js to utils folder. Updated README.md. Implemented HA config check to allow API service configuration. Implemented JWT initialization. Replaced authorization cookie contents with JWT token. Implemented /api/vouchers endpoint. Updated /api endpoints list.

This commit is contained in:
Glenn de Haan
2024-04-12 18:01:42 +02:00
parent 78c891cdbd
commit 30d66b9dfc
10 changed files with 343 additions and 22 deletions

22
utils/bytes.js Normal file
View File

@@ -0,0 +1,22 @@
/**
* Converts bytes to a human readable format
*
* @param bytes
* @param type
* @param perSecond
* @param decimals
* @return {string}
*/
module.exports = (bytes, type = 0, perSecond = false, decimals = 2) => {
if (bytes === 0) return '0 Bytes';
const k = 1000;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'].toSpliced(0, type);
const i = Math.floor(Math.log(bytes) / Math.log(k));
const suffix = perSecond ? sizes[i] + 'ps' : sizes[i].toUpperCase();
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + suffix;
}

15
utils/logo.js Normal file
View File

@@ -0,0 +1,15 @@
/**
* Output the ascii logo
*/
module.exports = () => {
console.log(` __ __ _ _______ _ __ __ `);
console.log(` / / / /___ (_) ____(_) | | / /___ __ _______/ /_ ___ _____`);
console.log(` / / / / __ \\/ / /_ / / | | / / __ \\/ / / / ___/ __ \\/ _ \\/ ___/`);
console.log(`/ /_/ / / / / / __/ / / | |/ / /_/ / /_/ / /__/ / / / __/ / `);
console.log(`\\____/_/ /_/_/_/ /_/ |___/\\____/\\__,_/\\___/_/ /_/\\___/_/ `);
console.log('');
console.log(' UniFi Voucher ');
console.log(' By: Glenn de Haan ');
console.log(' https://github.com/glenndehaan/unifi-voucher-site ');
console.log('');
}

20
utils/time.js Normal file
View File

@@ -0,0 +1,20 @@
/**
* Convert time minutes
*
* @param minutes
* @returns {string}
*/
module.exports = (minutes) => {
if (minutes < 60) {
return `${minutes} minute(s)`;
}
const hours = minutes / 60;
if (hours < 24) {
return `${hours % 1 === 0 ? hours : hours.toFixed(2)} ${(hours > 1) ? 'hours' : 'hour'}`;
}
const days = hours / 24;
return `${days} ${(days > 1) ? 'days' : 'day'}`;
}

20
utils/types.js Normal file
View File

@@ -0,0 +1,20 @@
/**
* Returns an array or object of voucher type(s)
*
* @param string
* @param single
* @returns {*}
*/
module.exports = (string, single = false) => {
if(single) {
const match = string.match(/^(?<expiration>\d+)?,(?<usage>\d+)?,(?<upload>\d+)?,(?<download>\d+)?,(?<megabytes>\d+)?/);
return match.groups.expiration ? {...match.groups, raw: string} : undefined;
}
const types = string.split(';');
return types.filter(n => n).map((type) => {
const match = type.match(/^(?<expiration>\d+)?,(?<usage>\d+)?,(?<upload>\d+)?,(?<download>\d+)?,(?<megabytes>\d+)?/);
return match.groups.expiration ? {...match.groups, raw: type} : undefined;
}).filter(n => n);
}