2021-12-21 18:44:44 +00:00
|
|
|
import { SentryIgnoredError } from "../common/errors";
|
2021-11-03 20:08:57 +00:00
|
|
|
import { VERSION } from "../constants";
|
2021-05-02 12:43:51 +00:00
|
|
|
|
|
|
|
export interface PlexPinResponse {
|
|
|
|
// Only has the fields we care about
|
|
|
|
authToken?: string;
|
|
|
|
code: string;
|
|
|
|
id: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface PlexResource {
|
|
|
|
name: string;
|
|
|
|
provides: string;
|
|
|
|
clientIdentifier: string;
|
2021-05-04 20:51:52 +00:00
|
|
|
owned: boolean;
|
2021-05-02 12:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const DEFAULT_HEADERS = {
|
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-Plex-Product": "authentik",
|
|
|
|
"X-Plex-Version": VERSION,
|
|
|
|
"X-Plex-Device-Vendor": "BeryJu.org",
|
|
|
|
};
|
|
|
|
|
2021-05-02 14:47:20 +00:00
|
|
|
export function popupCenterScreen(url: string, title: string, w: number, h: number): Window | null {
|
2021-08-03 15:52:21 +00:00
|
|
|
const top = (screen.height - h) / 4,
|
|
|
|
left = (screen.width - w) / 2;
|
|
|
|
const popup = window.open(
|
|
|
|
url,
|
|
|
|
title,
|
|
|
|
`scrollbars=yes,width=${w},height=${h},top=${top},left=${left}`,
|
|
|
|
);
|
2021-05-02 14:47:20 +00:00
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2021-05-02 12:43:51 +00:00
|
|
|
export class PlexAPIClient {
|
|
|
|
token: string;
|
|
|
|
|
|
|
|
constructor(token: string) {
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
static async getPin(
|
|
|
|
clientIdentifier: string,
|
|
|
|
): Promise<{ authUrl: string; pin: PlexPinResponse }> {
|
|
|
|
const headers = {
|
|
|
|
...DEFAULT_HEADERS,
|
|
|
|
...{
|
|
|
|
"X-Plex-Client-Identifier": clientIdentifier,
|
|
|
|
},
|
|
|
|
};
|
2021-05-02 12:43:51 +00:00
|
|
|
const pinResponse = await fetch("https://plex.tv/api/v2/pins.json?strong=true", {
|
|
|
|
method: "POST",
|
2021-08-03 15:52:21 +00:00
|
|
|
headers: headers,
|
2021-05-02 12:43:51 +00:00
|
|
|
});
|
|
|
|
const pin: PlexPinResponse = await pinResponse.json();
|
|
|
|
return {
|
2021-08-03 15:52:21 +00:00
|
|
|
authUrl: `https://app.plex.tv/auth#!?clientID=${encodeURIComponent(
|
|
|
|
clientIdentifier,
|
|
|
|
)}&code=${pin.code}`,
|
|
|
|
pin: pin,
|
2021-05-02 12:43:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-02 14:47:20 +00:00
|
|
|
static async pinStatus(clientIdentifier: string, id: number): Promise<string | undefined> {
|
2021-08-03 15:52:21 +00:00
|
|
|
const headers = {
|
|
|
|
...DEFAULT_HEADERS,
|
|
|
|
...{
|
|
|
|
"X-Plex-Client-Identifier": clientIdentifier,
|
|
|
|
},
|
|
|
|
};
|
2021-05-02 12:43:51 +00:00
|
|
|
const pinResponse = await fetch(`https://plex.tv/api/v2/pins/${id}`, {
|
2021-08-03 15:52:21 +00:00
|
|
|
headers: headers,
|
2021-05-02 12:43:51 +00:00
|
|
|
});
|
2021-11-03 20:08:57 +00:00
|
|
|
if (pinResponse.status > 200) {
|
2021-12-21 18:44:44 +00:00
|
|
|
throw new SentryIgnoredError("Invalid response code")
|
2021-11-03 20:08:57 +00:00
|
|
|
}
|
2021-05-02 12:43:51 +00:00
|
|
|
const pin: PlexPinResponse = await pinResponse.json();
|
2021-11-03 20:08:57 +00:00
|
|
|
console.debug(`authentik/plex: polling Pin`);
|
|
|
|
return pin.authToken;
|
2021-05-02 12:43:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 14:47:20 +00:00
|
|
|
static async pinPoll(clientIdentifier: string, id: number): Promise<string> {
|
|
|
|
const executePoll = async (
|
|
|
|
resolve: (authToken: string) => void,
|
2021-08-03 15:52:21 +00:00
|
|
|
reject: (e: Error) => void,
|
2021-05-02 14:47:20 +00:00
|
|
|
) => {
|
|
|
|
try {
|
2021-05-03 19:00:16 +00:00
|
|
|
const response = await PlexAPIClient.pinStatus(clientIdentifier, id);
|
2021-05-02 14:47:20 +00:00
|
|
|
|
|
|
|
if (response) {
|
|
|
|
resolve(response);
|
|
|
|
} else {
|
|
|
|
setTimeout(executePoll, 500, resolve, reject);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Promise(executePoll);
|
|
|
|
}
|
|
|
|
|
2021-05-02 12:43:51 +00:00
|
|
|
async getServers(): Promise<PlexResource[]> {
|
2021-08-03 15:52:21 +00:00
|
|
|
const resourcesResponse = await fetch(
|
|
|
|
`https://plex.tv/api/v2/resources?X-Plex-Token=${this.token}&X-Plex-Client-Identifier=authentik`,
|
|
|
|
{
|
|
|
|
headers: DEFAULT_HEADERS,
|
|
|
|
},
|
|
|
|
);
|
2021-05-02 12:43:51 +00:00
|
|
|
const resources: PlexResource[] = await resourcesResponse.json();
|
2021-08-03 15:52:21 +00:00
|
|
|
return resources.filter((r) => {
|
2021-05-04 20:51:52 +00:00
|
|
|
return r.provides.toLowerCase().includes("server") && r.owned;
|
2021-05-02 12:43:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|