mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): 🚚 Use api/ for API routes instead of server/api/
This commit is contained in:
parent
dfc0bf4595
commit
3c1b330d4b
143 changed files with 5 additions and 5 deletions
26
api/well-known/host-meta/index.ts
Normal file
26
api/well-known/host-meta/index.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { xmlResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
ratelimits: {
|
||||
duration: 60,
|
||||
max: 60,
|
||||
},
|
||||
route: "/.well-known/host-meta",
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, () => {
|
||||
return xmlResponse(
|
||||
`<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="${new URL(
|
||||
"/.well-known/webfinger",
|
||||
config.http.base_url,
|
||||
).toString()}?resource={uri}"/></XRD>`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
43
api/well-known/jwks/index.ts
Normal file
43
api/well-known/jwks/index.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { exportJWK } from "jose";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
ratelimits: {
|
||||
duration: 30,
|
||||
max: 60,
|
||||
},
|
||||
route: "/.well-known/jwks",
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, async (context) => {
|
||||
const publicKey = await crypto.subtle.importKey(
|
||||
"spki",
|
||||
Buffer.from(config.oidc.jwt_key.split(";")[1], "base64"),
|
||||
"Ed25519",
|
||||
true,
|
||||
["verify"],
|
||||
);
|
||||
|
||||
const jwk = await exportJWK(publicKey);
|
||||
|
||||
// Remove the private key
|
||||
jwk.d = undefined;
|
||||
|
||||
return context.json({
|
||||
keys: [
|
||||
{
|
||||
...jwk,
|
||||
use: "sig",
|
||||
alg: "EdDSA",
|
||||
kid: "1",
|
||||
},
|
||||
],
|
||||
});
|
||||
}),
|
||||
);
|
||||
31
api/well-known/nodeinfo/2.0/index.ts
Normal file
31
api/well-known/nodeinfo/2.0/index.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import manifest from "~/package.json";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
ratelimits: {
|
||||
duration: 60,
|
||||
max: 500,
|
||||
},
|
||||
route: "/.well-known/nodeinfo/2.0",
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, (context) => {
|
||||
return context.json({
|
||||
version: "2.0",
|
||||
software: { name: "versia-server", version: manifest.version },
|
||||
protocols: ["versia"],
|
||||
services: { outbound: [], inbound: [] },
|
||||
usage: {
|
||||
users: { total: 0, activeMonth: 0, activeHalfyear: 0 },
|
||||
localPosts: 0,
|
||||
},
|
||||
openRegistrations: false,
|
||||
metadata: {},
|
||||
});
|
||||
}),
|
||||
);
|
||||
24
api/well-known/nodeinfo/index.ts
Normal file
24
api/well-known/nodeinfo/index.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { redirect } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
ratelimits: {
|
||||
duration: 60,
|
||||
max: 60,
|
||||
},
|
||||
route: "/.well-known/nodeinfo",
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, () => {
|
||||
return redirect(
|
||||
new URL("/.well-known/nodeinfo/2.0", config.http.base_url),
|
||||
301,
|
||||
);
|
||||
}),
|
||||
);
|
||||
33
api/well-known/openid-configuration/index.ts
Normal file
33
api/well-known/openid-configuration/index.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
ratelimits: {
|
||||
duration: 30,
|
||||
max: 60,
|
||||
},
|
||||
route: "/.well-known/openid-configuration",
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, (context) => {
|
||||
const baseUrl = new URL(config.http.base_url);
|
||||
return context.json({
|
||||
issuer: baseUrl.origin.toString(),
|
||||
authorization_endpoint: `${baseUrl.origin}/oauth/authorize`,
|
||||
token_endpoint: `${baseUrl.origin}/oauth/token`,
|
||||
userinfo_endpoint: `${baseUrl.origin}/api/v1/accounts/verify_credentials`,
|
||||
jwks_uri: `${baseUrl.origin}/.well-known/jwks`,
|
||||
response_types_supported: ["code"],
|
||||
subject_types_supported: ["public"],
|
||||
id_token_signing_alg_values_supported: ["EdDSA"],
|
||||
scopes_supported: ["openid", "profile", "email"],
|
||||
token_endpoint_auth_methods_supported: ["client_secret_basic"],
|
||||
claims_supported: ["sub"],
|
||||
});
|
||||
}),
|
||||
);
|
||||
43
api/well-known/versia.ts
Normal file
43
api/well-known/versia.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { urlToContentFormat } from "@/content_types";
|
||||
import type { InstanceMetadata } from "@versia/federation/types";
|
||||
import pkg from "~/package.json";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
ratelimits: {
|
||||
duration: 60,
|
||||
max: 60,
|
||||
},
|
||||
route: "/.well-known/versia",
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, (context) => {
|
||||
return context.json({
|
||||
type: "InstanceMetadata",
|
||||
compatibility: {
|
||||
extensions: ["pub.versia:custom_emojis"],
|
||||
versions: ["0.4.0"],
|
||||
},
|
||||
host: new URL(config.http.base_url).host,
|
||||
name: config.instance.name,
|
||||
description: config.instance.description,
|
||||
public_key: {
|
||||
key: config.instance.keys.public,
|
||||
algorithm: "ed25519",
|
||||
},
|
||||
software: {
|
||||
name: "Versia Server",
|
||||
version: pkg.version,
|
||||
},
|
||||
banner: urlToContentFormat(config.instance.banner),
|
||||
logo: urlToContentFormat(config.instance.logo),
|
||||
created_at: "2021-10-01T00:00:00Z",
|
||||
} satisfies InstanceMetadata);
|
||||
}),
|
||||
);
|
||||
129
api/well-known/webfinger/index.ts
Normal file
129
api/well-known/webfinger/index.ts
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import {
|
||||
apiRoute,
|
||||
applyConfig,
|
||||
handleZodError,
|
||||
idValidator,
|
||||
webfingerMention,
|
||||
} from "@/api";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
import type { ResponseError } from "@versia/federation";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { lookup } from "mime-types";
|
||||
import { z } from "zod";
|
||||
import { Users } from "~/drizzle/schema";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { User } from "~/packages/database-interface/user";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
ratelimits: {
|
||||
duration: 60,
|
||||
max: 60,
|
||||
},
|
||||
route: "/.well-known/webfinger",
|
||||
});
|
||||
|
||||
export const schemas = {
|
||||
query: z.object({
|
||||
resource: z.string().trim().min(1).max(512).startsWith("acct:"),
|
||||
}),
|
||||
};
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(
|
||||
meta.allowedMethods,
|
||||
meta.route,
|
||||
zValidator("query", schemas.query, handleZodError),
|
||||
async (context) => {
|
||||
const { resource } = context.req.valid("query");
|
||||
|
||||
// Check if resource is in the correct format (acct:uuid/username@domain)
|
||||
if (!resource.match(webfingerMention)) {
|
||||
return context.json(
|
||||
{
|
||||
error: "Invalid resource (should be acct:(id or username)@domain)",
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
const requestedUser = resource.split("acct:")[1];
|
||||
|
||||
const host = new URL(config.http.base_url).host;
|
||||
|
||||
// Check if user is a local user
|
||||
if (requestedUser.split("@")[1] !== host) {
|
||||
return context.json({ error: "User is a remote user" }, 404);
|
||||
}
|
||||
|
||||
const isUuid = requestedUser.split("@")[0].match(idValidator);
|
||||
|
||||
const user = await User.fromSql(
|
||||
and(
|
||||
eq(
|
||||
isUuid ? Users.id : Users.username,
|
||||
requestedUser.split("@")[0],
|
||||
),
|
||||
isNull(Users.instanceId),
|
||||
),
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
let activityPubUrl = "";
|
||||
|
||||
if (config.federation.bridge.enabled) {
|
||||
const manager = await User.getFederationRequester();
|
||||
|
||||
try {
|
||||
activityPubUrl = await manager.webFinger(
|
||||
user.data.username,
|
||||
new URL(config.http.base_url).host,
|
||||
"application/activity+json",
|
||||
config.federation.bridge.url,
|
||||
);
|
||||
} catch (e) {
|
||||
const error = e as ResponseError;
|
||||
|
||||
getLogger("federation")
|
||||
.error`Error from bridge: ${await error.response.data}`;
|
||||
}
|
||||
}
|
||||
|
||||
return context.json({
|
||||
subject: `acct:${
|
||||
isUuid ? user.id : user.data.username
|
||||
}@${host}`,
|
||||
|
||||
links: [
|
||||
// Keep the ActivityPub link first, because Misskey only searches
|
||||
// for the first link with rel="self" and doesn't check the type.
|
||||
activityPubUrl && {
|
||||
rel: "self",
|
||||
type: "application/activity+json",
|
||||
href: activityPubUrl,
|
||||
},
|
||||
{
|
||||
rel: "self",
|
||||
type: "application/json",
|
||||
href: new URL(
|
||||
`/users/${user.id}`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
},
|
||||
{
|
||||
rel: "avatar",
|
||||
type: lookup(user.getAvatarUrl(config)),
|
||||
href: user.getAvatarUrl(config),
|
||||
},
|
||||
].filter(Boolean),
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue