summary refs log tree commit diff
path: root/src/api/util/utility
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/util/utility')
-rw-r--r--src/api/util/utility/Base64.ts3
-rw-r--r--src/api/util/utility/RandomInviteID.ts13
-rw-r--r--src/api/util/utility/String.ts14
-rw-r--r--src/api/util/utility/captcha.ts19
-rw-r--r--src/api/util/utility/ipAddress.ts44
-rw-r--r--src/api/util/utility/passwordStrength.ts20
6 files changed, 74 insertions, 39 deletions
diff --git a/src/api/util/utility/Base64.ts b/src/api/util/utility/Base64.ts

index 46cff77a..c10176f2 100644 --- a/src/api/util/utility/Base64.ts +++ b/src/api/util/utility/Base64.ts
@@ -1,4 +1,5 @@ -const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+"; +const alphabet = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+"; // binary to string lookup table const b2s = alphabet.split(""); diff --git a/src/api/util/utility/RandomInviteID.ts b/src/api/util/utility/RandomInviteID.ts
index 7ea344e0..bfed65bb 100644 --- a/src/api/util/utility/RandomInviteID.ts +++ b/src/api/util/utility/RandomInviteID.ts
@@ -2,7 +2,8 @@ import { Snowflake } from "@fosscord/util"; export function random(length = 6) { // Declare all characters - let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // Pick characers randomly let str = ""; @@ -15,18 +16,18 @@ export function random(length = 6) { export function snowflakeBasedInvite() { // Declare all characters - let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; let base = BigInt(chars.length); let snowflake = Snowflake.generateWorkerProcess(); // snowflakes hold ~10.75 characters worth of entropy; // safe to generate a 8-char invite out of them let str = ""; - for (let i=0; i < 10; i++) { - + for (let i = 0; i < 10; i++) { str.concat(chars.charAt(Number(snowflake % base))); snowflake = snowflake / base; } - - return str.substr(3,8).split("").reverse().join(""); + + return str.substr(3, 8).split("").reverse().join(""); } diff --git a/src/api/util/utility/String.ts b/src/api/util/utility/String.ts
index 982b7e11..0913013e 100644 --- a/src/api/util/utility/String.ts +++ b/src/api/util/utility/String.ts
@@ -2,13 +2,21 @@ import { Request } from "express"; import { ntob } from "./Base64"; import { FieldErrors } from "@fosscord/util"; -export function checkLength(str: string, min: number, max: number, key: string, req: Request) { +export function checkLength( + str: string, + min: number, + max: number, + key: string, + req: Request, +) { if (str.length < min || str.length > max) { throw FieldErrors({ [key]: { code: "BASE_TYPE_BAD_LENGTH", - message: req.t("common:field.BASE_TYPE_BAD_LENGTH", { length: `${min} - ${max}` }) - } + message: req.t("common:field.BASE_TYPE_BAD_LENGTH", { + length: `${min} - ${max}`, + }), + }, }); } } diff --git a/src/api/util/utility/captcha.ts b/src/api/util/utility/captcha.ts
index 739647d2..50e2c91a 100644 --- a/src/api/util/utility/captcha.ts +++ b/src/api/util/utility/captcha.ts
@@ -7,8 +7,8 @@ export interface hcaptchaResponse { hostname: string; credit: boolean; "error-codes": string[]; - score: number; // enterprise only - score_reason: string[]; // enterprise only + score: number; // enterprise only + score_reason: string[]; // enterprise only } export interface recaptchaResponse { @@ -23,7 +23,7 @@ export interface recaptchaResponse { const verifyEndpoints = { hcaptcha: "https://hcaptcha.com/siteverify", recaptcha: "https://www.google.com/recaptcha/api/siteverify", -} +}; export async function verifyCaptcha(response: string, ip?: string) { const { security } = Config.get(); @@ -36,11 +36,12 @@ export async function verifyCaptcha(response: string, ip?: string) { headers: { "Content-Type": "application/x-www-form-urlencoded", }, - body: `response=${encodeURIComponent(response)}` - + `&secret=${encodeURIComponent(secret!)}` - + `&sitekey=${encodeURIComponent(sitekey!)}` - + (ip ? `&remoteip=${encodeURIComponent(ip!)}` : ""), + body: + `response=${encodeURIComponent(response)}` + + `&secret=${encodeURIComponent(secret!)}` + + `&sitekey=${encodeURIComponent(sitekey!)}` + + (ip ? `&remoteip=${encodeURIComponent(ip!)}` : ""), }); - return await res.json() as hcaptchaResponse | recaptchaResponse; -} \ No newline at end of file + return (await res.json()) as hcaptchaResponse | recaptchaResponse; +} diff --git a/src/api/util/utility/ipAddress.ts b/src/api/util/utility/ipAddress.ts
index f17b145e..d166ebc5 100644 --- a/src/api/util/utility/ipAddress.ts +++ b/src/api/util/utility/ipAddress.ts
@@ -25,27 +25,27 @@ const exampleData = { name: "", domain: "", route: "", - type: "isp" + type: "isp", }, languages: [ { name: "", - native: "" - } + native: "", + }, ], currency: { name: "", code: "", symbol: "", native: "", - plural: "" + plural: "", }, time_zone: { name: "", abbr: "", offset: "", is_dst: true, - current_time: "" + current_time: "", }, threat: { is_tor: false, @@ -54,10 +54,10 @@ const exampleData = { is_known_attacker: false, is_known_abuser: false, is_threat: false, - is_bogon: false + is_bogon: false, }, count: 0, - status: 200 + status: 200, }; //TODO add function that support both ip and domain names @@ -65,7 +65,9 @@ export async function IPAnalysis(ip: string): Promise<typeof exampleData> { const { ipdataApiKey } = Config.get().security; if (!ipdataApiKey) return { ...exampleData, ip }; - return (await fetch(`https://api.ipdata.co/${ip}?api-key=${ipdataApiKey}`)).json() as any; // TODO: types + return ( + await fetch(`https://api.ipdata.co/${ip}?api-key=${ipdataApiKey}`) + ).json() as any; // TODO: types } export function isProxy(data: typeof exampleData) { @@ -77,19 +79,35 @@ export function isProxy(data: typeof exampleData) { } export function getIpAdress(req: Request): string { - // @ts-ignore - return req.headers[Config.get().security.forwadedFor] || req.socket.remoteAddress; + return ( + // @ts-ignore + req.headers[Config.get().security.forwadedFor] || + req.socket.remoteAddress + ); } export function distanceBetweenLocations(loc1: any, loc2: any): number { - return distanceBetweenCoords(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude); + return distanceBetweenCoords( + loc1.latitude, + loc1.longitude, + loc2.latitude, + loc2.longitude, + ); } //Haversine function -function distanceBetweenCoords(lat1: number, lon1: number, lat2: number, lon2: number) { +function distanceBetweenCoords( + lat1: number, + lon1: number, + lat2: number, + lon2: number, +) { const p = 0.017453292519943295; // Math.PI / 180 const c = Math.cos; - const a = 0.5 - c((lat2 - lat1) * p) / 2 + (c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2; + const a = + 0.5 - + c((lat2 - lat1) * p) / 2 + + (c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2; return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km } diff --git a/src/api/util/utility/passwordStrength.ts b/src/api/util/utility/passwordStrength.ts
index 439700d0..35c55999 100644 --- a/src/api/util/utility/passwordStrength.ts +++ b/src/api/util/utility/passwordStrength.ts
@@ -18,7 +18,8 @@ const blocklist: string[] = []; // TODO: update ones passwordblocklist is stored * Returns: 0 > pw > 1 */ export function checkPassword(password: string): number { - const { minLength, minNumbers, minUpperCase, minSymbols } = Config.get().register.password; + const { minLength, minNumbers, minUpperCase, minSymbols } = + Config.get().register.password; var strength = 0; // checks for total password len @@ -42,19 +43,24 @@ export function checkPassword(password: string): number { } // checks if password only consists of numbers or only consists of chars - if (password.length == password.count(reNUMBER) || password.length === password.count(reUPPERCASELETTER)) { + if ( + password.length == password.count(reNUMBER) || + password.length === password.count(reUPPERCASELETTER) + ) { strength = 0; } - + let entropyMap: { [key: string]: number } = {}; for (let i = 0; i < password.length; i++) { if (entropyMap[password[i]]) entropyMap[password[i]]++; else entropyMap[password[i]] = 1; } - + let entropies = Object.values(entropyMap); - - entropies.map(x => (x / entropyMap.length)); - strength += entropies.reduceRight((a: number, x: number) => a - (x * Math.log2(x))) / Math.log2(password.length); + + entropies.map((x) => x / entropyMap.length); + strength += + entropies.reduceRight((a: number, x: number) => a - x * Math.log2(x)) / + Math.log2(password.length); return strength; }