summary refs log tree commit diff
path: root/api/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/util')
-rw-r--r--api/src/util/handlers/Message.ts6
-rw-r--r--api/src/util/index.ts1
-rw-r--r--api/src/util/utility/captcha.ts46
3 files changed, 52 insertions, 1 deletions
diff --git a/api/src/util/handlers/Message.ts b/api/src/util/handlers/Message.ts

index 48f87dfe..ed17995b 100644 --- a/api/src/util/handlers/Message.ts +++ b/api/src/util/handlers/Message.ts
@@ -166,7 +166,11 @@ export async function postHandleMessage(message: Message) { const provider_name = $('meta[property="og:site_name"]').text(); const author_name = $('meta[property="article:author"]').attr("content"); const description = $('meta[property="og:description"]').attr("content") || $('meta[property="description"]').attr("content"); + const image = $('meta[property="og:image"]').attr("content"); + const width = parseInt($('meta[property="og:image:width"]').attr("content") || "") || undefined; + const height = parseInt($('meta[property="og:image:height"]').attr("content") || "") || undefined; + const url = $('meta[property="og:url"]').attr("content"); // TODO: color const embed: Embed = { @@ -177,7 +181,7 @@ export async function postHandleMessage(message: Message) { }; if (author_name) embed.author = { name: author_name }; - if (image) embed.thumbnail = { proxy_url: image, url: image }; + if (image) embed.thumbnail = { proxy_url: image, url: image, width: width, height: height }; if (title) embed.title = title; if (url) embed.url = url; if (description) embed.description = description; diff --git a/api/src/util/index.ts b/api/src/util/index.ts
index ffbcf24e..de6b6064 100644 --- a/api/src/util/index.ts +++ b/api/src/util/index.ts
@@ -6,3 +6,4 @@ export * from "./utility/RandomInviteID"; export * from "./handlers/route"; export * from "./utility/String"; export * from "./handlers/Voice"; +export * from "./utility/captcha"; \ No newline at end of file diff --git a/api/src/util/utility/captcha.ts b/api/src/util/utility/captcha.ts new file mode 100644
index 00000000..739647d2 --- /dev/null +++ b/api/src/util/utility/captcha.ts
@@ -0,0 +1,46 @@ +import { Config } from "@fosscord/util"; +import fetch from "node-fetch"; + +export interface hcaptchaResponse { + success: boolean; + challenge_ts: string; + hostname: string; + credit: boolean; + "error-codes": string[]; + score: number; // enterprise only + score_reason: string[]; // enterprise only +} + +export interface recaptchaResponse { + success: boolean; + score: number; // between 0 - 1 + action: string; + challenge_ts: string; + hostname: string; + "error-codes"?: string[]; +} + +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(); + const { service, secret, sitekey } = security.captcha; + + if (!service) throw new Error("Cannot verify captcha without service"); + + const res = await fetch(verifyEndpoints[service], { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + 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