2019-11-01 18:38:54 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2014-11-15 03:31:03 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-09-02 08:19:29 +02:00
|
|
|
using System.Net;
|
2021-06-19 18:02:33 +02:00
|
|
|
using Jellyfin.Extensions;
|
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;
|
2020-06-15 21:21:18 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-03-05 19:20:28 +01:00
|
|
|
using Microsoft.Net.Http.Headers;
|
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
|
|
|
}
|
|
|
|
|
2020-09-02 12:22:14 +02:00
|
|
|
public AuthorizationInfo GetAuthorizationInfo(HttpContext requestContext)
|
2014-11-15 03:31:03 +01:00
|
|
|
{
|
2020-09-02 12:22:14 +02:00
|
|
|
if (requestContext.Request.HttpContext.Items.TryGetValue("AuthorizationInfo", out var cached))
|
2014-11-15 03:31:03 +01:00
|
|
|
{
|
2021-05-20 21:28:18 +02:00
|
|
|
return (AuthorizationInfo)cached!; // Cache should never contain null
|
2014-11-15 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
2014-07-02 06:57:18 +02:00
|
|
|
return GetAuthorization(requestContext);
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:21:18 +02:00
|
|
|
public AuthorizationInfo GetAuthorizationInfo(HttpRequest requestContext)
|
|
|
|
{
|
|
|
|
var auth = GetAuthorizationDictionary(requestContext);
|
2020-11-08 16:10:33 +01:00
|
|
|
var authInfo = GetAuthorizationInfoFromDictionary(auth, requestContext.Headers, requestContext.Query);
|
2020-06-15 21:21:18 +02:00
|
|
|
return authInfo;
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:57:18 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the authorization.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="httpReq">The HTTP req.</param>
|
|
|
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
2020-09-02 12:22:14 +02:00
|
|
|
private AuthorizationInfo GetAuthorization(HttpContext httpReq)
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
|
|
|
var auth = GetAuthorizationDictionary(httpReq);
|
2020-11-08 16:10:33 +01:00
|
|
|
var authInfo = GetAuthorizationInfoFromDictionary(auth, httpReq.Request.Headers, httpReq.Request.Query);
|
2014-07-02 06:57:18 +02:00
|
|
|
|
2020-09-02 12:22:14 +02:00
|
|
|
httpReq.Request.HttpContext.Items["AuthorizationInfo"] = authInfo;
|
2020-06-15 21:21:18 +02:00
|
|
|
return authInfo;
|
|
|
|
}
|
|
|
|
|
2020-11-08 16:10:33 +01:00
|
|
|
private AuthorizationInfo GetAuthorizationInfoFromDictionary(
|
2021-05-20 21:28:18 +02:00
|
|
|
in Dictionary<string, string>? auth,
|
2020-06-15 21:21:18 +02:00
|
|
|
in IHeaderDictionary headers,
|
|
|
|
in IQueryCollection queryString)
|
|
|
|
{
|
2021-05-20 21:28:18 +02:00
|
|
|
string? deviceId = null;
|
|
|
|
string? device = null;
|
|
|
|
string? client = null;
|
|
|
|
string? version = null;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-15 21:21:18 +02:00
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
|
{
|
|
|
|
token = headers["X-Emby-Token"];
|
2017-01-13 22:05:12 +01:00
|
|
|
}
|
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
|
|
|
{
|
2020-06-15 21:21:18 +02:00
|
|
|
token = headers["X-MediaBrowser-Token"];
|
2015-10-05 00:04:56 +02:00
|
|
|
}
|
2020-06-15 21:21:18 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(token))
|
2014-07-04 04:22:57 +02:00
|
|
|
{
|
2020-06-15 21:21:18 +02:00
|
|
|
token = queryString["ApiKey"];
|
2014-07-04 04:22:57 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 00:45:17 +02:00
|
|
|
// TODO deprecate this query parameter.
|
2020-06-15 21:21:18 +02:00
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
|
{
|
|
|
|
token = queryString["api_key"];
|
|
|
|
}
|
|
|
|
|
|
|
|
var authInfo = 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,
|
2020-11-08 16:10:33 +01:00
|
|
|
Token = token,
|
2020-12-01 22:47:42 +01:00
|
|
|
IsAuthenticated = false,
|
|
|
|
HasToken = false
|
2014-07-02 06:57:18 +02:00
|
|
|
};
|
2014-11-15 03:31:03 +01:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(token))
|
2014-11-15 03:31:03 +01:00
|
|
|
{
|
2020-10-15 01:58:33 +02:00
|
|
|
// Request doesn't contain a token.
|
2020-11-08 16:10:33 +01:00
|
|
|
return authInfo;
|
2020-10-15 01:58:33 +02:00
|
|
|
}
|
2014-11-15 03:31:03 +01:00
|
|
|
|
2020-12-01 22:47:42 +01:00
|
|
|
authInfo.HasToken = true;
|
2020-10-15 01:58:33 +02:00
|
|
|
var result = _authRepo.Get(new AuthenticationInfoQuery
|
|
|
|
{
|
|
|
|
AccessToken = token
|
|
|
|
});
|
2014-11-15 03:31:03 +01:00
|
|
|
|
2020-11-08 16:10:33 +01:00
|
|
|
if (result.Items.Count > 0)
|
|
|
|
{
|
|
|
|
authInfo.IsAuthenticated = true;
|
|
|
|
}
|
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
var originalAuthenticationInfo = result.Items.Count > 0 ? result.Items[0] : null;
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if (originalAuthenticationInfo != null)
|
|
|
|
{
|
|
|
|
var updateToken = false;
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
// TODO: Remove these checks for IsNullOrWhiteSpace
|
|
|
|
if (string.IsNullOrWhiteSpace(authInfo.Client))
|
|
|
|
{
|
|
|
|
authInfo.Client = originalAuthenticationInfo.AppName;
|
|
|
|
}
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(authInfo.DeviceId))
|
|
|
|
{
|
|
|
|
authInfo.DeviceId = originalAuthenticationInfo.DeviceId;
|
|
|
|
}
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2020-10-15 01:58:33 +02: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;
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(authInfo.Device))
|
|
|
|
{
|
|
|
|
authInfo.Device = originalAuthenticationInfo.DeviceName;
|
|
|
|
}
|
|
|
|
else if (!string.Equals(authInfo.Device, originalAuthenticationInfo.DeviceName, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
if (allowTokenInfoUpdate)
|
2017-10-17 21:50:10 +02:00
|
|
|
{
|
2020-10-15 01:58:33 +02:00
|
|
|
updateToken = true;
|
|
|
|
originalAuthenticationInfo.DeviceName = authInfo.Device;
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
2020-10-15 01:58:33 +02:00
|
|
|
}
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(authInfo.Version))
|
|
|
|
{
|
|
|
|
authInfo.Version = originalAuthenticationInfo.AppVersion;
|
|
|
|
}
|
|
|
|
else if (!string.Equals(authInfo.Version, originalAuthenticationInfo.AppVersion, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
if (allowTokenInfoUpdate)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2017-10-17 21:50:10 +02:00
|
|
|
updateToken = true;
|
2020-10-15 01:58:33 +02:00
|
|
|
originalAuthenticationInfo.AppVersion = authInfo.Version;
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
2020-10-15 01:58:33 +02:00
|
|
|
}
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if ((DateTime.UtcNow - originalAuthenticationInfo.DateLastActivity).TotalMinutes > 3)
|
|
|
|
{
|
|
|
|
originalAuthenticationInfo.DateLastActivity = DateTime.UtcNow;
|
|
|
|
updateToken = true;
|
|
|
|
}
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if (!originalAuthenticationInfo.UserId.Equals(Guid.Empty))
|
|
|
|
{
|
|
|
|
authInfo.User = _userManager.GetUserById(originalAuthenticationInfo.UserId);
|
2017-10-17 21:50:10 +02:00
|
|
|
|
2020-10-15 01:58:33 +02:00
|
|
|
if (authInfo.User != null && !string.Equals(authInfo.User.Username, originalAuthenticationInfo.UserName, StringComparison.OrdinalIgnoreCase))
|
2017-10-17 21:50:10 +02:00
|
|
|
{
|
2020-10-15 01:58:33 +02:00
|
|
|
originalAuthenticationInfo.UserName = authInfo.User.Username;
|
|
|
|
updateToken = true;
|
2017-10-17 21:50:10 +02:00
|
|
|
}
|
2020-10-28 15:40:11 +01:00
|
|
|
|
2020-12-10 07:15:33 +01:00
|
|
|
authInfo.IsApiKey = false;
|
2020-10-28 15:40:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-10 07:15:33 +01:00
|
|
|
authInfo.IsApiKey = true;
|
2014-11-15 03:31:03 +01:00
|
|
|
}
|
2020-10-15 01:58:33 +02:00
|
|
|
|
|
|
|
if (updateToken)
|
|
|
|
{
|
|
|
|
_authRepo.Update(originalAuthenticationInfo);
|
2014-11-15 03:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 16:10:33 +01:00
|
|
|
return authInfo;
|
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>
|
2021-05-20 21:28:18 +02:00
|
|
|
private Dictionary<string, string>? GetAuthorizationDictionary(HttpContext httpReq)
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
2020-09-02 12:22:14 +02:00
|
|
|
var auth = httpReq.Request.Headers["X-Emby-Authorization"];
|
2020-06-15 21:21:18 +02:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(auth))
|
|
|
|
{
|
2020-09-02 12:22:14 +02:00
|
|
|
auth = httpReq.Request.Headers[HeaderNames.Authorization];
|
2020-06-15 21:21:18 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
return GetAuthorization(auth.Count > 0 ? auth[0] : null);
|
2020-06-15 21:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the auth.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="httpReq">The HTTP req.</param>
|
|
|
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
2021-05-20 21:28:18 +02:00
|
|
|
private Dictionary<string, string>? GetAuthorizationDictionary(HttpRequest httpReq)
|
2020-06-15 21:21:18 +02:00
|
|
|
{
|
2020-06-17 14:52:15 +02:00
|
|
|
var auth = httpReq.Headers["X-Emby-Authorization"];
|
2015-07-03 13:51:45 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(auth))
|
2015-07-03 13:51:45 +02:00
|
|
|
{
|
2019-03-05 19:20:28 +01:00
|
|
|
auth = httpReq.Headers[HeaderNames.Authorization];
|
2015-07-03 13:51:45 +02:00
|
|
|
}
|
2014-07-02 06:57:18 +02:00
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
return GetAuthorization(auth.Count > 0 ? auth[0] : null);
|
2014-07-02 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the authorization.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="authorizationHeader">The authorization header.</param>
|
|
|
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
2021-05-20 21:28:18 +02:00
|
|
|
private Dictionary<string, string>? GetAuthorization(ReadOnlySpan<char> authorizationHeader)
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
2019-09-02 08:19:29 +02:00
|
|
|
if (authorizationHeader == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-02 06:57:18 +02:00
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
var firstSpace = authorizationHeader.IndexOf(' ');
|
2014-07-02 06:57:18 +02:00
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
// There should be at least two parts
|
|
|
|
if (firstSpace == -1)
|
2019-09-02 08:19:29 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-02 06:57:18 +02:00
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
var name = authorizationHeader[..firstSpace];
|
2017-01-13 22:05:12 +01:00
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
if (!name.Equals("MediaBrowser", StringComparison.OrdinalIgnoreCase)
|
2021-05-16 15:27:31 +02:00
|
|
|
&& !name.Equals("Emby", StringComparison.OrdinalIgnoreCase))
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
authorizationHeader = authorizationHeader[(firstSpace + 1)..];
|
2014-07-02 06:57:18 +02:00
|
|
|
|
|
|
|
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
foreach (var item in authorizationHeader.Split(','))
|
2014-07-02 06:57:18 +02:00
|
|
|
{
|
2021-05-16 14:49:11 +02:00
|
|
|
var trimmedItem = item.Trim();
|
|
|
|
var firstEqualsSign = trimmedItem.IndexOf('=');
|
2014-09-05 05:48:53 +02:00
|
|
|
|
2021-05-16 14:49:11 +02:00
|
|
|
if (firstEqualsSign > 0)
|
2014-09-05 05:48:53 +02:00
|
|
|
{
|
2021-05-16 14:49:11 +02:00
|
|
|
var key = trimmedItem[..firstEqualsSign].ToString();
|
|
|
|
var value = NormalizeValue(trimmedItem[(firstEqualsSign + 1)..].Trim('"').ToString());
|
|
|
|
result[key] = 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
|
|
|
{
|
2020-06-15 21:21:18 +02:00
|
|
|
return string.IsNullOrEmpty(value) ? value : WebUtility.HtmlEncode(value);
|
2017-01-13 22:05:12 +01:00
|
|
|
}
|
2014-07-02 06:57:18 +02:00
|
|
|
}
|
|
|
|
}
|