Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix authorization header wrongly formatted #467

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/components/__tests__/jellyfinApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,10 @@ describe('getting security headers', () => {

// @ts-expect-error Since the method is private.
const result = JellyfinApi.getSecurityHeaders();
const correctAuth = `Jellyfin Client="Chromecast", Device="thisIsReceiverName", DeviceId="${btoa(
const correctAuth = `MediaBrowser Client="Chromecast", Token="thisIsAccessToken", Version="thisIsVersionNumber", DeviceId="${btoa(
'thisIsReceiverName'
)}", Version="thisIsVersionNumber", UserId="thisIsUserId"`;
)}", Device="thisIsReceiverName"`;

expect(result).toHaveProperty('X-MediaBrowser-Token');
expect(result).toHaveProperty('Authorization');
expect(result.Authorization).toMatch(correctAuth);
});
Expand All @@ -232,7 +231,8 @@ describe('getting security headers', () => {
// @ts-expect-error Since the method is private.
const result = JellyfinApi.getSecurityHeaders();
const correct = {
Authorization: `Jellyfin Client="Chromecast", Device="Google Cast", DeviceId="thisIsSenderId", Version="thisIsVersionNumber"`
Authorization:
'MediaBrowser Client="Chromecast", Token="thisIsAccessToken", Version="thisIsVersionNumber", DeviceId="thisIsSenderId", Device="Google%20Cast"'
};

expect(result).toMatchObject(correct);
Expand Down
43 changes: 26 additions & 17 deletions src/components/jellyfinApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ajax } from './fetchhelper';
import { Dictionary } from '~/types/global';

export abstract class JellyfinApi {
// userId that we are connecting as currently
Expand Down Expand Up @@ -34,9 +33,6 @@ export abstract class JellyfinApi {
this.accessToken = accessToken;
this.serverAddress = serverAddress;

// remove special characters from the receiver name
receiverName = receiverName.replace(/[^\w\s]/gi, '');

if (receiverName) {
this.deviceName = receiverName;
// deviceId just needs to be unique-ish
Expand All @@ -54,26 +50,39 @@ export abstract class JellyfinApi {
}

// create the necessary headers for authentication
private static getSecurityHeaders(): Dictionary<string> {
// TODO throw error if this fails
private static getSecurityHeaders(): { Authorization?: string } {
const parameters: Record<string, string> = {
Client: 'Chromecast'
};

let auth =
`Jellyfin Client="Chromecast", Device="${this.deviceName}", ` +
`DeviceId="${this.deviceId}", Version="${this.versionNumber}"`;
if (this.accessToken) {
parameters['Token'] = this.accessToken;
}

if (this.userId) {
auth += `, UserId="${this.userId}"`;
if (this.versionNumber) {
parameters['Version'] = this.versionNumber;
}

const headers: Dictionary<string> = {
Authorization: auth
};
if (this.deviceId) {
parameters['DeviceId'] = this.deviceId;
}

if (this.accessToken != undefined) {
headers['X-MediaBrowser-Token'] = this.accessToken;
if (this.deviceName) {
parameters['Device'] = this.deviceName;
}

return headers;
let header = 'MediaBrowser';

for (const [key, value] of Object.entries(parameters)) {
header += ` ${key}="${encodeURIComponent(value)}", `;
}

// Remove last comma
header = header.substring(0, header.length - 2);

return {
Authorization: header
};
}

// Create a basic url.
Expand Down