jellyfin/Emby.Server.Implementations/HttpServer/Security/AuthorizationContext.cs

283 lines
9.9 KiB
C#
Raw Normal View History

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