jellyfin-server/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs

280 lines
9.5 KiB
C#
Raw Normal View History

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;
using System.Net;
2021-05-21 03:56:59 +00:00
using System.Threading.Tasks;
2021-05-16 12:49:11 +00:00
using MediaBrowser.Common.Extensions;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
2021-05-21 03:56:59 +00:00
namespace Jellyfin.Server.Implementations.Security
{
public class AuthorizationContext : IAuthorizationContext
{
2021-05-21 03:56:59 +00:00
private readonly JellyfinDb _jellyfinDb;
2018-09-12 17:26:21 +00:00
private readonly IUserManager _userManager;
2014-11-15 02:31:03 +00:00
2021-05-21 03:56:59 +00:00
public AuthorizationContext(JellyfinDb jellyfinDb, IUserManager userManager)
2014-11-15 02:31:03 +00:00
{
2021-05-21 03:56:59 +00:00
_jellyfinDb = jellyfinDb;
2018-09-12 17:26:21 +00:00
_userManager = userManager;
2014-11-15 02:31:03 +00:00
}
2021-05-21 03:56:59 +00:00
public Task<AuthorizationInfo> GetAuthorizationInfo(HttpContext requestContext)
2014-11-15 02:31:03 +00:00
{
2021-05-21 03:56:59 +00:00
if (requestContext.Request.HttpContext.Items.TryGetValue("AuthorizationInfo", out var cached) && cached != null)
2014-11-15 02:31:03 +00:00
{
2021-05-21 03:56:59 +00:00
return Task.FromResult((AuthorizationInfo)cached);
2014-11-15 02:31:03 +00:00
}
return GetAuthorization(requestContext);
}
2021-05-21 03:56:59 +00:00
public async Task<AuthorizationInfo> GetAuthorizationInfo(HttpRequest requestContext)
{
var auth = GetAuthorizationDictionary(requestContext);
2021-05-21 03:56:59 +00:00
var authInfo = await GetAuthorizationInfoFromDictionary(auth, requestContext.Headers, requestContext.Query).ConfigureAwait(false);
return authInfo;
}
/// <summary>
/// Gets the authorization.
/// </summary>
/// <param name="httpReq">The HTTP req.</param>
/// <returns>Dictionary{System.StringSystem.String}.</returns>
2021-05-21 03:56:59 +00:00
private async Task<AuthorizationInfo> GetAuthorization(HttpContext httpReq)
{
var auth = GetAuthorizationDictionary(httpReq);
2021-05-21 03:56:59 +00:00
var authInfo = await GetAuthorizationInfoFromDictionary(auth, httpReq.Request.Headers, httpReq.Request.Query).ConfigureAwait(false);
2020-09-02 10:22:14 +00:00
httpReq.Request.HttpContext.Items["AuthorizationInfo"] = authInfo;
return authInfo;
}
2021-05-21 03:56:59 +00:00
private async Task<AuthorizationInfo> GetAuthorizationInfoFromDictionary(
IReadOnlyDictionary<string, string>? auth,
IHeaderDictionary headers,
IQueryCollection queryString)
{
2021-05-21 03:56:59 +00:00
string? deviceId = null;
string? deviceName = null;
string? client = null;
string? version = null;
string? token = null;
if (auth != null)
{
auth.TryGetValue("DeviceId", out deviceId);
2021-05-21 03:56:59 +00:00
auth.TryGetValue("Device", out deviceName);
auth.TryGetValue("Client", out client);
auth.TryGetValue("Version", out version);
2017-01-13 21:05:12 +00:00
auth.TryGetValue("Token", out token);
}
2021-05-21 03:56:59 +00:00
#pragma warning disable CA1508
// headers can return StringValues.Empty
if (string.IsNullOrEmpty(token))
{
token = headers["X-Emby-Token"];
2017-01-13 21:05:12 +00:00
}
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(token))
2015-10-04 22:04:56 +00:00
{
token = headers["X-MediaBrowser-Token"];
2015-10-04 22:04:56 +00:00
}
2018-09-12 17:26:21 +00: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,
2021-05-21 03:56:59 +00:00
Device = deviceName,
DeviceId = deviceId,
2014-07-02 18:34:08 +00:00
Version = version,
Token = token,
IsAuthenticated = false,
HasToken = false
};
2014-11-15 02:31:03 +00:00
2020-10-14 23:58:33 +00:00
if (string.IsNullOrWhiteSpace(token))
2014-11-15 02:31:03 +00:00
{
2020-10-14 23:58:33 +00:00
// Request doesn't contain a token.
return authInfo;
2020-10-14 23:58:33 +00:00
}
2021-05-21 03:56:59 +00:00
#pragma warning restore CA1508
2014-11-15 02:31:03 +00:00
authInfo.HasToken = true;
2021-05-21 03:56:59 +00:00
var device = await _jellyfinDb.Devices.FirstOrDefaultAsync(d => d.AccessToken == token).ConfigureAwait(false);
2014-11-15 02:31:03 +00:00
2021-05-21 03:56:59 +00:00
if (device != null)
{
authInfo.IsAuthenticated = true;
}
2021-05-21 03:56:59 +00:00
if (device != null)
2020-10-14 23:58:33 +00:00
{
var updateToken = false;
2020-10-14 23:58:33 +00:00
// TODO: Remove these checks for IsNullOrWhiteSpace
if (string.IsNullOrWhiteSpace(authInfo.Client))
{
2021-05-21 03:56:59 +00:00
authInfo.Client = device.AppName;
2020-10-14 23:58:33 +00:00
}
2020-10-14 23:58:33 +00:00
if (string.IsNullOrWhiteSpace(authInfo.DeviceId))
{
2021-05-21 03:56:59 +00:00
authInfo.DeviceId = device.DeviceId;
2020-10-14 23:58:33 +00:00
}
2020-10-14 23:58:33 +00:00
// Temporary. TODO - allow clients to specify that the token has been shared with a casting device
2021-05-21 03:56:59 +00:00
var allowTokenInfoUpdate = !authInfo.Client.Contains("chromecast", StringComparison.OrdinalIgnoreCase);
2020-10-14 23:58:33 +00:00
if (string.IsNullOrWhiteSpace(authInfo.Device))
{
2021-05-21 03:56:59 +00:00
authInfo.Device = device.DeviceName;
2020-10-14 23:58:33 +00:00
}
2021-05-21 03:56:59 +00:00
else if (!string.Equals(authInfo.Device, device.DeviceName, StringComparison.OrdinalIgnoreCase))
2020-10-14 23:58:33 +00:00
{
if (allowTokenInfoUpdate)
{
2020-10-14 23:58:33 +00:00
updateToken = true;
2021-05-21 03:56:59 +00:00
device.DeviceName = authInfo.Device;
2018-09-12 17:26:21 +00:00
}
2020-10-14 23:58:33 +00:00
}
2018-09-12 17:26:21 +00:00
2020-10-14 23:58:33 +00:00
if (string.IsNullOrWhiteSpace(authInfo.Version))
{
2021-05-21 03:56:59 +00:00
authInfo.Version = device.AppVersion;
2020-10-14 23:58:33 +00:00
}
2021-05-21 03:56:59 +00:00
else if (!string.Equals(authInfo.Version, device.AppVersion, StringComparison.OrdinalIgnoreCase))
2020-10-14 23:58:33 +00:00
{
if (allowTokenInfoUpdate)
2018-09-12 17:26:21 +00:00
{
updateToken = true;
2021-05-21 03:56:59 +00:00
device.AppVersion = authInfo.Version;
2018-09-12 17:26:21 +00:00
}
2020-10-14 23:58:33 +00:00
}
2018-09-12 17:26:21 +00:00
2021-05-21 03:56:59 +00:00
if ((DateTime.UtcNow - device.DateLastActivity).TotalMinutes > 3)
2020-10-14 23:58:33 +00:00
{
2021-05-21 03:56:59 +00:00
device.DateLastActivity = DateTime.UtcNow;
2020-10-14 23:58:33 +00:00
updateToken = true;
}
2018-09-12 17:26:21 +00:00
2021-05-21 03:56:59 +00:00
if (!device.UserId.Equals(Guid.Empty))
2020-10-14 23:58:33 +00:00
{
2021-05-21 03:56:59 +00:00
authInfo.User = _userManager.GetUserById(device.UserId);
authInfo.IsApiKey = false;
2020-10-28 14:40:11 +00:00
}
else
{
authInfo.IsApiKey = true;
2014-11-15 02:31:03 +00:00
}
2020-10-14 23:58:33 +00:00
if (updateToken)
{
2021-05-21 03:56:59 +00:00
_jellyfinDb.Devices.Update(device);
await _jellyfinDb.SaveChangesAsync().ConfigureAwait(false);
2014-11-15 02:31:03 +00:00
}
}
return authInfo;
}
/// <summary>
/// Gets the auth.
/// </summary>
/// <param name="httpReq">The HTTP req.</param>
/// <returns>Dictionary{System.StringSystem.String}.</returns>
2021-05-21 03:56:59 +00:00
private Dictionary<string, string>? GetAuthorizationDictionary(HttpContext httpReq)
{
2020-09-02 10:22:14 +00:00
var auth = httpReq.Request.Headers["X-Emby-Authorization"];
if (string.IsNullOrEmpty(auth))
{
2020-09-02 10:22:14 +00:00
auth = httpReq.Request.Headers[HeaderNames.Authorization];
}
return auth.Count > 0 ? GetAuthorization(auth[0]) : null;
}
/// <summary>
/// Gets the auth.
/// </summary>
/// <param name="httpReq">The HTTP req.</param>
/// <returns>Dictionary{System.StringSystem.String}.</returns>
2021-05-21 03:56:59 +00:00
private Dictionary<string, string>? GetAuthorizationDictionary(HttpRequest httpReq)
{
2020-06-17 12:52:15 +00:00
var auth = httpReq.Headers["X-Emby-Authorization"];
2015-07-03 11:51:45 +00:00
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(auth))
2015-07-03 11:51:45 +00:00
{
auth = httpReq.Headers[HeaderNames.Authorization];
2015-07-03 11:51:45 +00:00
}
return auth.Count > 0 ? GetAuthorization(auth[0]) : null;
}
/// <summary>
/// Gets the authorization.
/// </summary>
/// <param name="authorizationHeader">The authorization header.</param>
/// <returns>Dictionary{System.StringSystem.String}.</returns>
private Dictionary<string, string>? GetAuthorization(ReadOnlySpan<char> authorizationHeader)
{
2021-05-16 12:49:11 +00:00
var firstSpace = authorizationHeader.IndexOf(' ');
2021-05-16 12:49:11 +00:00
// There should be at least two parts
if (firstSpace == -1)
{
return null;
}
2021-05-16 12:49:11 +00:00
var name = authorizationHeader[..firstSpace];
2017-01-13 21:05:12 +00:00
2021-05-16 12:49:11 +00:00
if (!name.Equals("MediaBrowser", StringComparison.OrdinalIgnoreCase)
&& !name.Equals("Emby", StringComparison.OrdinalIgnoreCase))
{
return null;
}
2021-05-16 12:49:11 +00:00
authorizationHeader = authorizationHeader[(firstSpace + 1)..];
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2021-05-16 12:49:11 +00:00
foreach (var item in authorizationHeader.Split(','))
{
2021-05-16 12:49:11 +00:00
var trimmedItem = item.Trim();
var firstEqualsSign = trimmedItem.IndexOf('=');
2014-09-05 03:48:53 +00:00
2021-05-16 12:49:11 +00:00
if (firstEqualsSign > 0)
2014-09-05 03:48:53 +00:00
{
2021-05-16 12:49:11 +00:00
var key = trimmedItem[..firstEqualsSign].ToString();
var value = NormalizeValue(trimmedItem[(firstEqualsSign + 1)..].Trim('"').ToString());
result[key] = value;
2014-09-05 03:48:53 +00:00
}
}
return result;
}
private static string NormalizeValue(string value)
2017-01-13 21:05:12 +00:00
{
return string.IsNullOrEmpty(value) ? value : WebUtility.HtmlEncode(value);
2017-01-13 21:05:12 +00:00
}
}
}