2025-03-22 18:04:47 +01:00
|
|
|
import { RolePermission } from "@versia/client/schemas";
|
2025-06-15 23:50:34 +02:00
|
|
|
import { ApiError } from "@versia-server/kit";
|
|
|
|
|
import { auth, handleZodError } from "@versia-server/kit/api";
|
|
|
|
|
import { Application, db } from "@versia-server/kit/db";
|
|
|
|
|
import { OpenIdLoginFlows } from "@versia-server/kit/tables";
|
2025-03-30 22:10:33 +02:00
|
|
|
import { randomUUIDv7 } from "bun";
|
2025-03-29 03:30:06 +01:00
|
|
|
import { describeRoute } from "hono-openapi";
|
|
|
|
|
import { resolver, validator } from "hono-openapi/zod";
|
2024-09-24 14:42:39 +02:00
|
|
|
import {
|
|
|
|
|
calculatePKCECodeChallenge,
|
|
|
|
|
generateRandomCodeVerifier,
|
|
|
|
|
} from "oauth4webapi";
|
2025-03-29 03:30:06 +01:00
|
|
|
import { z } from "zod";
|
2024-10-04 15:22:48 +02:00
|
|
|
import type { PluginType } from "../../index.ts";
|
|
|
|
|
import { oauthDiscoveryRequest, oauthRedirectUri } from "../../utils.ts";
|
2024-09-24 14:42:39 +02:00
|
|
|
|
2024-11-02 00:43:33 +01:00
|
|
|
export default (plugin: PluginType): void => {
|
2024-09-24 14:42:39 +02:00
|
|
|
plugin.registerRoute("/api/v1/sso", (app) => {
|
2025-03-29 03:30:06 +01:00
|
|
|
app.get(
|
|
|
|
|
"/api/v1/sso",
|
|
|
|
|
describeRoute({
|
2024-09-24 14:42:39 +02:00
|
|
|
summary: "Get linked accounts",
|
2025-03-28 22:12:07 +01:00
|
|
|
tags: ["SSO"],
|
2024-09-24 14:42:39 +02:00
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Linked accounts",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-03-29 03:30:06 +01:00
|
|
|
schema: resolver(
|
|
|
|
|
z.array(
|
|
|
|
|
z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
name: z.string(),
|
|
|
|
|
icon: z.string().optional(),
|
|
|
|
|
}),
|
|
|
|
|
),
|
2024-09-24 14:42:39 +02:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-03-29 03:30:06 +01:00
|
|
|
}),
|
|
|
|
|
auth({
|
|
|
|
|
auth: true,
|
|
|
|
|
permissions: [RolePermission.OAuth],
|
|
|
|
|
}),
|
|
|
|
|
plugin.middleware,
|
2024-09-24 14:42:39 +02:00
|
|
|
async (context) => {
|
|
|
|
|
const { user } = context.get("auth");
|
|
|
|
|
|
2024-10-11 17:03:33 +02:00
|
|
|
const linkedAccounts = await user.getLinkedOidcAccounts(
|
|
|
|
|
context.get("pluginConfig").providers,
|
|
|
|
|
);
|
2024-09-24 14:42:39 +02:00
|
|
|
|
|
|
|
|
return context.json(
|
|
|
|
|
linkedAccounts.map((account) => ({
|
|
|
|
|
id: account.id,
|
|
|
|
|
name: account.name,
|
|
|
|
|
icon: account.icon,
|
|
|
|
|
})),
|
|
|
|
|
200,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
app.post(
|
|
|
|
|
"/api/v1/sso",
|
|
|
|
|
describeRoute({
|
2024-09-24 14:42:39 +02:00
|
|
|
summary: "Link account",
|
2025-03-28 22:12:07 +01:00
|
|
|
tags: ["SSO"],
|
2024-09-24 14:42:39 +02:00
|
|
|
responses: {
|
|
|
|
|
302: {
|
|
|
|
|
description: "Redirect to OpenID provider",
|
|
|
|
|
},
|
|
|
|
|
404: {
|
|
|
|
|
description: "Issuer not found",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-03-29 03:30:06 +01:00
|
|
|
schema: resolver(ApiError.zodSchema),
|
2024-09-24 14:42:39 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-03-29 03:30:06 +01:00
|
|
|
}),
|
|
|
|
|
auth({
|
|
|
|
|
auth: true,
|
|
|
|
|
permissions: [RolePermission.OAuth],
|
|
|
|
|
}),
|
|
|
|
|
plugin.middleware,
|
|
|
|
|
validator("json", z.object({ issuer: z.string() }), handleZodError),
|
2024-09-24 14:42:39 +02:00
|
|
|
async (context) => {
|
|
|
|
|
const { user } = context.get("auth");
|
|
|
|
|
|
|
|
|
|
const { issuer: issuerId } = context.req.valid("json");
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-01 16:32:18 +01:00
|
|
|
const authServer = await oauthDiscoveryRequest(
|
|
|
|
|
new URL(issuer.url),
|
|
|
|
|
);
|
2024-09-24 14:42:39 +02:00
|
|
|
|
|
|
|
|
const codeVerifier = generateRandomCodeVerifier();
|
|
|
|
|
|
|
|
|
|
const redirectUri = oauthRedirectUri(
|
|
|
|
|
context.get("config").http.base_url,
|
2024-10-11 17:09:51 +02:00
|
|
|
issuerId,
|
2024-09-24 14:42:39 +02:00
|
|
|
);
|
|
|
|
|
|
2024-10-23 17:56:47 +02:00
|
|
|
const application = await Application.insert({
|
2025-03-30 22:10:33 +02:00
|
|
|
id: randomUUIDv7(),
|
2024-10-23 17:56:47 +02:00
|
|
|
clientId:
|
|
|
|
|
user.id +
|
|
|
|
|
Buffer.from(
|
|
|
|
|
crypto.getRandomValues(new Uint8Array(32)),
|
|
|
|
|
).toString("base64"),
|
|
|
|
|
name: "Versia",
|
2025-02-01 16:32:18 +01:00
|
|
|
redirectUri: redirectUri.toString(),
|
2024-10-23 17:56:47 +02:00
|
|
|
scopes: "openid profile email",
|
|
|
|
|
secret: "",
|
|
|
|
|
});
|
2024-09-24 14:42:39 +02:00
|
|
|
|
|
|
|
|
// Store into database
|
|
|
|
|
const newFlow = (
|
|
|
|
|
await db
|
|
|
|
|
.insert(OpenIdLoginFlows)
|
|
|
|
|
.values({
|
2025-03-30 22:10:33 +02:00
|
|
|
id: randomUUIDv7(),
|
2024-09-24 14:42:39 +02:00
|
|
|
codeVerifier,
|
|
|
|
|
issuerId,
|
|
|
|
|
applicationId: application.id,
|
|
|
|
|
})
|
|
|
|
|
.returning()
|
|
|
|
|
)[0];
|
|
|
|
|
|
|
|
|
|
const codeChallenge =
|
|
|
|
|
await calculatePKCECodeChallenge(codeVerifier);
|
|
|
|
|
|
|
|
|
|
return context.redirect(
|
|
|
|
|
`${authServer.authorization_endpoint}?${new URLSearchParams(
|
|
|
|
|
{
|
|
|
|
|
client_id: issuer.client_id,
|
|
|
|
|
redirect_uri: `${redirectUri}?${new URLSearchParams(
|
|
|
|
|
{
|
|
|
|
|
flow: newFlow.id,
|
|
|
|
|
link: "true",
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
},
|
|
|
|
|
)}`,
|
|
|
|
|
response_type: "code",
|
|
|
|
|
scope: "openid profile email",
|
|
|
|
|
// PKCE
|
|
|
|
|
code_challenge_method: "S256",
|
|
|
|
|
code_challenge: codeChallenge,
|
|
|
|
|
},
|
|
|
|
|
).toString()}`,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
};
|