refactor: ♻️ Always use explicit types in every function

This commit is contained in:
Jesse Wierzbinski 2024-11-02 00:43:33 +01:00
parent 54cea29ce9
commit c1dcdc78ae
No known key found for this signature in database
62 changed files with 359 additions and 226 deletions

View file

@ -63,7 +63,7 @@ const schemas = {
}),
};
export default (plugin: PluginType) =>
export default (plugin: PluginType): void =>
plugin.registerRoute("/oauth/authorize", (app) =>
app.openapi(
{

View file

@ -3,7 +3,7 @@ import { createRoute, z } from "@hono/zod-openapi";
import { exportJWK } from "jose";
import type { PluginType } from "../index.ts";
export default (plugin: PluginType) => {
export default (plugin: PluginType): void => {
plugin.registerRoute("/.well-known/jwks", (app) =>
app.openapi(
createRoute({

View file

@ -2,7 +2,7 @@ import { randomString } from "@/math.ts";
import { setCookie } from "@hono/hono/cookie";
import { createRoute, z } from "@hono/zod-openapi";
import { User, db } from "@versia/kit/db";
import { and, eq, isNull } from "@versia/kit/drizzle";
import { type SQL, and, eq, isNull } from "@versia/kit/drizzle";
import {
OpenIdAccounts,
RolePermissions,
@ -29,7 +29,7 @@ export const schemas = {
}),
};
export default (plugin: PluginType) => {
export default (plugin: PluginType): void => {
plugin.registerRoute("/oauth/sso/{issuer}/callback", (app) => {
app.openapi(
createRoute({
@ -150,7 +150,7 @@ export default (plugin: PluginType) => {
// Check if account is already linked
const account = await db.query.OpenIdAccounts.findFirst({
where: (account, { eq, and }) =>
where: (account, { eq, and }): SQL | undefined =>
and(
eq(account.serverId, sub),
eq(account.issuerId, issuer.id),
@ -188,7 +188,7 @@ export default (plugin: PluginType) => {
let userId = (
await db.query.OpenIdAccounts.findFirst({
where: (account, { eq, and }) =>
where: (account, { eq, and }): SQL | undefined =>
and(
eq(account.serverId, sub),
eq(account.issuerId, issuer.id),

View file

@ -1,7 +1,7 @@
import { jsonOrForm } from "@/api";
import { createRoute, z } from "@hono/zod-openapi";
import { db } from "@versia/kit/db";
import { eq } from "@versia/kit/drizzle";
import { type SQL, eq } from "@versia/kit/drizzle";
import { Tokens } from "@versia/kit/tables";
import type { PluginType } from "../../index.ts";
@ -13,7 +13,7 @@ export const schemas = {
}),
};
export default (plugin: PluginType) => {
export default (plugin: PluginType): void => {
plugin.registerRoute("/oauth/revoke", (app) => {
app.openapi(
createRoute({
@ -63,7 +63,7 @@ export default (plugin: PluginType) => {
context.req.valid("json");
const foundToken = await db.query.Tokens.findFirst({
where: (tokenTable, { eq, and }) =>
where: (tokenTable, { eq, and }): SQL | undefined =>
and(
eq(tokenTable.accessToken, token ?? ""),
eq(tokenTable.clientId, client_id),

View file

@ -20,7 +20,7 @@ export const schemas = {
}),
};
export default (plugin: PluginType) => {
export default (plugin: PluginType): void => {
plugin.registerRoute("/oauth/sso", (app) => {
app.openapi(
createRoute({

View file

@ -1,7 +1,7 @@
import { jsonOrForm } from "@/api";
import { createRoute, z } from "@hono/zod-openapi";
import { Application, db } from "@versia/kit/db";
import { eq } from "@versia/kit/drizzle";
import { type SQL, eq } from "@versia/kit/drizzle";
import { Tokens } from "@versia/kit/tables";
import type { PluginType } from "../../index.ts";
@ -38,7 +38,7 @@ export const schemas = {
}),
};
export default (plugin: PluginType) => {
export default (plugin: PluginType): void => {
plugin.registerRoute("/oauth/token", (app) => {
app.openapi(
createRoute({
@ -155,7 +155,7 @@ export default (plugin: PluginType) => {
}
const token = await db.query.Tokens.findFirst({
where: (token, { eq, and }) =>
where: (token, { eq, and }): SQL | undefined =>
and(
eq(token.code, code),
eq(

View file

@ -2,12 +2,12 @@ import { auth } from "@/api";
import { proxyUrl } from "@/response";
import { createRoute, z } from "@hono/zod-openapi";
import { db } from "@versia/kit/db";
import { eq } from "@versia/kit/drizzle";
import { type SQL, eq } from "@versia/kit/drizzle";
import { OpenIdAccounts, RolePermissions } from "@versia/kit/tables";
import type { PluginType } from "~/plugins/openid";
import { ErrorSchema } from "~/types/api";
export default (plugin: PluginType) => {
export default (plugin: PluginType): void => {
plugin.registerRoute("/api/v1/sso", (app) => {
app.openapi(
createRoute({
@ -88,7 +88,7 @@ export default (plugin: PluginType) => {
}
const account = await db.query.OpenIdAccounts.findFirst({
where: (account, { eq, and }) =>
where: (account, { eq, and }): SQL | undefined =>
and(
eq(account.userId, user.id),
eq(account.issuerId, issuerId),
@ -181,7 +181,7 @@ export default (plugin: PluginType) => {
}
const account = await db.query.OpenIdAccounts.findFirst({
where: (account, { eq, and }) =>
where: (account, { eq, and }): SQL | undefined =>
and(
eq(account.userId, user.id),
eq(account.issuerId, issuerId),

View file

@ -10,7 +10,7 @@ import { ErrorSchema } from "~/types/api";
import type { PluginType } from "../../index.ts";
import { oauthDiscoveryRequest, oauthRedirectUri } from "../../utils.ts";
export default (plugin: PluginType) => {
export default (plugin: PluginType): void => {
plugin.registerRoute("/api/v1/sso", (app) => {
app.openapi(
{

View file

@ -1,5 +1,5 @@
import { db } from "@versia/kit/db";
import type { InferSelectModel } from "@versia/kit/drizzle";
import type { InferSelectModel, SQL } from "@versia/kit/drizzle";
import type { Applications, OpenIdLoginFlows } from "@versia/kit/tables";
import {
type AuthorizationResponseError,
@ -7,6 +7,7 @@ import {
ClientSecretPost,
type ResponseBodyError,
type TokenEndpointResponse,
type UserInfoResponse,
authorizationCodeGrantRequest,
discoveryRequest,
expectNoState,
@ -17,6 +18,7 @@ import {
userInfoRequest,
validateAuthResponse,
} from "oauth4webapi";
import type { ApplicationType } from "~/classes/database/application";
export const oauthDiscoveryRequest = (
issuerUrl: string | URL,
@ -28,12 +30,19 @@ export const oauthDiscoveryRequest = (
}).then((res) => processDiscoveryResponse(issuerUrlurl, res));
};
export const oauthRedirectUri = (baseUrl: string, issuer: string) =>
export const oauthRedirectUri = (baseUrl: string, issuer: string): string =>
new URL(`/oauth/sso/${issuer}/callback`, baseUrl).toString();
const getFlow = (flowId: string) => {
const getFlow = (
flowId: string,
): Promise<
| (InferSelectModel<typeof OpenIdLoginFlows> & {
application?: ApplicationType | null;
})
| undefined
> => {
return db.query.OpenIdLoginFlows.findFirst({
where: (flow, { eq }) => eq(flow.id, flowId),
where: (flow, { eq }): SQL | undefined => eq(flow.id, flowId),
with: {
application: true,
},
@ -100,7 +109,7 @@ const getUserInfo = (
clientId: string,
accessToken: string,
sub: string,
) => {
): Promise<UserInfoResponse> => {
return userInfoRequest(
authServer,
{
@ -138,7 +147,16 @@ export const automaticOidcFlow = async (
})
| null,
) => Response,
) => {
): Promise<
| Response
| {
userInfo: UserInfoResponse;
flow: InferSelectModel<typeof OpenIdLoginFlows> & {
application?: ApplicationType | null;
};
claims: Record<string, unknown>;
}
> => {
const flow = await getFlow(flowId);
if (!flow) {