refactor(netlify): rework the webfinger edge function

This commit is contained in:
Harsh Shandilya 2022-11-16 11:56:09 +05:30
parent 1b8466c0b4
commit abe3f0ca96
1 changed files with 40 additions and 24 deletions

View File

@ -1,28 +1,44 @@
import type { Context } from "https://edge.netlify.com";
export default async (request: Request, context: Context) => {
context.log(request.url);
return context.json({
subject: "acct:msfjarvis@androiddev.social",
aliases: [
"https://androiddev.social/@msfjarvis",
"https://androiddev.social/users/msfjarvis",
],
links: [
{
rel: "http://webfinger.net/rel/profile-page",
type: "text/html",
href: "https://androiddev.social/@msfjarvis",
},
{
rel: "self",
type: "application/activity+json",
href: "https://androiddev.social/users/msfjarvis",
},
{
rel: "http://ostatus.org/schema/1.0/subscribe",
template: "https://androiddev.social/authorize_interaction?uri={uri}",
},
],
});
const url = new URL(request.url);
const resourceParam = url.searchParams.get("resource");
context.log(`resourceParam=${resourceParam}`);
if (resourceParam === null) {
return context.json({
error: "No 'resource' query parameter was provided",
});
} else {
const re = /acct:(.*)@msfjarvis.dev/;
if (resourceParam.match(re) !== null) {
return context.json({
subject: "acct:msfjarvis@androiddev.social",
aliases: [
"https://androiddev.social/@msfjarvis",
"https://androiddev.social/users/msfjarvis",
],
links: [
{
rel: "http://webfinger.net/rel/profile-page",
type: "text/html",
href: "https://androiddev.social/@msfjarvis",
},
{
rel: "self",
type: "application/activity+json",
href: "https://androiddev.social/users/msfjarvis",
},
{
rel: "http://ostatus.org/schema/1.0/subscribe",
template:
"https://androiddev.social/authorize_interaction?uri={uri}",
},
],
});
} else {
return context.json({
error: "This domain only works for @msfjarvis.dev requests",
});
}
}
};