From 98959dc330110f04486c09673acc12e9be1bff1a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 8 Apr 2025 09:16:55 -0400 Subject: [PATCH] refactor: add OAuthClient.findByClientId --- server/models/oauth/OAuthClient.ts | 16 ++++++++++++++++ server/routes/oauth/index.ts | 5 +---- server/utils/oauth/OAuthInterface.ts | 6 +----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/server/models/oauth/OAuthClient.ts b/server/models/oauth/OAuthClient.ts index 608f4f31d5..048ca08e52 100644 --- a/server/models/oauth/OAuthClient.ts +++ b/server/models/oauth/OAuthClient.ts @@ -125,6 +125,22 @@ class OAuthClient extends ParanoidModel< model.clientSecret = OAuthClient.generateNewClientSecret(); } + // static methods + + /** + * Find an OAuthClient by it's public `clientId` + * + * @param clientId The public clientId of the OAuthClient + * @returns The OAuthClient or null if not found + */ + public static async findByClientId(clientId: string) { + return this.findOne({ + where: { + clientId, + }, + }); + } + private static generateNewClientId(): string { return rs.generate({ length: 20, diff --git a/server/routes/oauth/index.ts b/server/routes/oauth/index.ts index 92ec7db89a..d2a413efd6 100644 --- a/server/routes/oauth/index.ts +++ b/server/routes/oauth/index.ts @@ -29,10 +29,7 @@ router.post( throw ValidationError("Missing client_id"); } - const client = await OAuthClient.findOne({ - where: { clientId }, - rejectOnEmpty: true, - }); + const client = await OAuthClient.findByClientId(clientId); authorize(user, "read", client); // Note: These objects are mutated by the OAuth2Server library diff --git a/server/utils/oauth/OAuthInterface.ts b/server/utils/oauth/OAuthInterface.ts index 9e5a19abd0..b0211b3d4f 100644 --- a/server/utils/oauth/OAuthInterface.ts +++ b/server/utils/oauth/OAuthInterface.ts @@ -121,11 +121,7 @@ export const OAuthInterface: RefreshTokenModel & }, async getClient(clientId: string, clientSecret?: string) { - const client = await OAuthClient.findOne({ - where: { - clientId, - }, - }); + const client = await OAuthClient.findByClientId(clientId); if (!client) { return false; }