From 0e588bf3158c7a6473d62d520faeaa7371f835cd Mon Sep 17 00:00:00 2001 From: Hermanus Engelbrecht Date: Tue, 27 Aug 2024 15:27:53 +0200 Subject: [PATCH] refactor: resolving PR commets This refactor includes a change that adds a conditional arg to the ExternalApi get() method to override the base url. This method was pre-existing and already used in the calls and method. #943 --- server/api/embyconnect.ts | 50 +++++++++++++++++---------------------- server/api/externalapi.ts | 5 ++-- server/api/jellyfin.ts | 33 ++++++++++++-------------- 3 files changed, 40 insertions(+), 48 deletions(-) diff --git a/server/api/embyconnect.ts b/server/api/embyconnect.ts index b59f1cbc..455f4ce6 100644 --- a/server/api/embyconnect.ts +++ b/server/api/embyconnect.ts @@ -60,9 +60,9 @@ class EmbyConnectAPI extends ExternalAPI { } public async authenticateConnectUser(Email?: string, Password?: string) { - logger.debug( - `Attempting to authenticate via EmbyConnect with email: ${Email}` - ); + logger.debug(`Attempting to authenticate via EmbyConnect with email:`, { + Email, + }); const connectAuthResponse = await this.getConnectUserAccessToken( Email, @@ -76,8 +76,7 @@ class EmbyConnectAPI extends ExternalAPI { const matchingServer = this.findMatchingServer(linkedServers); - const embyServerApi = new EmbyServerApi(getHostname(), this.ClientIP); - const localUserExchangeResponse = await embyServerApi.localAuthExchange( + const localUserExchangeResponse = await this.localAuthExchange( matchingServer.AccessKey, connectAuthResponse.User.Id, this.DeviceId @@ -170,36 +169,31 @@ class EmbyConnectAPI extends ExternalAPI { return matchingServer; } -} -class EmbyServerApi extends ExternalAPI { - private ClientIP?: string; - constructor(embyHost: string, ClientIP?: string) { - super(embyHost, {}, {}); - this.ClientIP = ClientIP; - } - - async localAuthExchange( + private async localAuthExchange( accessKey: string, userId: string, deviceId?: string ): Promise { try { - return await this.get('/emby/Connect/Exchange', { - format: 'json', - ConnectUserId: userId, - 'X-Emby-Client': 'Jellyseerr', - 'X-Emby-Device-Id': deviceId ?? uniqueId(), - 'X-Emby-Client-Version': getAppVersion(), - 'X-Emby-Device-Name': 'Jellyseerr', - 'X-Emby-Token': accessKey, - }); + return this.get( + '/emby/Connect/Exchange', + { + format: 'json', + ConnectUserId: userId, + 'X-Emby-Client': 'Jellyseerr', + 'X-Emby-Device-Id': deviceId ?? uniqueId(), + 'X-Emby-Client-Version': getAppVersion(), + 'X-Emby-Device-Name': 'Jellyseerr', + 'X-Emby-Token': accessKey, + }, + undefined, + {}, + getHostname() + ); } catch (e) { - logger.error(`Failed to do local user auth exchange: ${e.message}`, { - label: 'EmbyConnect.EmbyServer API', - ip: this.ClientIP, - }); - throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); + logger.debug('Failed local user auth exchange'); + throw new ApiError(e.cause?.status, ApiErrorCode.InvalidCredentials); } } } diff --git a/server/api/externalapi.ts b/server/api/externalapi.ts index 75140bf0..45a3ee56 100644 --- a/server/api/externalapi.ts +++ b/server/api/externalapi.ts @@ -47,7 +47,8 @@ class ExternalAPI { endpoint: string, params?: Record, ttl?: number, - config?: RequestInit + config?: RequestInit, + overwriteBaseUrl?: string ): Promise { const cacheKey = this.serializeCacheKey(endpoint, { ...this.params, @@ -58,7 +59,7 @@ class ExternalAPI { return cachedItem; } - const url = this.formatUrl(endpoint, params); + const url = this.formatUrl(endpoint, params, overwriteBaseUrl); const response = await this.fetch(url, { ...config, headers: { diff --git a/server/api/jellyfin.ts b/server/api/jellyfin.ts index 7e847595..9f739fae 100644 --- a/server/api/jellyfin.ts +++ b/server/api/jellyfin.ts @@ -2,7 +2,9 @@ import EmbyConnectAPI from '@server/api/embyconnect'; import ExternalAPI from '@server/api/externalapi'; import { ApiErrorCode } from '@server/constants/error'; +import { MediaServerType } from '@server/constants/server'; import availabilitySync from '@server/lib/availabilitySync'; +import { getSettings } from '@server/lib/settings'; import logger from '@server/logger'; import { ApiError } from '@server/types/error'; import { getAppVersion } from '@server/utils/appVersion'; @@ -175,13 +177,20 @@ class JellyfinAPI extends ExternalAPI { } } - if (Username && EmailValidator.validate(Username)) { + const settings = getSettings(); + + if ( + settings.main.mediaServerType === MediaServerType.EMBY && + Username && + EmailValidator.validate(Username) + ) { try { - return await this.authenticateWithEmbyConnect( - ClientIP, - Username, - Password - ); + const connectApi = new EmbyConnectAPI({ + ClientIP: ClientIP, + DeviceId: this.deviceId, + }); + + return await connectApi.authenticateConnectUser(Username, Password); } catch (e) { logger.debug(`Emby Connect authentication failed: ${e}`); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidCredentials); @@ -191,18 +200,6 @@ class JellyfinAPI extends ExternalAPI { } } - private async authenticateWithEmbyConnect( - ClientIP: string | undefined, - Username: string | undefined, - Password: string | undefined - ): Promise { - const connectApi = new EmbyConnectAPI({ - ClientIP: ClientIP, - DeviceId: this.deviceId, - }); - return await connectApi.authenticateConnectUser(Username, Password); - } - public setUserId(userId: string): void { this.userId = userId; return;