mirror of
https://github.com/Radarr/Radarr.git
synced 2024-09-11 20:12:41 +02:00
Update plex token cache when password is changed
Fixed: New plex.tv password not taking effect immediately Fixed: Prevent an incorrect plex.tv password from locking out account
This commit is contained in:
parent
84ae81efe3
commit
61de750e05
@ -0,0 +1,14 @@
|
||||
namespace NzbDrone.Core.Notifications.Plex
|
||||
{
|
||||
public class PlexAuthenticationException : PlexException
|
||||
{
|
||||
public PlexAuthenticationException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public PlexAuthenticationException(string message, params object[] args)
|
||||
: base(message, args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -42,7 +42,7 @@ public List<PlexSection> GetTvSections(PlexServerSettings settings)
|
||||
var response = client.Execute(request);
|
||||
|
||||
_logger.Trace("Sections response: {0}", response.Content);
|
||||
CheckForError(response);
|
||||
CheckForError(response, settings);
|
||||
|
||||
return Json.Deserialize<PlexMediaContainer>(response.Content)
|
||||
.Directories
|
||||
@ -58,7 +58,7 @@ public void Update(int sectionId, PlexServerSettings settings)
|
||||
var response = client.Execute(request);
|
||||
|
||||
_logger.Trace("Update response: {0}", response.Content);
|
||||
CheckForError(response);
|
||||
CheckForError(response, settings);
|
||||
}
|
||||
|
||||
public void UpdateSeries(int metadataId, PlexServerSettings settings)
|
||||
@ -69,7 +69,7 @@ public void UpdateSeries(int metadataId, PlexServerSettings settings)
|
||||
var response = client.Execute(request);
|
||||
|
||||
_logger.Trace("Update Series response: {0}", response.Content);
|
||||
CheckForError(response);
|
||||
CheckForError(response, settings);
|
||||
}
|
||||
|
||||
public string Version(PlexServerSettings settings)
|
||||
@ -79,7 +79,7 @@ public string Version(PlexServerSettings settings)
|
||||
var response = client.Execute(request);
|
||||
|
||||
_logger.Trace("Version response: {0}", response.Content);
|
||||
CheckForError(response);
|
||||
CheckForError(response, settings);
|
||||
|
||||
return Json.Deserialize<PlexIdentity>(response.Content).Version;
|
||||
}
|
||||
@ -91,7 +91,7 @@ public List<PlexPreference> Preferences(PlexServerSettings settings)
|
||||
var response = client.Execute(request);
|
||||
|
||||
_logger.Trace("Preferences response: {0}", response.Content);
|
||||
CheckForError(response);
|
||||
CheckForError(response, settings);
|
||||
|
||||
return Json.Deserialize<PlexPreferences>(response.Content).Preferences;
|
||||
}
|
||||
@ -105,7 +105,7 @@ public List<PlexPreference> Preferences(PlexServerSettings settings)
|
||||
var response = client.Execute(request);
|
||||
|
||||
_logger.Trace("Sections response: {0}", response.Content);
|
||||
CheckForError(response);
|
||||
CheckForError(response, settings);
|
||||
|
||||
var item = Json.Deserialize<PlexSectionResponse>(response.Content)
|
||||
.Items
|
||||
@ -119,20 +119,18 @@ public List<PlexPreference> Preferences(PlexServerSettings settings)
|
||||
return item.Id;
|
||||
}
|
||||
|
||||
private String Authenticate(string username, string password)
|
||||
private String Authenticate(PlexServerSettings settings)
|
||||
{
|
||||
var request = GetMyPlexRequest("users/sign_in.json", Method.POST);
|
||||
var client = GetMyPlexClient(username, password);
|
||||
var client = GetMyPlexClient(settings.Username, settings.Password);
|
||||
|
||||
var response = client.Execute(request);
|
||||
|
||||
_logger.Debug("Authentication Response: {0}", response.Content);
|
||||
CheckForError(response);
|
||||
CheckForError(response, settings);
|
||||
|
||||
var user = Json.Deserialize<PlexUser>(JObject.Parse(response.Content).SelectToken("user").ToString());
|
||||
|
||||
_authCache.Set(username, user.AuthenticationToken);
|
||||
|
||||
return user.AuthenticationToken;
|
||||
}
|
||||
|
||||
@ -170,26 +168,40 @@ private RestRequest GetPlexServerRequest(string resource, Method method, PlexSer
|
||||
var request = new RestRequest(resource, method);
|
||||
request.AddHeader("Accept", "application/json");
|
||||
|
||||
if (!settings.Username.IsNullOrWhiteSpace())
|
||||
if (settings.Username.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
request.AddParameter("X-Plex-Token", GetAuthenticationToken(settings.Username, settings.Password), ParameterType.HttpHeader);
|
||||
request.AddParameter("X-Plex-Token", GetAuthenticationToken(settings), ParameterType.HttpHeader);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
private string GetAuthenticationToken(string username, string password)
|
||||
private string GetAuthenticationToken(PlexServerSettings settings)
|
||||
{
|
||||
return _authCache.Get(username, () => Authenticate(username, password));
|
||||
var token = _authCache.Get(settings.Username + settings.Password, () => Authenticate(settings));
|
||||
|
||||
if (token.IsNullOrWhiteSpace())
|
||||
{
|
||||
throw new PlexAuthenticationException("Invalid Token - Update your username and password");
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
private void CheckForError(IRestResponse response)
|
||||
private void CheckForError(IRestResponse response, PlexServerSettings settings)
|
||||
{
|
||||
_logger.Trace("Checking for error");
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
throw new PlexException("Unauthorized");
|
||||
if (settings.Username.IsNullOrWhiteSpace())
|
||||
{
|
||||
throw new PlexAuthenticationException("Unauthorized - Username and password required");
|
||||
}
|
||||
|
||||
//Set the token to null in the cache so we don't keep trying with bad credentials
|
||||
_authCache.Set(settings.Username + settings.Password, null);
|
||||
throw new PlexAuthenticationException("Unauthorized - Username or password is incorrect");
|
||||
}
|
||||
|
||||
var error = Json.Deserialize<PlexError>(response.Content);
|
||||
|
@ -151,6 +151,11 @@ public ValidationFailure Test(PlexServerSettings settings)
|
||||
return new ValidationFailure("Host", "At least one TV library is required");
|
||||
}
|
||||
}
|
||||
catch(PlexAuthenticationException ex)
|
||||
{
|
||||
_logger.ErrorException("Unable to connect to Plex Server: " + ex.Message, ex);
|
||||
return new ValidationFailure("Username", "Incorrect username or password");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Unable to connect to Plex Server: " + ex.Message, ex);
|
||||
|
@ -706,6 +706,7 @@
|
||||
<Compile Include="Notifications\Plex\Models\PlexPreferences.cs" />
|
||||
<Compile Include="Notifications\Plex\Models\PlexSectionItem.cs" />
|
||||
<Compile Include="Notifications\Plex\Models\PlexSection.cs" />
|
||||
<Compile Include="Notifications\Plex\PlexAuthenticationException.cs" />
|
||||
<Compile Include="Notifications\Plex\PlexHomeTheater.cs" />
|
||||
<Compile Include="Notifications\Plex\PlexHomeTheaterSettings.cs" />
|
||||
<Compile Include="Notifications\Plex\PlexClientService.cs" />
|
||||
|
@ -2,9 +2,9 @@
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" aria-hidden="true" data-dismiss="modal">×</button>
|
||||
{{#if id}}
|
||||
<h3>Edit - {{implementation}}</h3>
|
||||
<h3>Edit - {{implementationName}}</h3>
|
||||
{{else}}
|
||||
<h3>Add - {{implementation}}</h3>
|
||||
<h3>Add - {{implementationName}}</h3>
|
||||
{{/if}}
|
||||
</div>
|
||||
<div class="modal-body indexer-modal">
|
||||
|
@ -2,9 +2,9 @@
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||
{{#if id}}
|
||||
<h3>Edit - {{implementation}}</h3>
|
||||
<h3>Edit - {{implementationName}}</h3>
|
||||
{{else}}
|
||||
<h3>Add - {{implementation}}</h3>
|
||||
<h3>Add - {{implementationName}}</h3>
|
||||
{{/if}}
|
||||
</div>
|
||||
<div class="modal-body notification-modal">
|
||||
|
Loading…
Reference in New Issue
Block a user