From b6645c8b58cfca7d74f44a60b9a88ed5e3686a97 Mon Sep 17 00:00:00 2001 From: Jamie Date: Sat, 18 Aug 2018 23:04:48 +0100 Subject: [PATCH 1/3] Made the OAuth a Popout to work with Org --- src/Ombi.Api.Plex/IPlexApi.cs | 2 +- src/Ombi.Api.Plex/PlexApi.cs | 8 ++--- .../Authentication/PlexOAuthManager.cs | 17 ++-------- .../ClientApp/app/login/login.component.ts | 31 ++++++++++++++----- src/Ombi/Controllers/TokenController.cs | 2 +- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/Ombi.Api.Plex/IPlexApi.cs b/src/Ombi.Api.Plex/IPlexApi.cs index 82db74278..343eaa2d7 100644 --- a/src/Ombi.Api.Plex/IPlexApi.cs +++ b/src/Ombi.Api.Plex/IPlexApi.cs @@ -24,7 +24,7 @@ namespace Ombi.Api.Plex Task GetAccount(string authToken); Task GetRecentlyAdded(string authToken, string uri, string sectionId); Task GetPin(int pinId); - Task GetOAuthUrl(int pinId, string code, string applicationUrl, bool wizard); + Task GetOAuthUrl(int pinId, string code, string applicationUrl); Task AddUser(string emailAddress, string serverId, string authToken, int[] libs); } } \ No newline at end of file diff --git a/src/Ombi.Api.Plex/PlexApi.cs b/src/Ombi.Api.Plex/PlexApi.cs index bed75b4d8..f0808622f 100644 --- a/src/Ombi.Api.Plex/PlexApi.cs +++ b/src/Ombi.Api.Plex/PlexApi.cs @@ -217,15 +217,11 @@ namespace Ombi.Api.Plex return await Api.Request(request); } - public async Task GetOAuthUrl(int pinId, string code, string applicationUrl, bool wizard) + public async Task GetOAuthUrl(int pinId, string code, string applicationUrl) { var request = new Request("auth#", "https://app.plex.tv", HttpMethod.Get); await AddHeaders(request); - var forwardUrl = wizard - ? new Request($"Wizard/OAuth/{pinId}", applicationUrl, HttpMethod.Get) - : new Request($"Login/OAuth/{pinId}", applicationUrl, HttpMethod.Get); - - request.AddQueryString("forwardUrl", forwardUrl.FullUri.ToString()); + request.AddQueryString("pinID", pinId.ToString()); request.AddQueryString("code", code); request.AddQueryString("context[device][product]", ApplicationName); diff --git a/src/Ombi.Core/Authentication/PlexOAuthManager.cs b/src/Ombi.Core/Authentication/PlexOAuthManager.cs index 803176d74..426037bb7 100644 --- a/src/Ombi.Core/Authentication/PlexOAuthManager.cs +++ b/src/Ombi.Core/Authentication/PlexOAuthManager.cs @@ -28,19 +28,6 @@ namespace Ombi.Core.Authentication return string.Empty; } - if (pin.authToken.IsNullOrEmpty()) - { - // Looks like we do not have a pin yet, we should retry a few times. - var retryCount = 0; - var retryMax = 5; - var retryWaitMs = 1000; - while (pin.authToken.IsNullOrEmpty() && retryCount < retryMax) - { - retryCount++; - await Task.Delay(retryWaitMs); - pin = await _api.GetPin(pinId); - } - } return pin.authToken; } @@ -52,14 +39,14 @@ namespace Ombi.Core.Authentication public async Task GetOAuthUrl(int pinId, string code, string websiteAddress = null) { var settings = await _customizationSettingsService.GetSettingsAsync(); - var url = await _api.GetOAuthUrl(pinId, code, settings.ApplicationUrl.IsNullOrEmpty() ? websiteAddress : settings.ApplicationUrl, false); + var url = await _api.GetOAuthUrl(pinId, code, settings.ApplicationUrl.IsNullOrEmpty() ? websiteAddress : settings.ApplicationUrl); return url; } public async Task GetWizardOAuthUrl(int pinId, string code, string websiteAddress) { - var url = await _api.GetOAuthUrl(pinId, code, websiteAddress, true); + var url = await _api.GetOAuthUrl(pinId, code, websiteAddress); return url; } } diff --git a/src/Ombi/ClientApp/app/login/login.component.ts b/src/Ombi/ClientApp/app/login/login.component.ts index 349c1600e..e4e530461 100644 --- a/src/Ombi/ClientApp/app/login/login.component.ts +++ b/src/Ombi/ClientApp/app/login/login.component.ts @@ -40,6 +40,7 @@ export class LoginComponent implements OnDestroy, OnInit { } private timer: any; + private pinTimer: any; private clientId: string; private errorBody: string; @@ -128,17 +129,33 @@ export class LoginComponent implements OnDestroy, OnInit { this.plexTv.GetPin(this.clientId, this.appName).subscribe((pin: any) => { this.authService.login({ usePlexOAuth: true, password: "", rememberMe: true, username: "", plexTvPin: pin }).subscribe(x => { - if (window.frameElement) { - // in frame - window.open(x.url, "_blank"); - } else { - // not in frame - window.location.href = x.url; - } + + window.open(x.url, "_blank"); + this.pinTimer = setInterval(() => { + this.getPinResult(x.pinId); + }, 10000); }); }); } + public getPinResult(pinId: number) { + this.authService.oAuth(pinId).subscribe(x => { + if(x.access_token) { + localStorage.setItem("id_token", x.access_token); + + if (this.authService.loggedIn()) { + this.router.navigate(["search"]); + return; + } + } + + }, err => { + this.notify.error(err.statusText); + + this.router.navigate(["login"]); + }); + } + public ngOnDestroy() { clearInterval(this.timer); } diff --git a/src/Ombi/Controllers/TokenController.cs b/src/Ombi/Controllers/TokenController.cs index aad367dbe..1314a741a 100644 --- a/src/Ombi/Controllers/TokenController.cs +++ b/src/Ombi/Controllers/TokenController.cs @@ -91,7 +91,7 @@ namespace Ombi.Controllers error = "Application URL has not been set" }); } - return new JsonResult(new { url = url.ToString() }); + return new JsonResult(new { url = url.ToString(), pinId = model.PlexTvPin.id }); } return new UnauthorizedResult(); From 70a3f2d02c586d1f30fb09f76f6b9e85174ca8f0 Mon Sep 17 00:00:00 2001 From: Jamie Date: Sat, 18 Aug 2018 23:15:12 +0100 Subject: [PATCH 2/3] Fixed linting !wip --- src/Ombi/ClientApp/app/login/login.component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Ombi/ClientApp/app/login/login.component.ts b/src/Ombi/ClientApp/app/login/login.component.ts index e4e530461..2c5a52404 100644 --- a/src/Ombi/ClientApp/app/login/login.component.ts +++ b/src/Ombi/ClientApp/app/login/login.component.ts @@ -30,6 +30,7 @@ export class LoginComponent implements OnDestroy, OnInit { public landingFlag: boolean; public baseUrl: string; public loginWithOmbi: boolean; + public pinTimer: any; public get appName(): string { if (this.customizationSettings.applicationName) { @@ -40,7 +41,6 @@ export class LoginComponent implements OnDestroy, OnInit { } private timer: any; - private pinTimer: any; private clientId: string; private errorBody: string; @@ -158,6 +158,7 @@ export class LoginComponent implements OnDestroy, OnInit { public ngOnDestroy() { clearInterval(this.timer); + clearInterval(this.pinTimer); } private cycleBackground() { From af9739d80596bbe1505bf268abf308e279dad269 Mon Sep 17 00:00:00 2001 From: TidusJar Date: Sun, 19 Aug 2018 20:28:26 +0100 Subject: [PATCH 3/3] Made the popup a bit better !wip --- src/Ombi/ClientApp/app/login/login.component.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Ombi/ClientApp/app/login/login.component.ts b/src/Ombi/ClientApp/app/login/login.component.ts index 2c5a52404..abf387fa1 100644 --- a/src/Ombi/ClientApp/app/login/login.component.ts +++ b/src/Ombi/ClientApp/app/login/login.component.ts @@ -116,6 +116,7 @@ export class LoginComponent implements OnDestroy, OnInit { localStorage.setItem("id_token", x.access_token); if (this.authService.loggedIn()) { + this.ngOnDestroy(); this.router.navigate(["search"]); } else { this.notify.error(this.errorBody); @@ -130,8 +131,17 @@ export class LoginComponent implements OnDestroy, OnInit { this.authService.login({ usePlexOAuth: true, password: "", rememberMe: true, username: "", plexTvPin: pin }).subscribe(x => { - window.open(x.url, "_blank"); + window.open(x.url, "_blank", `toolbar=0, + location=0, + status=0, + menubar=0, + scrollbars=1, + resizable=1, + width=500, + height=500`); + this.pinTimer = setInterval(() => { + this.notify.info("Authenticating", "Loading... Please Wait"); this.getPinResult(x.pinId); }, 10000); }); @@ -144,6 +154,7 @@ export class LoginComponent implements OnDestroy, OnInit { localStorage.setItem("id_token", x.access_token); if (this.authService.loggedIn()) { + this.ngOnDestroy(); this.router.navigate(["search"]); return; }