Skip to content
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
1 change: 1 addition & 0 deletions cypress/config/settings.cypress.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"clientId": "6919275e-142a-48d8-be6b-93594cbd4626",
"sessionSecret": "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"vapidPrivate": "tmnslaO8ZWN6bNbSEv_rolPeBTlNxOwCCAHrM9oZz3M",
"vapidPublic": "BK_EpP8NDm9waor2zn6_S28o3ZYv4kCkJOfYpO3pt3W6jnPmxrgTLANUBNbbyaNatPnSQ12De9CeqSYQrqWzHTs",
"main": {
Expand Down
8 changes: 8 additions & 0 deletions seerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,18 @@ components:
example: A Label
PublicSettings:
type: object
required:
- initialized
- plexClientIdentifier
properties:
initialized:
type: boolean
example: false
plexClientIdentifier:
type: string
format: uuid
description: Instance Plex OAuth client identifier
example: 6919275e-142a-48d8-be6b-93594cbd4626
Comment thread
coderabbitai[bot] marked this conversation as resolved.
MovieResult:
type: object
required:
Expand Down
2 changes: 1 addition & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ app
server.use(
'/api',
session({
secret: settings.clientId,
secret: settings.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
Expand Down
1 change: 1 addition & 0 deletions server/interfaces/api/settingsInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface PublicSettingsResponse {
emailEnabled: boolean;
newPlexLogin: boolean;
youtubeUrl: string;
plexClientIdentifier: string;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

export interface CacheItem {
Expand Down
14 changes: 13 additions & 1 deletion server/lib/settings/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MediaServerType } from '@server/constants/server';
import { Permission } from '@server/lib/permissions';
import { runMigrations } from '@server/lib/settings/migrator';
import { randomUUID } from 'crypto';
import { randomBytes, randomUUID } from 'crypto';
import fs from 'fs/promises';
import { mergeWith } from 'lodash';
import path from 'path';
Expand Down Expand Up @@ -211,6 +211,7 @@ interface FullPublicSettings extends PublicSettings {
userEmailRequired: boolean;
newPlexLogin: boolean;
youtubeUrl: string;
plexClientIdentifier: string;
}

export interface NotificationAgentConfig {
Expand Down Expand Up @@ -360,6 +361,7 @@ export type JobId =

export interface AllSettings {
clientId: string;
sessionSecret?: string;
vapidPublic: string;
vapidPrivate: string;
main: MainSettings;
Expand Down Expand Up @@ -387,6 +389,7 @@ class Settings {
constructor(initialSettings?: AllSettings) {
this.data = {
clientId: randomUUID(),
sessionSecret: '',
vapidPrivate: '',
vapidPublic: '',
main: {
Expand Down Expand Up @@ -713,6 +716,7 @@ class Settings {
this.data.notifications.agents.email.options.userEmailRequired,
newPlexLogin: this.data.main.newPlexLogin,
youtubeUrl: this.data.main.youtubeUrl,
plexClientIdentifier: this.data.clientId,
};
}

Expand Down Expand Up @@ -752,6 +756,10 @@ class Settings {
return this.data.clientId;
}

get sessionSecret(): string {
return this.data.sessionSecret!;
Comment thread
0xSysR3ll marked this conversation as resolved.
}

get vapidPublic(): string {
return this.data.vapidPublic;
}
Expand Down Expand Up @@ -821,6 +829,10 @@ class Settings {
this.data.clientId = randomUUID();
change = true;
}
if (!this.data.sessionSecret) {
this.data.sessionSecret = randomBytes(32).toString('hex');
change = true;
}
if (!this.data.vapidPublic || !this.data.vapidPrivate) {
const vapidKeys = webpush.generateVAPIDKeys();
this.data.vapidPrivate = vapidKeys.privateKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ const UserLinkedAccountsSettings = () => {
const linkPlexAccount = async () => {
setError(null);
try {
const authToken = await plexOAuth.login();
const authToken = await plexOAuth.login(
settings.currentSettings.plexClientIdentifier
);
Comment thread
0xSysR3ll marked this conversation as resolved.
await axios.post(
`/api/v1/user/${user?.id}/settings/linked-accounts/plex`,
{
Expand Down
1 change: 1 addition & 0 deletions src/context/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const defaultSettings = {
emailEnabled: false,
newPlexLogin: true,
youtubeUrl: '',
plexClientIdentifier: '',
};

export const SettingsContext = React.createContext<SettingsContextProps>({
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/usePlexLogin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import useSettings from '@app/hooks/useSettings';
import PlexOAuth from '@app/utils/plex';
import { useState } from 'react';

Expand All @@ -11,11 +12,14 @@ function usePlexLogin({
onError?: (err: string) => void;
}) {
const [loading, setLoading] = useState(false);
const { currentSettings } = useSettings();

const getPlexLogin = async () => {
setLoading(true);
try {
const authToken = await plexOAuth.login();
const authToken = await plexOAuth.login(
currentSettings.plexClientIdentifier
);
Comment thread
0xSysR3ll marked this conversation as resolved.
setLoading(false);
onAuthToken(authToken);
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ CoreApp.getInitialProps = async (initialProps) => {
emailEnabled: false,
newPlexLogin: true,
youtubeUrl: '',
plexClientIdentifier: '',
};

if (ctx.res) {
Expand Down
55 changes: 25 additions & 30 deletions src/utils/plex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ export interface PlexPin {
code: string;
}

const uuidv4 = (): string => {
return ((1e7).toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(
/[018]/g,
function (c) {
return (
parseInt(c) ^
(window.crypto.getRandomValues(new Uint8Array(1))[0] &
(15 >> (parseInt(c) / 4)))
).toString(16);
}
);
};

class PlexOAuth {
private plexHeaders?: PlexHeaders;

Expand All @@ -41,26 +28,25 @@ class PlexOAuth {

private authToken?: string;

public initializeHeaders(): void {
if (!window) {
public initializeHeaders(plexClientIdentifier: string): void {
if (typeof window === 'undefined') {
throw new Error(
'Window is not defined. Are you calling this in the browser?'
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

let clientId = localStorage.getItem('plex-client-id');
if (!clientId) {
const uuid = uuidv4();
localStorage.setItem('plex-client-id', uuid);
clientId = uuid;
if (!plexClientIdentifier) {
throw new Error(
'Plex client identifier missing. Reload the page and try again.'
);
}

const browser = Bowser.getParser(window.navigator.userAgent);
this.plexHeaders = {
Accept: 'application/json',
'X-Plex-Product': 'Seerr',
'X-Plex-Version': 'Plex OAuth',
'X-Plex-Client-Identifier': clientId,
'X-Plex-Client-Identifier': plexClientIdentifier,
'X-Plex-Model': 'Plex OAuth',
'X-Plex-Platform': browser.getBrowserName(),
'X-Plex-Platform-Version': browser.getBrowserVersion() || 'Unknown',
Expand Down Expand Up @@ -93,9 +79,14 @@ class PlexOAuth {
this.openPopup({ title: 'Plex Auth', w: 600, h: 700 });
}

public async login(): Promise<string> {
this.initializeHeaders();
await this.getPin();
public async login(plexClientIdentifier: string): Promise<string> {
try {
this.initializeHeaders(plexClientIdentifier);
await this.getPin();
} catch (e) {
this.closePopup();
throw e;
}

if (!this.plexHeaders || !this.pin) {
throw new Error('Unable to call login if class is not initialized.');
Expand All @@ -117,12 +108,16 @@ class PlexOAuth {
code: this.pin.code,
};

if (this.popup) {
this.popup.location.href = `https://app.plex.tv/auth/#!?${this.encodeData(
params
)}`;
if (!this.popup || this.popup.closed) {
throw new Error(
'Unable to open the Plex login window. Please allow popups for this site and try again.'
);
}

this.popup.location.href = `https://app.plex.tv/auth/#!?${this.encodeData(
params
)}`;

return this.pinPoll();
}

Expand All @@ -145,7 +140,7 @@ class PlexOAuth {
this.authToken = response.data.authToken as string;
this.closePopup();
resolve(this.authToken);
} else if (!response.data?.authToken && !this.popup?.closed) {
} else if (this.popup && !this.popup.closed) {
setTimeout(executePoll, 1000, resolve, reject);
} else {
reject(new Error('Popup closed without completing login'));
Expand Down Expand Up @@ -173,7 +168,7 @@ class PlexOAuth {
w: number;
h: number;
}): Window | void {
if (!window) {
if (typeof window === 'undefined') {
throw new Error(
'Window is undefined. Are you running this in the browser?'
);
Expand Down
Loading