Added some more logging and resilience around the Plex OAuth login #4232

pull/4245/head v4.0.1432
tidusjar 3 years ago
parent f8c1114401
commit bd857c1e4f

@ -23,7 +23,7 @@ namespace Ombi.Api.Plex
Task<PlexFriends> GetUsers(string authToken);
Task<PlexAccount> GetAccount(string authToken);
Task<PlexMetadata> GetRecentlyAdded(string authToken, string uri, string sectionId);
Task<OAuthPin> GetPin(int pinId);
Task<OAuthContainer> GetPin(int pinId);
Task<Uri> GetOAuthUrl(string code, string applicationUrl);
Task<PlexAddWrapper> AddUser(string emailAddress, string serverId, string authToken, int[] libs);
}

@ -1,7 +1,14 @@
using System;
using System.Collections.Generic;
namespace Ombi.Api.Plex.Models.OAuth
{
public class OAuthContainer
{
public OAuthPin Result { get; set; }
public OAuthErrorsContainer Errors { get; set; }
}
public class OAuthPin
{
public int id { get; set; }
@ -24,4 +31,15 @@ namespace Ombi.Api.Plex.Models.OAuth
public string coordinates { get; set; }
}
public class OAuthErrorsContainer
{
public List<OAuthErrors> errors { get; set; }
}
public class OAuthErrors
{
public int code { get; set; }
public string message { get; set; }
}
}

@ -1,7 +1,7 @@
using System;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Ombi.Api.Plex.Models;
using Ombi.Api.Plex.Models.Friends;
using Ombi.Api.Plex.Models.OAuth;
@ -208,12 +208,28 @@ namespace Ombi.Api.Plex
return await Api.Request<PlexMetadata>(request);
}
public async Task<OAuthPin> GetPin(int pinId)
public async Task<OAuthContainer> GetPin(int pinId)
{
var request = new Request($"api/v2/pins/{pinId}", "https://plex.tv/", HttpMethod.Get);
await AddHeaders(request);
return await Api.Request<OAuthPin>(request);
var response = await Api.RequestContent(request);
if (response.Contains("errors"))
{
var errors = JsonConvert.DeserializeObject<OAuthErrorsContainer>(response, Ombi.Api.Api.Settings);
return new OAuthContainer
{
Errors = errors
};
}
var pinResult = JsonConvert.DeserializeObject<OAuthPin>(response, Ombi.Api.Api.Settings);
return new OAuthContainer
{
Result = pinResult
};
}
public async Task<Uri> GetOAuthUrl(string code, string applicationUrl)

@ -73,7 +73,7 @@ namespace Ombi.Api
}
// do something with the response
var receivedString = await httpResponseMessage.Content.ReadAsStringAsync();
var receivedString = await httpResponseMessage.Content.ReadAsStringAsync(cancellationToken);
LogDebugContent(receivedString);
if (request.ContentType == ContentType.Json)
{

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Ombi.Api.Plex;
using Ombi.Api.Plex.Models;
using Ombi.Api.Plex.Models.OAuth;
@ -11,24 +13,37 @@ namespace Ombi.Core.Authentication
{
public class PlexOAuthManager : IPlexOAuthManager
{
public PlexOAuthManager(IPlexApi api, ISettingsService<CustomizationSettings> settings)
public PlexOAuthManager(IPlexApi api, ISettingsService<CustomizationSettings> settings, ILogger<PlexOAuthManager> logger)
{
_api = api;
_customizationSettingsService = settings;
_logger = logger;
}
private readonly IPlexApi _api;
private readonly ISettingsService<CustomizationSettings> _customizationSettingsService;
private readonly ILogger _logger;
public async Task<string> GetAccessTokenFromPin(int pinId)
{
var pin = await _api.GetPin(pinId);
if (pin.expiresAt < DateTime.UtcNow)
if (pin.Errors != null)
{
foreach (var err in pin.Errors?.errors ?? new List<OAuthErrors>())
{
_logger.LogError($"Code: '{err.code}' : '{err.message}'");
}
return string.Empty;
}
if (pin.Result.expiresIn <= 0)
{
_logger.LogError("Pin has expired");
return string.Empty;
}
return pin.authToken;
return pin.Result.authToken;
}
public async Task<PlexAccount> GetAccount(string accessToken)

@ -20,6 +20,7 @@ export class PlexTvService {
"X-Plex-Device": "Ombi (Web)",
"X-Plex-Platform": "Web",
"Accept": "application/json",
'X-Plex-Model': 'Plex OAuth',
});
return this.http.post<IPlexPin>("https://plex.tv/api/v2/pins?strong=true", null, {headers});
}

Loading…
Cancel
Save