sources/plex: allow users to connect their plex account without login flow

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer
2021-11-03 21:08:57 +01:00
parent 08eff4cc5d
commit 5374352411
12 changed files with 139 additions and 35 deletions

View File

@ -1,4 +1,4 @@
import { VERSION } from "../../../constants";
import { VERSION } from "../constants";
export interface PlexPinResponse {
// Only has the fields we care about
@ -72,8 +72,12 @@ export class PlexAPIClient {
const pinResponse = await fetch(`https://plex.tv/api/v2/pins/${id}`, {
headers: headers,
});
if (pinResponse.status > 200) {
throw new Error("Invalid response code")
}
const pin: PlexPinResponse = await pinResponse.json();
return pin.authToken || "";
console.debug(`authentik/plex: polling Pin`);
return pin.authToken;
}
static async pinPoll(clientIdentifier: string, id: number): Promise<string> {

View File

@ -19,10 +19,10 @@ import {
import { SourcesApi } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { PlexAPIClient, popupCenterScreen } from "../../../api/Plex";
import { MessageLevel } from "../../../elements/messages/Message";
import { showMessage } from "../../../elements/messages/MessageContainer";
import { BaseStage } from "../../stages/base";
import { PlexAPIClient, popupCenterScreen } from "./API";
@customElement("ak-flow-sources-plex")
export class PlexLoginInit extends BaseStage<

View File

@ -931,6 +931,7 @@ msgid "Configure what data should be used as unique User Identifier. For most ca
msgstr "Configure what data should be used as unique User Identifier. For most cases, the default should be fine."
#: src/user/user-settings/sources/SourceSettingsOAuth.ts
#: src/user/user-settings/sources/SourceSettingsPlex.ts
msgid "Connect"
msgstr "Connect"

View File

@ -929,6 +929,7 @@ msgid "Configure what data should be used as unique User Identifier. For most ca
msgstr "Configure quelle donnée utiliser pour l'identifiant unique utilisateur. La valeur par défaut devrait être correcte dans la plupart des cas."
#: src/user/user-settings/sources/SourceSettingsOAuth.ts
#: src/user/user-settings/sources/SourceSettingsPlex.ts
msgid "Connect"
msgstr "Connecter"

View File

@ -925,6 +925,7 @@ msgid "Configure what data should be used as unique User Identifier. For most ca
msgstr ""
#: src/user/user-settings/sources/SourceSettingsOAuth.ts
#: src/user/user-settings/sources/SourceSettingsPlex.ts
msgid "Connect"
msgstr ""

View File

@ -14,10 +14,10 @@ import {
} from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { PlexAPIClient, PlexResource, popupCenterScreen } from "../../../api/Plex";
import "../../../elements/forms/FormGroup";
import "../../../elements/forms/HorizontalFormElement";
import { ModelForm } from "../../../elements/forms/ModelForm";
import { PlexAPIClient, PlexResource, popupCenterScreen } from "../../../flows/sources/plex/API";
import { first, randomString } from "../../../utils";
@customElement("ak-source-plex-form")

View File

@ -47,6 +47,7 @@ export class UserSourceSettingsPage extends LitElement {
return html`<ak-user-settings-source-plex
objectId=${source.objectUid}
title=${source.title}
.configureUrl=${source.configureUrl}
>
</ak-user-settings-source-plex>`;
default:

View File

@ -7,6 +7,8 @@ import { until } from "lit/directives/until";
import { SourcesApi } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { PlexAPIClient, popupCenterScreen } from "../../../api/Plex";
import { EVENT_REFRESH } from "../../../constants";
import { BaseUserSettings } from "../BaseUserSettings";
@customElement("ak-user-settings-source-plex")
@ -21,6 +23,26 @@ export class SourceSettingsPlex extends BaseUserSettings {
</div>`;
}
async doPlex(): Promise<void> {
const authInfo = await PlexAPIClient.getPin(this.configureUrl || "");
const authWindow = popupCenterScreen(authInfo.authUrl, "plex auth", 550, 700);
PlexAPIClient.pinPoll(this.configureUrl || "", authInfo.pin.id).then((token) => {
authWindow?.close();
new SourcesApi(DEFAULT_CONFIG).sourcesPlexRedeemTokenAuthenticatedCreate({
plexTokenRedeemRequest: {
plexToken: token,
},
slug: this.objectId,
});
});
this.dispatchEvent(
new CustomEvent(EVENT_REFRESH, {
bubbles: true,
composed: true,
}),
);
}
renderInner(): TemplateResult {
return html`${until(
new SourcesApi(DEFAULT_CONFIG)
@ -43,7 +65,10 @@ export class SourceSettingsPlex extends BaseUserSettings {
${t`Disconnect`}
</button>`;
}
return html`<p>${t`Not connected.`}</p>`;
return html`<p>${t`Not connected.`}</p>
<button @click=${this.doPlex} class="pf-c-button pf-m-primary">
${t`Connect`}
</button>`;
}),
)}`;
}