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