mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 13:59:16 +01:00
refactor(api): ♻️ Move all SSO account linking endpoint logic to OpenID plugin
This commit is contained in:
parent
6d4b4eb13b
commit
74ec563ba5
9 changed files with 267 additions and 286 deletions
38
plugins/openid/routes/sso/:id/index.test.ts
Normal file
38
plugins/openid/routes/sso/:id/index.test.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import { fakeRequest, getTestUsers } from "~/tests/utils";
|
||||
|
||||
const { deleteUsers, tokens } = await getTestUsers(1);
|
||||
|
||||
afterAll(async () => {
|
||||
await deleteUsers();
|
||||
});
|
||||
|
||||
// /api/v1/sso/:id
|
||||
describe("/api/v1/sso/:id", () => {
|
||||
test("should not find unknown issuer", async () => {
|
||||
const response = await fakeRequest("/api/v1/sso/unknown", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${tokens[0]?.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(await response.json()).toMatchObject({
|
||||
error: "Issuer with ID unknown not found in instance's OpenID configuration",
|
||||
});
|
||||
|
||||
const response2 = await fakeRequest("/api/v1/sso/unknown", {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${tokens[0]?.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
expect(response2.status).toBe(404);
|
||||
expect(await response2.json()).toMatchObject({
|
||||
error: "Issuer with ID unknown not found in instance's OpenID configuration",
|
||||
});
|
||||
});
|
||||
});
|
||||
208
plugins/openid/routes/sso/:id/index.ts
Normal file
208
plugins/openid/routes/sso/:id/index.ts
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
import { auth } from "@/api";
|
||||
import { proxyUrl } from "@/response";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "~/drizzle/db";
|
||||
import { OpenIdAccounts, RolePermissions } from "~/drizzle/schema";
|
||||
import type { PluginType } from "~/plugins/openid";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
export default (plugin: PluginType) => {
|
||||
plugin.registerRoute("/api/v1/sso", (app) => {
|
||||
app.openapi(
|
||||
createRoute({
|
||||
method: "get",
|
||||
path: "/api/v1/sso/{id}",
|
||||
summary: "Get linked account",
|
||||
middleware: [
|
||||
auth(
|
||||
{
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
required: [RolePermissions.OAuth],
|
||||
},
|
||||
),
|
||||
plugin.middleware,
|
||||
],
|
||||
request: {
|
||||
params: z.object({
|
||||
id: z.string(),
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "Linked account",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
icon: z.string().optional(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
401: {
|
||||
description: "Unauthorized",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ErrorSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description: "Account not found",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ErrorSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
async (context) => {
|
||||
const { id: issuerId } = context.req.valid("param");
|
||||
const { user } = context.get("auth");
|
||||
|
||||
if (!user) {
|
||||
return context.json(
|
||||
{
|
||||
error: "Unauthorized",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
const issuer = context
|
||||
.get("pluginConfig")
|
||||
.providers.find((provider) => provider.id === issuerId);
|
||||
|
||||
if (!issuer) {
|
||||
return context.json(
|
||||
{
|
||||
error: `Issuer with ID ${issuerId} not found in instance's OpenID configuration`,
|
||||
},
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
const account = await db.query.OpenIdAccounts.findFirst({
|
||||
where: (account, { eq, and }) =>
|
||||
and(
|
||||
eq(account.userId, user.id),
|
||||
eq(account.issuerId, issuerId),
|
||||
),
|
||||
});
|
||||
|
||||
if (!account) {
|
||||
return context.json(
|
||||
{
|
||||
error: "Account not found or is not linked to this issuer",
|
||||
},
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
return context.json(
|
||||
{
|
||||
id: issuer.id,
|
||||
name: issuer.name,
|
||||
icon: proxyUrl(issuer.icon) ?? undefined,
|
||||
},
|
||||
200,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
method: "delete",
|
||||
path: "/api/v1/sso/{id}",
|
||||
summary: "Unlink account",
|
||||
middleware: [
|
||||
auth(
|
||||
{
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
required: [RolePermissions.OAuth],
|
||||
},
|
||||
),
|
||||
plugin.middleware,
|
||||
],
|
||||
request: {
|
||||
params: z.object({
|
||||
id: z.string(),
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
204: {
|
||||
description: "Account unlinked",
|
||||
},
|
||||
401: {
|
||||
description: "Unauthorized",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ErrorSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description: "Account not found",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ErrorSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
async (context) => {
|
||||
const { id: issuerId } = context.req.valid("param");
|
||||
const { user } = context.get("auth");
|
||||
|
||||
if (!user) {
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
// Check if issuer exists
|
||||
const issuer = context
|
||||
.get("pluginConfig")
|
||||
.providers.find((provider) => provider.id === issuerId);
|
||||
|
||||
if (!issuer) {
|
||||
return context.json(
|
||||
{
|
||||
error: `Issuer with ID ${issuerId} not found in instance's OpenID configuration`,
|
||||
},
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
const account = await db.query.OpenIdAccounts.findFirst({
|
||||
where: (account, { eq, and }) =>
|
||||
and(
|
||||
eq(account.userId, user.id),
|
||||
eq(account.issuerId, issuerId),
|
||||
),
|
||||
});
|
||||
|
||||
if (!account) {
|
||||
return context.json(
|
||||
{
|
||||
error: "Account not found or is not linked to this issuer",
|
||||
},
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
await db
|
||||
.delete(OpenIdAccounts)
|
||||
.where(eq(OpenIdAccounts.id, account.id));
|
||||
|
||||
return context.newResponse(null, 204);
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue