feat: fixing merge conflicts, adding user linking for emby connect

#943
This commit is contained in:
Hermanus Engelbrecht
2025-02-22 19:11:37 +01:00
parent 844bfe65a2
commit d1cec17e0b
7 changed files with 474 additions and 1460 deletions

View File

@@ -85,6 +85,7 @@ class EmbyConnectAPI extends ExternalAPI {
return {
User: {
Name: connectAuthResponse.User.Name,
Email: connectAuthResponse.User.Email,
ServerId: matchingServer.SystemId,
ServerName: matchingServer.Name,
Id: localUserExchangeResponse.LocalUserId,
@@ -122,7 +123,10 @@ class EmbyConnectAPI extends ExternalAPI {
label: 'EmbyConnect API',
ip: this.ClientIP,
});
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidCredentials);
throw new ApiError(
e.cause?.status ?? 401,
ApiErrorCode.InvalidCredentials
);
}
}

View File

@@ -112,7 +112,10 @@ class ExternalAPI {
ttl?: number,
config?: RequestInit
): Promise<T> {
const headers = { ...this.defaultHeaders, ...config?.headers };
const headers = new Headers({
...this.defaultHeaders,
...(config?.headers || {}),
});
const cacheKey = this.serializeCacheKey(endpoint, {
config: { ...this.params, ...params },
headers,
@@ -125,10 +128,6 @@ class ExternalAPI {
}
const url = this.formatUrl(endpoint, params);
const headers = new Headers({
...this.defaultHeaders,
...(config?.headers || {}),
});
const isFormUrlEncoded = headers
.get('Content-Type')

View File

@@ -12,6 +12,7 @@ import * as EmailValidator from 'email-validator';
export interface JellyfinUserResponse {
Name: string;
Email?: string;
ServerId: string;
ServerName: string;
Id: string;
@@ -141,6 +142,31 @@ class JellyfinAPI extends ExternalAPI {
);
};
if (
getSettings().main.mediaServerType === MediaServerType.EMBY &&
Username &&
EmailValidator.validate(Username)
) {
try {
const connectApi = new EmbyConnectAPI({
ClientIP: ClientIP,
DeviceId: this.deviceId,
});
return await connectApi.authenticateConnectUser(Username, Password);
} catch (e) {
// Possible local Emby user with email as username
logger.warn(
`Emby Connect authentication failed: ${e}, attempting local Emby server authentication`,
{
label: 'Jellyfin API',
error:
e.cause?.message ?? e.cause?.statusText ?? ApiErrorCode.Unknown,
ip: ClientIP,
}
);
}
}
try {
return await authenticate(true);
} catch (e) {
@@ -164,39 +190,18 @@ class JellyfinAPI extends ExternalAPI {
} catch (e) {
if (e.cause.status === 401) {
throw new ApiError(e.cause.status, ApiErrorCode.InvalidCredentials);
} else {
logger.error(
'Something went wrong while authenticating with the Jellyfin server',
{
label: 'Jellyfin API',
error: e.cause.message ?? e.cause.statusText,
ip: ClientIP,
}
);
throw new ApiError(e.cause.status, ApiErrorCode.Unknown);
}
}
const settings = getSettings();
if (
settings.main.mediaServerType === MediaServerType.EMBY &&
Username &&
EmailValidator.validate(Username)
) {
try {
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);
}
} else {
logger.error(
'Something went wrong while authenticating with the Jellyfin server',
{
label: 'Jellyfin API',
error: e.cause.message ?? e.cause.statusText,
ip: ClientIP,
}
);
throw new ApiError(e.cause.status, ApiErrorCode.Unknown);
}
}
public setUserId(userId: string): void {
@@ -233,9 +238,9 @@ class JellyfinAPI extends ExternalAPI {
public async getUsers(): Promise<JellyfinUserListResponse> {
try {
const userReponse = await this.get<JellyfinUserResponse[]>(`/Users`);
const userResponse = await this.get<JellyfinUserResponse[]>(`/Users`);
return { users: userReponse };
return { users: userResponse };
} catch (e) {
logger.error(
'Something went wrong while getting the account from the Jellyfin server',
@@ -248,10 +253,10 @@ class JellyfinAPI extends ExternalAPI {
public async getUser(): Promise<JellyfinUserResponse> {
try {
const userReponse = await this.get<JellyfinUserResponse>(
const userResponse = await this.get<JellyfinUserResponse>(
`/Users/${this.userId ?? 'Me'}`
);
return userReponse;
return userResponse;
} catch (e) {
logger.error(
'Something went wrong while getting the account from the Jellyfin server',