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