2014-10-20 05:04:45 +02:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Connect;
|
2014-09-14 17:10:51 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-10-13 22:14:53 +02:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
using MediaBrowser.Controller.Security;
|
2014-09-15 01:39:06 +02:00
|
|
|
|
using MediaBrowser.Model.Connect;
|
2014-10-13 22:14:53 +02:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-10-27 04:06:01 +01:00
|
|
|
|
using MediaBrowser.Model.Events;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-09-04 03:44:40 +02:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
2014-10-12 19:31:41 +02:00
|
|
|
|
using System.Linq;
|
2014-09-06 06:21:23 +02:00
|
|
|
|
using System.Net;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Connect
|
|
|
|
|
{
|
|
|
|
|
public class ConnectManager : IConnectManager
|
|
|
|
|
{
|
2014-10-13 22:14:53 +02:00
|
|
|
|
private readonly SemaphoreSlim _operationLock = new SemaphoreSlim(1, 1);
|
2014-10-12 20:12:20 +02:00
|
|
|
|
|
2014-08-25 05:54:45 +02:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly IJsonSerializer _json;
|
|
|
|
|
private readonly IEncryptionManager _encryption;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-09-14 17:10:51 +02:00
|
|
|
|
private readonly IUserManager _userManager;
|
2014-10-13 22:14:53 +02:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
private ConnectData _data = new ConnectData();
|
|
|
|
|
|
|
|
|
|
public string ConnectServerId
|
|
|
|
|
{
|
|
|
|
|
get { return _data.ServerId; }
|
|
|
|
|
}
|
|
|
|
|
public string ConnectAccessKey
|
|
|
|
|
{
|
|
|
|
|
get { return _data.AccessKey; }
|
|
|
|
|
}
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-09-10 02:28:59 +02:00
|
|
|
|
public string DiscoveredWanIpAddress { get; private set; }
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-09-10 02:28:59 +02:00
|
|
|
|
public string WanIpAddress
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var address = _config.Configuration.WanDdns;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(address))
|
|
|
|
|
{
|
|
|
|
|
address = DiscoveredWanIpAddress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return address;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
2014-08-25 05:54:45 +02:00
|
|
|
|
public string WanApiAddress
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var ip = WanIpAddress;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(ip))
|
|
|
|
|
{
|
|
|
|
|
if (!ip.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
|
!ip.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
ip = "http://" + ip;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-10 00:22:04 +02:00
|
|
|
|
return ip + ":" + _config.Configuration.PublicPort.ToString(CultureInfo.InvariantCulture);
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConnectManager(ILogger logger,
|
|
|
|
|
IApplicationPaths appPaths,
|
|
|
|
|
IJsonSerializer json,
|
|
|
|
|
IEncryptionManager encryption,
|
|
|
|
|
IHttpClient httpClient,
|
|
|
|
|
IServerApplicationHost appHost,
|
2014-10-13 22:14:53 +02:00
|
|
|
|
IServerConfigurationManager config, IUserManager userManager, IProviderManager providerManager)
|
2014-08-25 05:54:45 +02:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_json = json;
|
|
|
|
|
_encryption = encryption;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_appHost = appHost;
|
|
|
|
|
_config = config;
|
2014-09-14 17:10:51 +02:00
|
|
|
|
_userManager = userManager;
|
2014-10-13 22:14:53 +02:00
|
|
|
|
_providerManager = providerManager;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-10-27 04:06:01 +01:00
|
|
|
|
_userManager.UserConfigurationUpdated += _userManager_UserConfigurationUpdated;
|
|
|
|
|
|
2014-08-25 05:54:45 +02:00
|
|
|
|
LoadCachedData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void OnWanAddressResolved(string address)
|
|
|
|
|
{
|
2014-09-10 02:28:59 +02:00
|
|
|
|
DiscoveredWanIpAddress = address;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-09-13 16:10:30 +02:00
|
|
|
|
UpdateConnectInfo();
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void UpdateConnectInfo()
|
2014-10-12 20:12:20 +02:00
|
|
|
|
{
|
|
|
|
|
await _operationLock.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await UpdateConnectInfoInternal().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UpdateConnectInfoInternal()
|
2014-08-25 05:54:45 +02:00
|
|
|
|
{
|
|
|
|
|
var wanApiAddress = WanApiAddress;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(wanApiAddress))
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("Cannot update Media Browser Connect information without a WanApiAddress");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-10-23 06:26:01 +02:00
|
|
|
|
var localAddress = _appHost.GetSystemInfo().LocalAddress;
|
|
|
|
|
|
2014-08-25 05:54:45 +02:00
|
|
|
|
var hasExistingRecord = !string.IsNullOrWhiteSpace(ConnectServerId) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(ConnectAccessKey);
|
|
|
|
|
|
2014-09-04 03:44:40 +02:00
|
|
|
|
var createNewRegistration = !hasExistingRecord;
|
|
|
|
|
|
2014-08-25 05:54:45 +02:00
|
|
|
|
if (hasExistingRecord)
|
|
|
|
|
{
|
2014-09-04 03:44:40 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-10-23 06:26:01 +02:00
|
|
|
|
await UpdateServerRegistration(wanApiAddress, localAddress).ConfigureAwait(false);
|
2014-09-04 03:44:40 +02:00
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
2014-10-23 06:26:01 +02:00
|
|
|
|
if (!ex.StatusCode.HasValue ||
|
|
|
|
|
!new[] { HttpStatusCode.NotFound, HttpStatusCode.Unauthorized }.Contains(ex.StatusCode.Value))
|
2014-09-04 03:44:40 +02:00
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createNewRegistration = true;
|
|
|
|
|
}
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
2014-09-04 03:44:40 +02:00
|
|
|
|
|
|
|
|
|
if (createNewRegistration)
|
2014-08-25 05:54:45 +02:00
|
|
|
|
{
|
2014-10-23 06:26:01 +02:00
|
|
|
|
await CreateServerRegistration(wanApiAddress, localAddress).ConfigureAwait(false);
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
2014-10-12 19:31:41 +02:00
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RefreshAuthorizationsInternal(true, CancellationToken.None).ConfigureAwait(false);
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error registering with Connect", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
private async Task CreateServerRegistration(string wanApiAddress, string localAddress)
|
2014-08-25 05:54:45 +02:00
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(wanApiAddress))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("wanApiAddress");
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 05:54:45 +02:00
|
|
|
|
var url = "Servers";
|
|
|
|
|
url = GetConnectUrl(url);
|
2014-09-03 04:30:05 +02:00
|
|
|
|
|
|
|
|
|
var postData = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"name", _appHost.FriendlyName},
|
2014-09-09 03:15:31 +02:00
|
|
|
|
{"url", wanApiAddress},
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{"systemId", _appHost.SystemId}
|
2014-09-03 04:30:05 +02:00
|
|
|
|
};
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(localAddress))
|
|
|
|
|
{
|
|
|
|
|
postData["localAddress"] = localAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-02 21:44:06 +02:00
|
|
|
|
using (var stream = await _httpClient.Post(url, postData, CancellationToken.None).ConfigureAwait(false))
|
2014-08-25 05:54:45 +02:00
|
|
|
|
{
|
|
|
|
|
var data = _json.DeserializeFromStream<ServerRegistrationResponse>(stream);
|
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
_data.ServerId = data.Id;
|
|
|
|
|
_data.AccessKey = data.AccessKey;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
|
|
|
|
CacheData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
private async Task UpdateServerRegistration(string wanApiAddress, string localAddress)
|
2014-08-25 05:54:45 +02:00
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(wanApiAddress))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("wanApiAddress");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(ConnectServerId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ConnectServerId");
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-02 21:44:06 +02:00
|
|
|
|
var url = "Servers";
|
2014-08-25 05:54:45 +02:00
|
|
|
|
url = GetConnectUrl(url);
|
2014-09-02 21:44:06 +02:00
|
|
|
|
url += "?id=" + ConnectServerId;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
var postData = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"name", _appHost.FriendlyName},
|
|
|
|
|
{"url", wanApiAddress},
|
|
|
|
|
{"systemId", _appHost.SystemId}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(localAddress))
|
|
|
|
|
{
|
|
|
|
|
postData["localAddress"] = localAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 04:30:05 +02:00
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = CancellationToken.None
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
options.SetPostData(postData);
|
2014-09-03 04:30:05 +02:00
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
SetServerAccessToken(options);
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
|
|
|
|
// No need to examine the response
|
2014-09-03 04:30:05 +02:00
|
|
|
|
using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content)
|
2014-08-25 05:54:45 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
private readonly object _dataFileLock = new object();
|
2014-08-25 05:54:45 +02:00
|
|
|
|
private string CacheFilePath
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(_appPaths.DataPath, "connect.txt"); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CacheData()
|
|
|
|
|
{
|
|
|
|
|
var path = CacheFilePath;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
var json = _json.SerializeToString(_data);
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
|
|
|
|
var encrypted = _encryption.EncryptString(json);
|
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
lock (_dataFileLock)
|
|
|
|
|
{
|
|
|
|
|
File.WriteAllText(path, encrypted, Encoding.UTF8);
|
|
|
|
|
}
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error saving data", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadCachedData()
|
|
|
|
|
{
|
|
|
|
|
var path = CacheFilePath;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-10-13 22:14:53 +02:00
|
|
|
|
lock (_dataFileLock)
|
|
|
|
|
{
|
|
|
|
|
var encrypted = File.ReadAllText(path, Encoding.UTF8);
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
var json = _encryption.DecryptString(encrypted);
|
2014-08-25 05:54:45 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
_data = _json.DeserializeFromString<ConnectData>(json);
|
|
|
|
|
}
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
|
|
|
|
catch (IOException)
|
|
|
|
|
{
|
|
|
|
|
// File isn't there. no biggie
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error loading data", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
private User GetUser(string id)
|
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(id);
|
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("User not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 05:54:45 +02:00
|
|
|
|
private string GetConnectUrl(string handler)
|
|
|
|
|
{
|
2014-09-14 18:15:56 +02:00
|
|
|
|
return "https://connect.mediabrowser.tv/service/" + handler;
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
2014-09-15 01:39:06 +02:00
|
|
|
|
public async Task<UserLinkResult> LinkUser(string userId, string connectUsername)
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{
|
|
|
|
|
await _operationLock.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await LinkUserInternal(userId, connectUsername).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<UserLinkResult> LinkUserInternal(string userId, string connectUsername)
|
2014-09-14 17:10:51 +02:00
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("userId");
|
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
if (string.IsNullOrWhiteSpace(connectUsername))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("connectUsername");
|
|
|
|
|
}
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(ConnectServerId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ConnectServerId");
|
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
|
|
|
|
var connectUser = await GetConnectUser(new ConnectUserQuery
|
|
|
|
|
{
|
2014-11-04 13:41:12 +01:00
|
|
|
|
NameOrEmail = connectUsername
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
|
|
|
|
}, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
2014-09-14 19:25:46 +02:00
|
|
|
|
if (!connectUser.IsActive)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("The Media Browser account has been disabled.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
var user = GetUser(userId);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(user.ConnectUserId))
|
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RemoveConnect(user, connectUser.Id).ConfigureAwait(false);
|
2014-09-14 17:10:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var url = GetConnectUrl("ServerAuthorizations");
|
|
|
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = CancellationToken.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var accessToken = Guid.NewGuid().ToString("N");
|
|
|
|
|
|
|
|
|
|
var postData = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"serverId", ConnectServerId},
|
|
|
|
|
{"userId", connectUser.Id},
|
|
|
|
|
{"userType", "Linked"},
|
|
|
|
|
{"accessToken", accessToken}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.SetPostData(postData);
|
|
|
|
|
|
|
|
|
|
SetServerAccessToken(options);
|
|
|
|
|
|
2014-09-15 01:39:06 +02:00
|
|
|
|
var result = new UserLinkResult();
|
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
// No need to examine the response
|
|
|
|
|
using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content)
|
|
|
|
|
{
|
2014-09-14 19:12:47 +02:00
|
|
|
|
var response = _json.DeserializeFromStream<ServerUserAuthorizationResponse>(stream);
|
2014-09-15 01:39:06 +02:00
|
|
|
|
|
|
|
|
|
result.IsPending = string.Equals(response.AcceptStatus, "waiting", StringComparison.OrdinalIgnoreCase);
|
2014-09-14 17:10:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.ConnectAccessKey = accessToken;
|
|
|
|
|
user.ConnectUserName = connectUser.Name;
|
|
|
|
|
user.ConnectUserId = connectUser.Id;
|
2014-09-15 01:39:06 +02:00
|
|
|
|
user.ConnectLinkType = UserLinkType.LinkedUser;
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
|
|
|
|
await user.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
|
2014-09-15 01:39:06 +02:00
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
user.Configuration.SyncConnectImage = false;
|
|
|
|
|
user.Configuration.SyncConnectName = false;
|
2014-10-13 22:14:53 +02:00
|
|
|
|
_userManager.UpdateConfiguration(user, user.Configuration);
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
2014-09-15 01:39:06 +02:00
|
|
|
|
return result;
|
2014-09-14 17:10:51 +02:00
|
|
|
|
}
|
2014-10-14 06:22:17 +02:00
|
|
|
|
|
2014-10-31 05:57:24 +01:00
|
|
|
|
public async Task<UserLinkResult> InviteUser(ConnectAuthorizationRequest request)
|
2014-09-14 17:10:51 +02:00
|
|
|
|
{
|
2014-10-13 22:14:53 +02:00
|
|
|
|
await _operationLock.WaitAsync().ConfigureAwait(false);
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-10-31 05:57:24 +01:00
|
|
|
|
return await InviteUserInternal(request).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 05:57:24 +01:00
|
|
|
|
private async Task<UserLinkResult> InviteUserInternal(ConnectAuthorizationRequest request)
|
2014-09-14 17:10:51 +02:00
|
|
|
|
{
|
2014-10-31 05:57:24 +01:00
|
|
|
|
var connectUsername = request.ConnectUserName;
|
|
|
|
|
var sendingUserId = request.SendingUserId;
|
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
if (string.IsNullOrWhiteSpace(connectUsername))
|
2014-09-14 17:10:51 +02:00
|
|
|
|
{
|
2014-10-13 22:14:53 +02:00
|
|
|
|
throw new ArgumentNullException("connectUsername");
|
|
|
|
|
}
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(ConnectServerId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ConnectServerId");
|
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
string connectUserId = null;
|
|
|
|
|
var result = new UserLinkResult();
|
|
|
|
|
|
|
|
|
|
try
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{
|
2014-10-23 06:26:01 +02:00
|
|
|
|
var connectUser = await GetConnectUser(new ConnectUserQuery
|
|
|
|
|
{
|
2014-11-04 13:41:12 +01:00
|
|
|
|
NameOrEmail = connectUsername
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
}, CancellationToken.None).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
if (!connectUser.IsActive)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("The Media Browser account has been disabled.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connectUserId = connectUser.Id;
|
|
|
|
|
result.GuestDisplayName = connectUser.Name;
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{
|
2014-10-23 06:26:01 +02:00
|
|
|
|
if (!ex.StatusCode.HasValue ||
|
|
|
|
|
ex.StatusCode.Value != HttpStatusCode.NotFound ||
|
|
|
|
|
!Validator.EmailIsValid(connectUsername))
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sendingUser = GetUser(sendingUserId);
|
|
|
|
|
var requesterUserName = sendingUser.ConnectUserName;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(requesterUserName))
|
|
|
|
|
{
|
|
|
|
|
requesterUserName = sendingUser.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(connectUserId))
|
|
|
|
|
{
|
|
|
|
|
return await SendNewUserInvitation(requesterUserName, connectUsername).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
var url = GetConnectUrl("ServerAuthorizations");
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = CancellationToken.None
|
|
|
|
|
};
|
2014-09-14 19:12:47 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
var accessToken = Guid.NewGuid().ToString("N");
|
2014-10-16 05:26:39 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
var postData = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"serverId", ConnectServerId},
|
2014-10-23 06:26:01 +02:00
|
|
|
|
{"userId", connectUserId},
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{"userType", "Guest"},
|
|
|
|
|
{"accessToken", accessToken},
|
2014-10-16 05:26:39 +02:00
|
|
|
|
{"requesterUserName", requesterUserName}
|
2014-10-13 22:14:53 +02:00
|
|
|
|
};
|
2014-09-14 19:12:47 +02:00
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
options.SetPostData(postData);
|
|
|
|
|
|
|
|
|
|
SetServerAccessToken(options);
|
|
|
|
|
|
|
|
|
|
// No need to examine the response
|
|
|
|
|
using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content)
|
|
|
|
|
{
|
|
|
|
|
var response = _json.DeserializeFromStream<ServerUserAuthorizationResponse>(stream);
|
|
|
|
|
|
|
|
|
|
result.IsPending = string.Equals(response.AcceptStatus, "waiting", StringComparison.OrdinalIgnoreCase);
|
2014-10-31 05:57:24 +01:00
|
|
|
|
|
2014-11-03 04:38:43 +01:00
|
|
|
|
_data.PendingAuthorizations.Add(new ConnectAuthorizationInternal
|
2014-10-31 05:57:24 +01:00
|
|
|
|
{
|
|
|
|
|
ConnectUserId = response.UserId,
|
|
|
|
|
Id = response.Id,
|
|
|
|
|
ImageUrl = response.UserImageUrl,
|
|
|
|
|
UserName = response.UserName,
|
|
|
|
|
ExcludedLibraries = request.ExcludedLibraries,
|
|
|
|
|
ExcludedChannels = request.ExcludedChannels,
|
2014-11-03 04:38:43 +01:00
|
|
|
|
EnableLiveTv = request.EnableLiveTv,
|
|
|
|
|
AccessToken = accessToken
|
2014-10-31 05:57:24 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CacheData();
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-10-14 06:22:17 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
private async Task<UserLinkResult> SendNewUserInvitation(string fromName, string email)
|
|
|
|
|
{
|
|
|
|
|
var url = GetConnectUrl("users/invite");
|
|
|
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = CancellationToken.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var postData = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"email", email},
|
|
|
|
|
{"requesterUserName", fromName}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.SetPostData(postData);
|
|
|
|
|
|
|
|
|
|
// No need to examine the response
|
|
|
|
|
using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content)
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-10-27 22:45:50 +01:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
return new UserLinkResult
|
|
|
|
|
{
|
|
|
|
|
IsNewUserInvitation = true,
|
|
|
|
|
GuestDisplayName = email
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
public Task RemoveConnect(string userId)
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{
|
|
|
|
|
var user = GetUser(userId);
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
return RemoveConnect(user, user.ConnectUserId);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
private async Task RemoveConnect(User user, string connectUserId)
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(connectUserId))
|
|
|
|
|
{
|
|
|
|
|
await CancelAuthorizationByConnectUserId(connectUserId).ConfigureAwait(false);
|
2014-09-14 17:10:51 +02:00
|
|
|
|
}
|
2014-09-14 19:12:47 +02:00
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
user.ConnectAccessKey = null;
|
|
|
|
|
user.ConnectUserName = null;
|
|
|
|
|
user.ConnectUserId = null;
|
2014-10-14 06:22:17 +02:00
|
|
|
|
user.ConnectLinkType = null;
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
|
|
|
|
await user.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ConnectUser> GetConnectUser(ConnectUserQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var url = GetConnectUrl("user");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.Id))
|
|
|
|
|
{
|
|
|
|
|
url = url + "?id=" + WebUtility.UrlEncode(query.Id);
|
|
|
|
|
}
|
2014-11-04 13:41:12 +01:00
|
|
|
|
else if (!string.IsNullOrWhiteSpace(query.NameOrEmail))
|
|
|
|
|
{
|
|
|
|
|
url = url + "?nameOrEmail=" + WebUtility.UrlEncode(query.NameOrEmail);
|
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
else if (!string.IsNullOrWhiteSpace(query.Name))
|
|
|
|
|
{
|
2014-09-22 23:56:54 +02:00
|
|
|
|
url = url + "?name=" + WebUtility.UrlEncode(query.Name);
|
2014-09-14 17:10:51 +02:00
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrWhiteSpace(query.Email))
|
|
|
|
|
{
|
2014-09-22 23:56:54 +02:00
|
|
|
|
url = url + "?name=" + WebUtility.UrlEncode(query.Email);
|
2014-09-14 17:10:51 +02:00
|
|
|
|
}
|
2014-11-05 00:50:26 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Empty ConnectUserQuery supplied");
|
|
|
|
|
}
|
2014-09-14 17:10:51 +02:00
|
|
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SetServerAccessToken(options);
|
|
|
|
|
|
|
|
|
|
using (var stream = await _httpClient.Get(options).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
var response = _json.DeserializeFromStream<GetConnectUserResponse>(stream);
|
|
|
|
|
|
|
|
|
|
return new ConnectUser
|
|
|
|
|
{
|
|
|
|
|
Email = response.Email,
|
|
|
|
|
Id = response.Id,
|
2014-09-14 19:25:46 +02:00
|
|
|
|
Name = response.Name,
|
2014-10-14 06:22:17 +02:00
|
|
|
|
IsActive = response.IsActive,
|
|
|
|
|
ImageUrl = response.ImageUrl
|
2014-09-14 17:10:51 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetServerAccessToken(HttpRequestOptions options)
|
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(ConnectAccessKey))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ConnectAccessKey");
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-14 17:10:51 +02:00
|
|
|
|
options.RequestHeaders.Add("X-Connect-Token", ConnectAccessKey);
|
|
|
|
|
}
|
2014-10-12 19:31:41 +02:00
|
|
|
|
|
|
|
|
|
public async Task RefreshAuthorizations(CancellationToken cancellationToken)
|
2014-10-12 20:12:20 +02:00
|
|
|
|
{
|
|
|
|
|
await _operationLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RefreshAuthorizationsInternal(true, cancellationToken).ConfigureAwait(false);
|
2014-10-12 20:12:20 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
private async Task RefreshAuthorizationsInternal(bool refreshImages, CancellationToken cancellationToken)
|
2014-10-12 19:31:41 +02:00
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(ConnectServerId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ConnectServerId");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 19:31:41 +02:00
|
|
|
|
var url = GetConnectUrl("ServerAuthorizations");
|
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
url += "?serverId=" + ConnectServerId;
|
|
|
|
|
|
2014-10-12 19:31:41 +02:00
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SetServerAccessToken(options);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-10-13 22:14:53 +02:00
|
|
|
|
using (var stream = (await _httpClient.SendAsync(options, "GET").ConfigureAwait(false)).Content)
|
2014-10-12 19:31:41 +02:00
|
|
|
|
{
|
|
|
|
|
var list = _json.DeserializeFromStream<List<ServerUserAuthorizationResponse>>(stream);
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RefreshAuthorizations(list, refreshImages).ConfigureAwait(false);
|
2014-10-12 19:31:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error refreshing server authorizations.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
private readonly SemaphoreSlim _connectImageSemaphore = new SemaphoreSlim(5, 5);
|
|
|
|
|
private async Task RefreshAuthorizations(List<ServerUserAuthorizationResponse> list, bool refreshImages)
|
2014-10-12 19:31:41 +02:00
|
|
|
|
{
|
2014-10-12 19:46:25 +02:00
|
|
|
|
var users = _userManager.Users.ToList();
|
|
|
|
|
|
|
|
|
|
// Handle existing authorizations that were removed by the Connect server
|
|
|
|
|
// Handle existing authorizations whose status may have been updated
|
|
|
|
|
foreach (var user in users)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(user.ConnectUserId))
|
|
|
|
|
{
|
|
|
|
|
var connectEntry = list.FirstOrDefault(i => string.Equals(i.UserId, user.ConnectUserId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (connectEntry == null)
|
|
|
|
|
{
|
2014-10-23 06:26:01 +02:00
|
|
|
|
var deleteUser = user.ConnectLinkType.HasValue &&
|
|
|
|
|
user.ConnectLinkType.Value == UserLinkType.Guest;
|
|
|
|
|
|
2014-10-12 19:46:25 +02:00
|
|
|
|
user.ConnectUserId = null;
|
|
|
|
|
user.ConnectAccessKey = null;
|
|
|
|
|
user.ConnectUserName = null;
|
2014-10-14 06:22:17 +02:00
|
|
|
|
user.ConnectLinkType = null;
|
2014-10-12 19:46:25 +02:00
|
|
|
|
|
2014-10-12 20:12:20 +02:00
|
|
|
|
await _userManager.UpdateUser(user).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
2014-10-23 06:26:01 +02:00
|
|
|
|
if (deleteUser)
|
2014-10-12 19:52:43 +02:00
|
|
|
|
{
|
2014-10-13 22:14:53 +02:00
|
|
|
|
_logger.Debug("Deleting guest user {0}", user.Name);
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await _userManager.DeleteUser(user).ConfigureAwait(false);
|
2014-10-12 19:52:43 +02:00
|
|
|
|
}
|
2014-10-12 19:46:25 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var changed = !string.Equals(user.ConnectAccessKey, connectEntry.AccessToken, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
|
{
|
|
|
|
|
user.ConnectUserId = connectEntry.UserId;
|
|
|
|
|
user.ConnectAccessKey = connectEntry.AccessToken;
|
|
|
|
|
|
2014-10-12 19:52:43 +02:00
|
|
|
|
await _userManager.UpdateUser(user).ConfigureAwait(false);
|
2014-10-12 19:46:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 05:57:24 +01:00
|
|
|
|
var currentPendingList = _data.PendingAuthorizations.ToList();
|
2014-11-03 04:38:43 +01:00
|
|
|
|
var newPendingList = new List<ConnectAuthorizationInternal>();
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
2014-10-12 19:52:43 +02:00
|
|
|
|
foreach (var connectEntry in list)
|
|
|
|
|
{
|
2014-10-13 22:14:53 +02:00
|
|
|
|
if (string.Equals(connectEntry.UserType, "guest", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2014-10-31 05:57:24 +01:00
|
|
|
|
var currentPendingEntry = currentPendingList.FirstOrDefault(i => string.Equals(i.Id, connectEntry.Id, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
if (string.Equals(connectEntry.AcceptStatus, "accepted", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2014-10-16 05:26:39 +02:00
|
|
|
|
var user = _userManager.Users
|
|
|
|
|
.FirstOrDefault(i => string.Equals(i.ConnectUserId, connectEntry.UserId, StringComparison.OrdinalIgnoreCase));
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
// Add user
|
2014-10-14 06:22:17 +02:00
|
|
|
|
user = await _userManager.CreateUser(connectEntry.UserName).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
user.ConnectUserName = connectEntry.UserName;
|
|
|
|
|
user.ConnectUserId = connectEntry.UserId;
|
|
|
|
|
user.ConnectLinkType = UserLinkType.Guest;
|
|
|
|
|
user.ConnectAccessKey = connectEntry.AccessToken;
|
|
|
|
|
|
|
|
|
|
await _userManager.UpdateUser(user).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
user.Configuration.SyncConnectImage = true;
|
|
|
|
|
user.Configuration.SyncConnectName = true;
|
2014-10-15 02:05:09 +02:00
|
|
|
|
user.Configuration.IsHidden = true;
|
2014-10-16 05:26:39 +02:00
|
|
|
|
user.Configuration.EnableLiveTvManagement = false;
|
2014-10-31 05:57:24 +01:00
|
|
|
|
user.Configuration.EnableContentDeletion = false;
|
|
|
|
|
user.Configuration.EnableRemoteControlOfOtherUsers = false;
|
2014-10-16 05:26:39 +02:00
|
|
|
|
user.Configuration.IsAdministrator = false;
|
2014-10-14 06:22:17 +02:00
|
|
|
|
|
2014-10-31 05:57:24 +01:00
|
|
|
|
if (currentPendingEntry != null)
|
|
|
|
|
{
|
|
|
|
|
user.Configuration.EnableLiveTvAccess = currentPendingEntry.EnableLiveTv;
|
|
|
|
|
user.Configuration.BlockedMediaFolders = currentPendingEntry.ExcludedLibraries;
|
|
|
|
|
user.Configuration.BlockedChannels = currentPendingEntry.ExcludedChannels;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
_userManager.UpdateConfiguration(user, user.Configuration);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(connectEntry.AcceptStatus, "waiting", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2014-11-03 04:38:43 +01:00
|
|
|
|
currentPendingEntry = currentPendingEntry ?? new ConnectAuthorizationInternal();
|
2014-10-31 05:57:24 +01:00
|
|
|
|
|
|
|
|
|
currentPendingEntry.ConnectUserId = connectEntry.UserId;
|
|
|
|
|
currentPendingEntry.ImageUrl = connectEntry.UserImageUrl;
|
|
|
|
|
currentPendingEntry.UserName = connectEntry.UserName;
|
|
|
|
|
currentPendingEntry.Id = connectEntry.Id;
|
2014-11-03 04:38:43 +01:00
|
|
|
|
currentPendingEntry.AccessToken = connectEntry.AccessToken;
|
2014-10-31 05:57:24 +01:00
|
|
|
|
|
|
|
|
|
newPendingList.Add(currentPendingEntry);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 05:57:24 +01:00
|
|
|
|
_data.PendingAuthorizations = newPendingList;
|
2014-10-13 22:14:53 +02:00
|
|
|
|
CacheData();
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RefreshGuestNames(list, refreshImages).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
private async Task RefreshGuestNames(List<ServerUserAuthorizationResponse> list, bool refreshImages)
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{
|
|
|
|
|
var users = _userManager.Users
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i.ConnectUserId) &&
|
|
|
|
|
(i.Configuration.SyncConnectImage || i.Configuration.SyncConnectName))
|
2014-10-14 06:22:17 +02:00
|
|
|
|
.ToList();
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
|
|
|
|
foreach (var user in users)
|
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
var authorization = list.FirstOrDefault(i => string.Equals(i.UserId, user.ConnectUserId, StringComparison.Ordinal));
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
if (authorization == null)
|
2014-10-12 19:52:43 +02:00
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
_logger.Warn("Unable to find connect authorization record for user {0}", user.Name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
|
|
|
|
if (user.Configuration.SyncConnectName)
|
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
var changed = !string.Equals(authorization.UserName, user.Name, StringComparison.OrdinalIgnoreCase);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
|
|
|
|
if (changed)
|
2014-10-12 19:52:43 +02:00
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await user.Rename(authorization.UserName).ConfigureAwait(false);
|
2014-10-12 19:52:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
|
|
|
|
if (user.Configuration.SyncConnectImage)
|
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
var imageUrl = authorization.UserImageUrl;
|
2014-10-13 22:14:53 +02:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(imageUrl))
|
|
|
|
|
{
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
|
|
|
|
if (!user.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
2014-10-14 06:22:17 +02:00
|
|
|
|
else if (refreshImages)
|
2014-10-13 22:14:53 +02:00
|
|
|
|
{
|
|
|
|
|
using (var response = await _httpClient.SendAsync(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = imageUrl,
|
|
|
|
|
BufferContent = false
|
|
|
|
|
|
|
|
|
|
}, "HEAD").ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
var length = response.ContentLength;
|
|
|
|
|
|
|
|
|
|
if (length != new FileInfo(user.GetImageInfo(ImageType.Primary, 0).Path).Length)
|
|
|
|
|
{
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await _providerManager.SaveImage(user, imageUrl, _connectImageSemaphore, ImageType.Primary, null, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
await user.RefreshMetadata(new MetadataRefreshOptions
|
|
|
|
|
{
|
|
|
|
|
ForceSave = true,
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
}, CancellationToken.None).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<ConnectAuthorization>> GetPendingGuests()
|
|
|
|
|
{
|
2014-10-14 06:22:17 +02:00
|
|
|
|
var time = DateTime.UtcNow - _data.LastAuthorizationsRefresh;
|
|
|
|
|
|
|
|
|
|
if (time.TotalMinutes >= 5)
|
|
|
|
|
{
|
|
|
|
|
await _operationLock.WaitAsync(CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
_data.LastAuthorizationsRefresh = DateTime.UtcNow;
|
|
|
|
|
CacheData();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-03 04:38:43 +01:00
|
|
|
|
return _data.PendingAuthorizations.Select(i => new ConnectAuthorization
|
|
|
|
|
{
|
|
|
|
|
ConnectUserId = i.ConnectUserId,
|
|
|
|
|
EnableLiveTv = i.EnableLiveTv,
|
|
|
|
|
ExcludedChannels = i.ExcludedChannels,
|
|
|
|
|
ExcludedLibraries = i.ExcludedLibraries,
|
|
|
|
|
Id = i.Id,
|
|
|
|
|
ImageUrl = i.ImageUrl,
|
|
|
|
|
UserName = i.UserName
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task CancelAuthorization(string id)
|
|
|
|
|
{
|
|
|
|
|
await _operationLock.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await CancelAuthorizationInternal(id).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CancelAuthorizationInternal(string id)
|
|
|
|
|
{
|
|
|
|
|
var connectUserId = _data.PendingAuthorizations
|
|
|
|
|
.First(i => string.Equals(i.Id, id, StringComparison.Ordinal))
|
|
|
|
|
.ConnectUserId;
|
|
|
|
|
|
|
|
|
|
await CancelAuthorizationByConnectUserId(connectUserId).ConfigureAwait(false);
|
|
|
|
|
|
2014-10-14 06:22:17 +02:00
|
|
|
|
await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false);
|
2014-10-13 22:14:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CancelAuthorizationByConnectUserId(string connectUserId)
|
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(connectUserId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("connectUserId");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(ConnectServerId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ConnectServerId");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 22:14:53 +02:00
|
|
|
|
var url = GetConnectUrl("ServerAuthorizations");
|
|
|
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = CancellationToken.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var postData = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"serverId", ConnectServerId},
|
|
|
|
|
{"userId", connectUserId}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.SetPostData(postData);
|
|
|
|
|
|
|
|
|
|
SetServerAccessToken(options);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// No need to examine the response
|
|
|
|
|
using (var stream = (await _httpClient.SendAsync(options, "DELETE").ConfigureAwait(false)).Content)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
|
|
|
|
// If connect says the auth doesn't exist, we can handle that gracefully since this is a remove operation
|
|
|
|
|
|
|
|
|
|
if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Connect returned a 404 when removing a user auth link. Handling it.");
|
2014-10-12 19:52:43 +02:00
|
|
|
|
}
|
2014-10-12 19:31:41 +02:00
|
|
|
|
}
|
2014-10-17 06:52:41 +02:00
|
|
|
|
|
|
|
|
|
public async Task Authenticate(string username, string passwordMd5)
|
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("username");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(passwordMd5))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("passwordMd5");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-17 06:52:41 +02:00
|
|
|
|
var request = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = GetConnectUrl("user/authenticate")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
request.SetPostData(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"userName",username},
|
|
|
|
|
{"password",passwordMd5}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// No need to examine the response
|
|
|
|
|
using (var stream = (await _httpClient.SendAsync(request, "POST").ConfigureAwait(false)).Content)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-27 04:06:01 +01:00
|
|
|
|
|
|
|
|
|
async void _userManager_UserConfigurationUpdated(object sender, GenericEventArgs<User> e)
|
|
|
|
|
{
|
|
|
|
|
var user = e.Argument;
|
|
|
|
|
|
2014-11-08 02:15:30 +01:00
|
|
|
|
//await TryUploadUserPreferences(user, CancellationToken.None).ConfigureAwait(false);
|
2014-10-27 04:06:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task TryUploadUserPreferences(User user, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-11-05 00:50:26 +01:00
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("user");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 04:06:01 +01:00
|
|
|
|
if (string.IsNullOrEmpty(user.ConnectUserId))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(ConnectAccessKey))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var url = GetConnectUrl("user/preferences");
|
|
|
|
|
url += "?userId=" + user.ConnectUserId;
|
|
|
|
|
url += "&key=userpreferences";
|
|
|
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
2014-11-07 23:37:10 +01:00
|
|
|
|
CancellationToken = cancellationToken
|
2014-10-27 04:06:01 +01:00
|
|
|
|
};
|
|
|
|
|
|
2014-11-07 23:37:10 +01:00
|
|
|
|
var postData = new Dictionary<string, string>();
|
|
|
|
|
postData["data"] = _json.SerializeToString(ConnectUserPreferences.FromUserConfiguration(user.Configuration));
|
|
|
|
|
options.SetPostData(postData);
|
|
|
|
|
|
2014-10-27 04:06:01 +01:00
|
|
|
|
SetServerAccessToken(options);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// No need to examine the response
|
|
|
|
|
using (var stream = (await _httpClient.SendAsync(options, "POST").ConfigureAwait(false)).Content)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error uploading user preferences", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task DownloadUserPreferences(User user, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-10-27 22:45:50 +01:00
|
|
|
|
|
2014-10-27 04:06:01 +01:00
|
|
|
|
}
|
2014-11-03 04:38:43 +01:00
|
|
|
|
|
|
|
|
|
public async Task<User> GetLocalUser(string connectUserId)
|
|
|
|
|
{
|
|
|
|
|
var user = _userManager.Users
|
|
|
|
|
.FirstOrDefault(i => string.Equals(i.ConnectUserId, connectUserId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
await RefreshAuthorizations(CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _userManager.Users
|
|
|
|
|
.FirstOrDefault(i => string.Equals(i.ConnectUserId, connectUserId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsAuthorizationTokenValid(string token)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("token");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _userManager.Users.Any(u => string.Equals(token, u.ConnectAccessKey, StringComparison.OrdinalIgnoreCase)) ||
|
|
|
|
|
_data.PendingAuthorizations.Select(i => i.AccessToken).Contains(token, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
}
|
2014-08-25 05:54:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|