From 860b9d583e720ba5e3d003e1eaca81faf22e360f Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sat, 25 Mar 2023 18:01:56 -0400 Subject: oapi: finish users --- src/api/routes/applications/#id/bot/index.ts | 4 +-- src/api/routes/auth/register.ts | 2 +- src/api/routes/auth/reset.ts | 2 +- .../routes/users/@me/guilds/#guild_id/settings.ts | 36 +++++++++++++++++----- src/api/routes/users/@me/mfa/totp/disable.ts | 12 +++++++- src/api/routes/users/@me/mfa/totp/enable.ts | 15 ++++++++- .../@me/mfa/webauthn/credentials/#key_id/index.ts | 34 ++++++++++++-------- .../users/@me/mfa/webauthn/credentials/index.ts | 12 +++++++- src/util/schemas/responses/TokenResponse.ts | 11 ++++++- src/util/schemas/responses/UserResponse.ts | 11 ++----- .../schemas/responses/WebAuthnCreateResponse.ts | 4 +++ src/util/schemas/responses/index.ts | 1 + 12 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 src/util/schemas/responses/WebAuthnCreateResponse.ts (limited to 'src') 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"; -- cgit 1.4.1