2014-11-15 03:31:03 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2017-08-30 20:52:29 +02:00
|
|
|
using System.Linq;
|
2018-09-12 19:26:21 +02:00
|
|
|
using MediaBrowser.Controller.Library;
|
2019-01-13 20:20:41 +01:00
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using MediaBrowser.Controller.Security;
|
|
|
|
using MediaBrowser.Model.Services;
|
2014-07-02 06:57:18 +02:00
|
|
|
|
2016-11-04 02:18:51 +01:00
|
|
|
namespace Emby.Server.Implementations.HttpServer.Security
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
|
|
|
public class AuthorizationContext : IAuthorizationContext
|
|
|
|
{
|
2014-11-15 03:31:03 +01:00
|
|
|
private readonly IAuthenticationRepository _authRepo;
|
2018-09-12 19:26:21 +02:00
|
|
|
private readonly IUserManager _userManager;
|
2014-11-15 03:31:03 +01:00
|
|
|
|
2018-12-28 00:27:57 +01:00
|
|
|
public AuthorizationContext(IAuthenticationRepository authRepo, IUserManager userManager)
|
2014-11-15 03:31:03 +01:00
|
|
|
{
|
|
|
|
_authRepo = authRepo;
|
2018-09-12 19:26:21 +02:00
|
|
|
_userManager = userManager;
|
2014-11-15 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public AuthorizationInfo GetAuthorizationInfo(object requestContext)
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
2017-09-03 20:38:26 +02:00
|
|
|
return GetAuthorizationInfo((IRequest)requestContext);
|
2014-11-15 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:38:26 +02:00
|
|
|
public AuthorizationInfo GetAuthorizationInfo(IRequest requestContext)
|
2014-11-15 03:31:03 +01:00
|
|
|
{
|
|
|
|
object cached;
|
|
|
|
if (requestContext.Items.TryGetValue("AuthorizationInfo", out cached))
|
|
|
|
{
|
|
|
|
return (AuthorizationInfo)cached;
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:57:18 +02:00
|
|
|
return GetAuthorization(requestContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the authorization.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="httpReq">The HTTP req.</param>
|
|
|
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
2017-09-03 20:38:26 +02:00
|
|
|
private AuthorizationInfo GetAuthorization(IRequest httpReq)
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
|
|
|
var auth = GetAuthorizationDictionary(httpReq);
|
|
|
|
|
|
|
|
string deviceId = null;
|
|
|
|
string device = null;
|
|
|
|
string client = null;
|
|
|
|
string version = null;
|
2017-01-13 22:05:12 +01:00
|
|
|
string token = null;
|
2014-07-02 06:57:18 +02:00
|
|
|
|
|
|
|
if (auth != null)
|
|
|
|
{
|
|
|
|
auth.TryGetValue("DeviceId", out deviceId);
|
|
|
|
auth.TryGetValue("Device", out device);
|
|
|
|
auth.TryGetValue("Client", out client);
|
|
|
|
auth.TryGetValue("Version", out version);
|
2017-01-13 22:05:12 +01:00
|
|
|
auth.TryGetValue("Token", out token);
|
2014-07-02 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(token))
|
2017-01-13 22:05:12 +01:00
|
|
|
{
|
|
|
|
token = httpReq.Headers["X-Emby-Token"];
|
|
|
|
}
|
2014-07-04 04:22:57 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(token))
|
2015-10-05 00:04:56 +02:00
|
|
|
{
|
|
|
|
token = httpReq.Headers["X-MediaBrowser-Token"];
|
|
|
|
}
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(token))
|
2014-07-04 04:22:57 +02:00
|
|
|
{
|
|
|
|
token = httpReq.QueryString["api_key"];
|
|
|
|
}
|
|
|
|
|
2014-11-15 03:31:03 +01:00
|
|
|
var info = new AuthorizationInfo
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
|
|
|
Client = client,
|
|
|
|
Device = device,
|
|
|
|
DeviceId = deviceId,
|
2014-07-02 20:34:08 +02:00
|
|
|
Version = version,
|
2014-07-04 04:22:57 +02:00
|
|
|
Token = token
|
2014-07-02 06:57:18 +02:00
|
|
|
};
|
2014-11-15 03:31:03 +01:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(token))
|
|
|
|
{
|
|
|
|
var result = _authRepo.Get(new AuthenticationInfoQuery
|
|
|
|
{
|
|
|
|
AccessToken = token
|
|
|
|
});
|
|
|
|
|
2017-08-30 20:52:29 +02:00
|
|
|
var tokenInfo = result.Items.Length > 0 ? result.Items[0] : null;
|
2014-11-15 03:31:03 +01:00
|
|
|
|
|
|
|
if (tokenInfo != null)
|
|
|
|
{
|
2017-10-17 21:50:10 +02:00
|
|
|
var updateToken = false;
|
|
|
|
|
2015-02-12 04:54:31 +01:00
|
|
|
// TODO: Remove these checks for IsNullOrWhiteSpace
|
|
|
|
if (string.IsNullOrWhiteSpace(info.Client))
|
|
|
|
{
|
|
|
|
info.Client = tokenInfo.AppName;
|
|
|
|
}
|
2017-10-17 21:50:10 +02:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(info.DeviceId))
|
|
|
|
{
|
|
|
|
info.DeviceId = tokenInfo.DeviceId;
|
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
// Temporary. TODO - allow clients to specify that the token has been shared with a casting device
|
|
|
|
var allowTokenInfoUpdate = info.Client == null || info.Client.IndexOf("chromecast", StringComparison.OrdinalIgnoreCase) == -1;
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2015-02-12 04:54:31 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(info.Device))
|
|
|
|
{
|
|
|
|
info.Device = tokenInfo.DeviceName;
|
|
|
|
}
|
2019-01-08 00:27:46 +01:00
|
|
|
|
2017-10-17 21:50:10 +02:00
|
|
|
else if (!string.Equals(info.Device, tokenInfo.DeviceName, StringComparison.OrdinalIgnoreCase))
|
2015-02-12 04:54:31 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
if (allowTokenInfoUpdate)
|
|
|
|
{
|
|
|
|
updateToken = true;
|
|
|
|
tokenInfo.DeviceName = info.Device;
|
|
|
|
}
|
2015-02-12 04:54:31 +01:00
|
|
|
}
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2016-06-23 07:25:43 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(info.Version))
|
|
|
|
{
|
|
|
|
info.Version = tokenInfo.AppVersion;
|
|
|
|
}
|
2017-10-17 21:50:10 +02:00
|
|
|
else if (!string.Equals(info.Version, tokenInfo.AppVersion, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
if (allowTokenInfoUpdate)
|
|
|
|
{
|
|
|
|
updateToken = true;
|
|
|
|
tokenInfo.AppVersion = info.Version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((DateTime.UtcNow - tokenInfo.DateLastActivity).TotalMinutes > 3)
|
|
|
|
{
|
|
|
|
tokenInfo.DateLastActivity = DateTime.UtcNow;
|
2017-10-17 21:50:10 +02:00
|
|
|
updateToken = true;
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tokenInfo.UserId.Equals(Guid.Empty))
|
|
|
|
{
|
|
|
|
info.User = _userManager.GetUserById(tokenInfo.UserId);
|
|
|
|
|
|
|
|
if (info.User != null && !string.Equals(info.User.Name, tokenInfo.UserName, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
tokenInfo.UserName = info.User.Name;
|
|
|
|
updateToken = true;
|
|
|
|
}
|
2017-10-17 21:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (updateToken)
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
_authRepo.Update(tokenInfo);
|
2017-10-17 21:50:10 +02:00
|
|
|
}
|
2014-11-15 03:31:03 +01:00
|
|
|
}
|
|
|
|
httpReq.Items["OriginalAuthenticationInfo"] = tokenInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
httpReq.Items["AuthorizationInfo"] = info;
|
|
|
|
|
|
|
|
return info;
|
2014-07-02 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the auth.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="httpReq">The HTTP req.</param>
|
|
|
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
2017-09-03 20:38:26 +02:00
|
|
|
private Dictionary<string, string> GetAuthorizationDictionary(IRequest httpReq)
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
2015-07-03 13:51:45 +02:00
|
|
|
var auth = httpReq.Headers["X-Emby-Authorization"];
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(auth))
|
2015-07-03 13:51:45 +02:00
|
|
|
{
|
|
|
|
auth = httpReq.Headers["Authorization"];
|
|
|
|
}
|
2014-07-02 06:57:18 +02:00
|
|
|
|
|
|
|
return GetAuthorization(auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the authorization.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="authorizationHeader">The authorization header.</param>
|
|
|
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
2014-09-05 05:48:53 +02:00
|
|
|
private Dictionary<string, string> GetAuthorization(string authorizationHeader)
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
|
|
|
if (authorizationHeader == null) return null;
|
|
|
|
|
2014-09-05 05:48:53 +02:00
|
|
|
var parts = authorizationHeader.Split(new[] { ' ' }, 2);
|
2014-07-02 06:57:18 +02:00
|
|
|
|
|
|
|
// There should be at least to parts
|
2014-09-05 05:48:53 +02:00
|
|
|
if (parts.Length != 2) return null;
|
2014-07-02 06:57:18 +02:00
|
|
|
|
2017-08-30 20:52:29 +02:00
|
|
|
var acceptedNames = new[] { "MediaBrowser", "Emby" };
|
2017-01-13 22:05:12 +01:00
|
|
|
|
2014-07-02 06:57:18 +02:00
|
|
|
// It has to be a digest request
|
2017-01-13 22:05:12 +01:00
|
|
|
if (!acceptedNames.Contains(parts[0] ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove uptil the first space
|
2014-09-05 05:48:53 +02:00
|
|
|
authorizationHeader = parts[1];
|
2014-07-02 06:57:18 +02:00
|
|
|
parts = authorizationHeader.Split(',');
|
|
|
|
|
|
|
|
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
foreach (var item in parts)
|
|
|
|
{
|
|
|
|
var param = item.Trim().Split(new[] { '=' }, 2);
|
2014-09-05 05:48:53 +02:00
|
|
|
|
|
|
|
if (param.Length == 2)
|
|
|
|
{
|
2017-01-13 22:05:12 +01:00
|
|
|
var value = NormalizeValue(param[1].Trim(new[] { '"' }));
|
2015-12-25 22:21:18 +01:00
|
|
|
result.Add(param[0], value);
|
2014-09-05 05:48:53 +02:00
|
|
|
}
|
2014-07-02 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-12-25 22:21:18 +01:00
|
|
|
|
2019-01-06 21:50:43 +01:00
|
|
|
private static string NormalizeValue(string value)
|
2017-01-13 22:05:12 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(value))
|
2017-01-13 22:05:12 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2015-12-25 22:21:18 +01:00
|
|
|
|
2017-01-13 22:05:12 +01:00
|
|
|
return System.Net.WebUtility.HtmlEncode(value);
|
|
|
|
}
|
2014-07-02 06:57:18 +02:00
|
|
|
}
|
|
|
|
}
|