mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
Replace eslint and prettier with Biome
This commit is contained in:
parent
4a5a2ea590
commit
af0d627f19
199 changed files with 16493 additions and 16361 deletions
|
|
@ -1,105 +1,103 @@
|
|||
import { randomBytes } from "node:crypto";
|
||||
import { apiRoute, applyConfig } from "@api";
|
||||
import { randomBytes } from "crypto";
|
||||
import { client } from "~database/datasource";
|
||||
import { TokenType } from "~database/entities/Token";
|
||||
import { userRelations } from "~database/entities/relations";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["POST"],
|
||||
ratelimits: {
|
||||
max: 4,
|
||||
duration: 60,
|
||||
},
|
||||
route: "/auth/login",
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
allowedMethods: ["POST"],
|
||||
ratelimits: {
|
||||
max: 4,
|
||||
duration: 60,
|
||||
},
|
||||
route: "/auth/login",
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* OAuth Code flow
|
||||
*/
|
||||
export default apiRoute<{
|
||||
email: string;
|
||||
password: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}>(async (req, matchedRoute, extraData) => {
|
||||
const scopes = (matchedRoute.query.scope || "")
|
||||
.replaceAll("+", " ")
|
||||
.split(" ");
|
||||
const redirect_uri = matchedRoute.query.redirect_uri;
|
||||
const response_type = matchedRoute.query.response_type;
|
||||
const client_id = matchedRoute.query.client_id;
|
||||
const scopes = (matchedRoute.query.scope || "")
|
||||
.replaceAll("+", " ")
|
||||
.split(" ");
|
||||
const redirect_uri = matchedRoute.query.redirect_uri;
|
||||
const response_type = matchedRoute.query.response_type;
|
||||
const client_id = matchedRoute.query.client_id;
|
||||
|
||||
const { email, password } = extraData.parsedRequest;
|
||||
const { email, password } = extraData.parsedRequest;
|
||||
|
||||
const redirectToLogin = (error: string) =>
|
||||
Response.redirect(
|
||||
`/oauth/authorize?` +
|
||||
new URLSearchParams({
|
||||
...matchedRoute.query,
|
||||
error: encodeURIComponent(error),
|
||||
}).toString(),
|
||||
302
|
||||
);
|
||||
const redirectToLogin = (error: string) =>
|
||||
Response.redirect(
|
||||
`/oauth/authorize?${new URLSearchParams({
|
||||
...matchedRoute.query,
|
||||
error: encodeURIComponent(error),
|
||||
}).toString()}`,
|
||||
302,
|
||||
);
|
||||
|
||||
if (response_type !== "code")
|
||||
return redirectToLogin("Invalid response_type");
|
||||
if (response_type !== "code")
|
||||
return redirectToLogin("Invalid response_type");
|
||||
|
||||
if (!email || !password)
|
||||
return redirectToLogin("Invalid username or password");
|
||||
if (!email || !password)
|
||||
return redirectToLogin("Invalid username or password");
|
||||
|
||||
// Get user
|
||||
const user = await client.user.findFirst({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
include: userRelations,
|
||||
});
|
||||
// Get user
|
||||
const user = await client.user.findFirst({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
include: userRelations,
|
||||
});
|
||||
|
||||
if (!user || !(await Bun.password.verify(password, user.password || "")))
|
||||
return redirectToLogin("Invalid username or password");
|
||||
if (!user || !(await Bun.password.verify(password, user.password || "")))
|
||||
return redirectToLogin("Invalid username or password");
|
||||
|
||||
// Get application
|
||||
const application = await client.application.findFirst({
|
||||
where: {
|
||||
client_id,
|
||||
},
|
||||
});
|
||||
// Get application
|
||||
const application = await client.application.findFirst({
|
||||
where: {
|
||||
client_id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!application) return redirectToLogin("Invalid client_id");
|
||||
if (!application) return redirectToLogin("Invalid client_id");
|
||||
|
||||
const code = randomBytes(32).toString("hex");
|
||||
const code = randomBytes(32).toString("hex");
|
||||
|
||||
await client.application.update({
|
||||
where: { id: application.id },
|
||||
data: {
|
||||
tokens: {
|
||||
create: {
|
||||
access_token: randomBytes(64).toString("base64url"),
|
||||
code: code,
|
||||
scope: scopes.join(" "),
|
||||
token_type: TokenType.BEARER,
|
||||
user: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
await client.application.update({
|
||||
where: { id: application.id },
|
||||
data: {
|
||||
tokens: {
|
||||
create: {
|
||||
access_token: randomBytes(64).toString("base64url"),
|
||||
code: code,
|
||||
scope: scopes.join(" "),
|
||||
token_type: TokenType.BEARER,
|
||||
user: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Redirect to OAuth confirmation screen
|
||||
return Response.redirect(
|
||||
`/oauth/redirect?` +
|
||||
new URLSearchParams({
|
||||
redirect_uri,
|
||||
code,
|
||||
client_id,
|
||||
application: application.name,
|
||||
website: application.website ?? "",
|
||||
scope: scopes.join(" "),
|
||||
}).toString(),
|
||||
302
|
||||
);
|
||||
// Redirect to OAuth confirmation screen
|
||||
return Response.redirect(
|
||||
`/oauth/redirect?${new URLSearchParams({
|
||||
redirect_uri,
|
||||
code,
|
||||
client_id,
|
||||
application: application.name,
|
||||
website: application.website ?? "",
|
||||
scope: scopes.join(" "),
|
||||
}).toString()}`,
|
||||
302,
|
||||
);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,56 +3,55 @@ import { client } from "~database/datasource";
|
|||
import { userRelations } from "~database/entities/relations";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["POST"],
|
||||
ratelimits: {
|
||||
max: 4,
|
||||
duration: 60,
|
||||
},
|
||||
route: "/auth/redirect",
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
allowedMethods: ["POST"],
|
||||
ratelimits: {
|
||||
max: 4,
|
||||
duration: 60,
|
||||
},
|
||||
route: "/auth/redirect",
|
||||
auth: {
|
||||
required: false,
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* OAuth Code flow
|
||||
*/
|
||||
export default apiRoute<{
|
||||
email: string;
|
||||
password: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}>(async (req, matchedRoute) => {
|
||||
const redirect_uri = decodeURIComponent(matchedRoute.query.redirect_uri);
|
||||
const client_id = matchedRoute.query.client_id;
|
||||
const code = matchedRoute.query.code;
|
||||
const redirect_uri = decodeURIComponent(matchedRoute.query.redirect_uri);
|
||||
const client_id = matchedRoute.query.client_id;
|
||||
const code = matchedRoute.query.code;
|
||||
|
||||
const redirectToLogin = (error: string) =>
|
||||
Response.redirect(
|
||||
`/oauth/authorize?` +
|
||||
new URLSearchParams({
|
||||
...matchedRoute.query,
|
||||
error: encodeURIComponent(error),
|
||||
}).toString(),
|
||||
302
|
||||
);
|
||||
const redirectToLogin = (error: string) =>
|
||||
Response.redirect(
|
||||
`/oauth/authorize?${new URLSearchParams({
|
||||
...matchedRoute.query,
|
||||
error: encodeURIComponent(error),
|
||||
}).toString()}`,
|
||||
302,
|
||||
);
|
||||
|
||||
// Get token
|
||||
const token = await client.token.findFirst({
|
||||
where: {
|
||||
code,
|
||||
application: {
|
||||
client_id,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
include: userRelations,
|
||||
},
|
||||
application: true,
|
||||
},
|
||||
});
|
||||
// Get token
|
||||
const token = await client.token.findFirst({
|
||||
where: {
|
||||
code,
|
||||
application: {
|
||||
client_id,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
include: userRelations,
|
||||
},
|
||||
application: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!token) return redirectToLogin("Invalid code");
|
||||
if (!token) return redirectToLogin("Invalid code");
|
||||
|
||||
// Redirect back to application
|
||||
return Response.redirect(`${redirect_uri}?code=${code}`, 302);
|
||||
// Redirect back to application
|
||||
return Response.redirect(`${redirect_uri}?code=${code}`, 302);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue