summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2023-03-25 18:01:56 -0400
committerPuyodead1 <puyodead@proton.me>2023-04-13 15:39:31 -0400
commit860b9d583e720ba5e3d003e1eaca81faf22e360f (patch)
treed5c8301f756fe4e4a3b85dc7f58f6003f5f62c65 /src
parentoapi: users progress (diff)
downloadserver-860b9d583e720ba5e3d003e1eaca81faf22e360f.tar.xz
oapi: finish users
Diffstat (limited to 'src')
-rw-r--r--src/api/routes/applications/#id/bot/index.ts4
-rw-r--r--src/api/routes/auth/register.ts2
-rw-r--r--src/api/routes/auth/reset.ts2
-rw-r--r--src/api/routes/users/@me/guilds/#guild_id/settings.ts36
-rw-r--r--src/api/routes/users/@me/mfa/totp/disable.ts12
-rw-r--r--src/api/routes/users/@me/mfa/totp/enable.ts15
-rw-r--r--src/api/routes/users/@me/mfa/webauthn/credentials/#key_id/index.ts34
-rw-r--r--src/api/routes/users/@me/mfa/webauthn/credentials/index.ts12
-rw-r--r--src/util/schemas/responses/TokenResponse.ts11
-rw-r--r--src/util/schemas/responses/UserResponse.ts11
-rw-r--r--src/util/schemas/responses/WebAuthnCreateResponse.ts4
-rw-r--r--src/util/schemas/responses/index.ts1
12 files changed, 107 insertions, 37 deletions
diff --git a/src/api/routes/applications/#id/bot/index.ts b/src/api/routes/applications/#id/bot/index.ts
index 5eea27e9..0a6e6fd4 100644
--- a/src/api/routes/applications/#id/bot/index.ts
+++ b/src/api/routes/applications/#id/bot/index.ts
@@ -35,8 +35,8 @@ router.post(
 	"/",
 	route({
 		responses: {
-			200: {
-				body: "TokenResponse",
+			204: {
+				body: "TokenOnlyResponse",
 			},
 			400: {
 				body: "APIErrorResponse",
diff --git a/src/api/routes/auth/register.ts b/src/api/routes/auth/register.ts
index 73a28fed..321b4a65 100644
--- a/src/api/routes/auth/register.ts
+++ b/src/api/routes/auth/register.ts
@@ -45,7 +45,7 @@ router.post(
 	route({
 		requestBody: "RegisterSchema",
 		responses: {
-			200: { body: "TokenResponse" },
+			200: { body: "TokenOnlyResponse" },
 			400: { body: "APIErrorOrCaptchaResponse" },
 		},
 	}),
diff --git a/src/api/routes/auth/reset.ts b/src/api/routes/auth/reset.ts
index cbfa4935..f97045a6 100644
--- a/src/api/routes/auth/reset.ts
+++ b/src/api/routes/auth/reset.ts
@@ -38,7 +38,7 @@ router.post(
 		requestBody: "PasswordResetSchema",
 		responses: {
 			200: {
-				body: "TokenResponse",
+				body: "TokenOnlyResponse",
 			},
 			400: {
 				body: "APIErrorOrCaptchaResponse",
diff --git a/src/api/routes/users/@me/guilds/#guild_id/settings.ts b/src/api/routes/users/@me/guilds/#guild_id/settings.ts
index 2ae82423..ac6586ce 100644
--- a/src/api/routes/users/@me/guilds/#guild_id/settings.ts
+++ b/src/api/routes/users/@me/guilds/#guild_id/settings.ts
@@ -28,17 +28,37 @@ import { Request, Response, Router } from "express";
 const router = Router();
 
 // GET doesn't exist on discord.com
-router.get("/", route({}), async (req: Request, res: Response) => {
-	const user = await Member.findOneOrFail({
-		where: { id: req.user_id, guild_id: req.params.guild_id },
-		select: ["settings"],
-	});
-	return res.json(user.settings);
-});
+router.get(
+	"/",
+	route({
+		responses: {
+			200: {},
+			404: {},
+		},
+	}),
+	async (req: Request, res: Response) => {
+		const user = await Member.findOneOrFail({
+			where: { id: req.user_id, guild_id: req.params.guild_id },
+			select: ["settings"],
+		});
+		return res.json(user.settings);
+	},
+);
 
 router.patch(
 	"/",
-	route({ requestBody: "UserGuildSettingsSchema" }),
+	route({
+		requestBody: "UserGuildSettingsSchema",
+		responses: {
+			200: {},
+			400: {
+				body: "APIErrorResponse",
+			},
+			404: {
+				body: "APIErrorResponse",
+			},
+		},
+	}),
 	async (req: Request, res: Response) => {
 		const body = req.body as UserGuildSettingsSchema;
 
diff --git a/src/api/routes/users/@me/mfa/totp/disable.ts b/src/api/routes/users/@me/mfa/totp/disable.ts
index bade76c3..362152d7 100644
--- a/src/api/routes/users/@me/mfa/totp/disable.ts
+++ b/src/api/routes/users/@me/mfa/totp/disable.ts
@@ -31,7 +31,17 @@ const router = Router();
 
 router.post(
 	"/",
-	route({ requestBody: "TotpDisableSchema" }),
+	route({
+		requestBody: "TotpDisableSchema",
+		responses: {
+			200: {
+				body: "TokenOnlyResponse",
+			},
+			400: {
+				body: "APIErrorResponse",
+			},
+		},
+	}),
 	async (req: Request, res: Response) => {
 		const body = req.body as TotpDisableSchema;
 
diff --git a/src/api/routes/users/@me/mfa/totp/enable.ts b/src/api/routes/users/@me/mfa/totp/enable.ts
index 87bbaec9..19836e4d 100644
--- a/src/api/routes/users/@me/mfa/totp/enable.ts
+++ b/src/api/routes/users/@me/mfa/totp/enable.ts
@@ -32,7 +32,20 @@ const router = Router();
 
 router.post(
 	"/",
-	route({ requestBody: "TotpEnableSchema" }),
+	route({
+		requestBody: "TotpEnableSchema",
+		responses: {
+			200: {
+				body: "TokenWithBackupCodesResponse",
+			},
+			400: {
+				body: "APIErrorResponse",
+			},
+			404: {
+				body: "APIErrorResponse",
+			},
+		},
+	}),
 	async (req: Request, res: Response) => {
 		const body = req.body as TotpEnableSchema;
 
diff --git a/src/api/routes/users/@me/mfa/webauthn/credentials/#key_id/index.ts b/src/api/routes/users/@me/mfa/webauthn/credentials/#key_id/index.ts
index 04aca7e4..9cf42def 100644
--- a/src/api/routes/users/@me/mfa/webauthn/credentials/#key_id/index.ts
+++ b/src/api/routes/users/@me/mfa/webauthn/credentials/#key_id/index.ts
@@ -21,21 +21,31 @@ import { SecurityKey, User } from "@spacebar/util";
 import { Request, Response, Router } from "express";
 const router = Router();
 
-router.delete("/", route({}), async (req: Request, res: Response) => {
-	const { key_id } = req.params;
+router.delete(
+	"/",
+	route({
+		responses: {
+			204: {},
+		},
+	}),
+	async (req: Request, res: Response) => {
+		const { key_id } = req.params;
 
-	await SecurityKey.delete({
-		id: key_id,
-		user_id: req.user_id,
-	});
+		await SecurityKey.delete({
+			id: key_id,
+			user_id: req.user_id,
+		});
 
-	const keys = await SecurityKey.count({ where: { user_id: req.user_id } });
+		const keys = await SecurityKey.count({
+			where: { user_id: req.user_id },
+		});
 
-	// disable webauthn if there are no keys left
-	if (keys === 0)
-		await User.update({ id: req.user_id }, { webauthn_enabled: false });
+		// disable webauthn if there are no keys left
+		if (keys === 0)
+			await User.update({ id: req.user_id }, { webauthn_enabled: false });
 
-	res.sendStatus(204);
-});
+		res.sendStatus(204);
+	},
+);
 
 export default router;
diff --git a/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts b/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts
index 46bdfdd0..f383ffb7 100644
--- a/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts
+++ b/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts
@@ -73,7 +73,17 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 
 router.post(
 	"/",
-	route({ requestBody: "WebAuthnPostSchema" }),
+	route({
+		requestBody: "WebAuthnPostSchema",
+		responses: {
+			200: {
+				body: "WebAuthnCreateResponse",
+			},
+			400: {
+				body: "APIErrorResponse",
+			},
+		},
+	}),
 	async (req: Request, res: Response) => {
 		if (!WebAuthn.fido2) {
 			// TODO: I did this for typescript and I can't use !
diff --git a/src/util/schemas/responses/TokenResponse.ts b/src/util/schemas/responses/TokenResponse.ts
index c811632f..7e93055a 100644
--- a/src/util/schemas/responses/TokenResponse.ts
+++ b/src/util/schemas/responses/TokenResponse.ts
@@ -1,6 +1,15 @@
-import { UserSettings } from "../../entities";
+import { BackupCode, UserSettings } from "../../entities";
 
 export interface TokenResponse {
 	token: string;
 	settings: UserSettings;
 }
+
+export interface TokenOnlyResponse {
+	token: string;
+}
+
+export interface TokenWithBackupCodesResponse {
+	token: string;
+	backup_codes: BackupCode[];
+}
diff --git a/src/util/schemas/responses/UserResponse.ts b/src/util/schemas/responses/UserResponse.ts
index 21c30cd5..95f73649 100644
--- a/src/util/schemas/responses/UserResponse.ts
+++ b/src/util/schemas/responses/UserResponse.ts
@@ -1,5 +1,5 @@
 import { DmChannelDTO } from "../../dtos";
-import { Guild, PrivateUser, PublicUser, User } from "../../entities";
+import { BackupCode, Guild, PrivateUser, PublicUser } from "../../entities";
 
 export type PublicUserResponse = PublicUser;
 export type PrivateUserResponse = PrivateUser;
@@ -12,11 +12,4 @@ export type UserGuildsResponse = Guild[];
 
 export type UserChannelsResponse = DmChannelDTO[];
 
-export interface UserBackupCodesResponse {
-	expired: unknown;
-	user: User;
-	code: string;
-	consumed: boolean;
-	id: string;
-}
-[];
+export type UserBackupCodesResponse = BackupCode[];
diff --git a/src/util/schemas/responses/WebAuthnCreateResponse.ts b/src/util/schemas/responses/WebAuthnCreateResponse.ts
new file mode 100644
index 00000000..9aa9e206
--- /dev/null
+++ b/src/util/schemas/responses/WebAuthnCreateResponse.ts
@@ -0,0 +1,4 @@
+export interface WebAuthnCreateResponse {
+	name: string;
+	id: string;
+}
diff --git a/src/util/schemas/responses/index.ts b/src/util/schemas/responses/index.ts
index 1f0e2aed..e75ab382 100644
--- a/src/util/schemas/responses/index.ts
+++ b/src/util/schemas/responses/index.ts
@@ -44,4 +44,5 @@ export * from "./UserProfileResponse";
 export * from "./UserRelationshipsResponse";
 export * from "./UserRelationsResponse";
 export * from "./UserResponse";
+export * from "./WebAuthnCreateResponse";
 export * from "./WebhookCreateResponse";