You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
284 lines
9.3 KiB
284 lines
9.3 KiB
5 years ago
|
#pragma warning disable CS1591
|
||
|
|
||
10 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
7 years ago
|
using System.Linq;
|
||
5 years ago
|
using System.Net;
|
||
4 years ago
|
using System.Threading.Tasks;
|
||
6 years ago
|
using MediaBrowser.Controller.Library;
|
||
6 years ago
|
using MediaBrowser.Controller.Net;
|
||
4 years ago
|
using Microsoft.AspNetCore.Http;
|
||
6 years ago
|
using Microsoft.Net.Http.Headers;
|
||
10 years ago
|
|
||
4 years ago
|
namespace Jellyfin.Server.Implementations.Security
|
||
10 years ago
|
{
|
||
|
public class AuthorizationContext : IAuthorizationContext
|
||
|
{
|
||
4 years ago
|
private readonly JellyfinDb _jellyfinDb;
|
||
6 years ago
|
private readonly IUserManager _userManager;
|
||
10 years ago
|
|
||
4 years ago
|
public AuthorizationContext(JellyfinDb jellyfinDb, IUserManager userManager)
|
||
10 years ago
|
{
|
||
4 years ago
|
_jellyfinDb = jellyfinDb;
|
||
6 years ago
|
_userManager = userManager;
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
public Task<AuthorizationInfo> GetAuthorizationInfo(HttpContext requestContext)
|
||
10 years ago
|
{
|
||
4 years ago
|
if (requestContext.Request.HttpContext.Items.TryGetValue("AuthorizationInfo", out var cached) && cached != null)
|
||
10 years ago
|
{
|
||
4 years ago
|
return Task.FromResult((AuthorizationInfo)cached);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
return GetAuthorization(requestContext);
|
||
|
}
|
||
|
|
||
4 years ago
|
public async Task<AuthorizationInfo> GetAuthorizationInfo(HttpRequest requestContext)
|
||
4 years ago
|
{
|
||
|
var auth = GetAuthorizationDictionary(requestContext);
|
||
4 years ago
|
var authInfo = await GetAuthorizationInfoFromDictionary(auth, requestContext.Headers, requestContext.Query).ConfigureAwait(false);
|
||
4 years ago
|
return authInfo;
|
||
|
}
|
||
|
|
||
10 years ago
|
/// <summary>
|
||
|
/// Gets the authorization.
|
||
|
/// </summary>
|
||
|
/// <param name="httpReq">The HTTP req.</param>
|
||
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
||
4 years ago
|
private async Task<AuthorizationInfo> GetAuthorization(HttpContext httpReq)
|
||
10 years ago
|
{
|
||
|
var auth = GetAuthorizationDictionary(httpReq);
|
||
4 years ago
|
var authInfo = await GetAuthorizationInfoFromDictionary(auth, httpReq.Request.Headers, httpReq.Request.Query).ConfigureAwait(false);
|
||
10 years ago
|
|
||
4 years ago
|
httpReq.Request.HttpContext.Items["AuthorizationInfo"] = authInfo;
|
||
4 years ago
|
return authInfo;
|
||
|
}
|
||
|
|
||
4 years ago
|
private async Task<AuthorizationInfo> GetAuthorizationInfoFromDictionary(
|
||
|
IReadOnlyDictionary<string, string>? auth,
|
||
|
IHeaderDictionary headers,
|
||
|
IQueryCollection queryString)
|
||
4 years ago
|
{
|
||
4 years ago
|
string? deviceId = null;
|
||
|
string? deviceName = null;
|
||
|
string? client = null;
|
||
|
string? version = null;
|
||
|
string? token = null;
|
||
10 years ago
|
|
||
|
if (auth != null)
|
||
|
{
|
||
|
auth.TryGetValue("DeviceId", out deviceId);
|
||
4 years ago
|
auth.TryGetValue("Device", out deviceName);
|
||
10 years ago
|
auth.TryGetValue("Client", out client);
|
||
|
auth.TryGetValue("Version", out version);
|
||
8 years ago
|
auth.TryGetValue("Token", out token);
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
#pragma warning disable CA1508
|
||
|
// headers can return StringValues.Empty
|
||
4 years ago
|
if (string.IsNullOrEmpty(token))
|
||
|
{
|
||
|
token = headers["X-Emby-Token"];
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
6 years ago
|
if (string.IsNullOrEmpty(token))
|
||
9 years ago
|
{
|
||
4 years ago
|
token = headers["X-MediaBrowser-Token"];
|
||
9 years ago
|
}
|
||
4 years ago
|
|
||
6 years ago
|
if (string.IsNullOrEmpty(token))
|
||
10 years ago
|
{
|
||
4 years ago
|
token = queryString["ApiKey"];
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
// TODO deprecate this query parameter.
|
||
4 years ago
|
if (string.IsNullOrEmpty(token))
|
||
|
{
|
||
|
token = queryString["api_key"];
|
||
|
}
|
||
|
|
||
|
var authInfo = new AuthorizationInfo
|
||
10 years ago
|
{
|
||
|
Client = client,
|
||
4 years ago
|
Device = deviceName,
|
||
10 years ago
|
DeviceId = deviceId,
|
||
10 years ago
|
Version = version,
|
||
4 years ago
|
Token = token,
|
||
4 years ago
|
IsAuthenticated = false,
|
||
|
HasToken = false
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
4 years ago
|
if (string.IsNullOrWhiteSpace(token))
|
||
10 years ago
|
{
|
||
4 years ago
|
// Request doesn't contain a token.
|
||
4 years ago
|
return authInfo;
|
||
4 years ago
|
}
|
||
4 years ago
|
#pragma warning restore CA1508
|
||
10 years ago
|
|
||
4 years ago
|
authInfo.HasToken = true;
|
||
4 years ago
|
var device = await _jellyfinDb.Devices.FirstOrDefaultAsync(d => d.AccessToken == token).ConfigureAwait(false);
|
||
10 years ago
|
|
||
4 years ago
|
if (device != null)
|
||
4 years ago
|
{
|
||
|
authInfo.IsAuthenticated = true;
|
||
|
}
|
||
|
|
||
4 years ago
|
if (device != null)
|
||
4 years ago
|
{
|
||
|
var updateToken = false;
|
||
7 years ago
|
|
||
4 years ago
|
// TODO: Remove these checks for IsNullOrWhiteSpace
|
||
|
if (string.IsNullOrWhiteSpace(authInfo.Client))
|
||
|
{
|
||
4 years ago
|
authInfo.Client = device.AppName;
|
||
4 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
if (string.IsNullOrWhiteSpace(authInfo.DeviceId))
|
||
|
{
|
||
4 years ago
|
authInfo.DeviceId = device.DeviceId;
|
||
4 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
// Temporary. TODO - allow clients to specify that the token has been shared with a casting device
|
||
4 years ago
|
var allowTokenInfoUpdate = !authInfo.Client.Contains("chromecast", StringComparison.OrdinalIgnoreCase);
|
||
7 years ago
|
|
||
4 years ago
|
if (string.IsNullOrWhiteSpace(authInfo.Device))
|
||
|
{
|
||
4 years ago
|
authInfo.Device = device.DeviceName;
|
||
4 years ago
|
}
|
||
4 years ago
|
else if (!string.Equals(authInfo.Device, device.DeviceName, StringComparison.OrdinalIgnoreCase))
|
||
4 years ago
|
{
|
||
|
if (allowTokenInfoUpdate)
|
||
7 years ago
|
{
|
||
4 years ago
|
updateToken = true;
|
||
4 years ago
|
device.DeviceName = authInfo.Device;
|
||
6 years ago
|
}
|
||
4 years ago
|
}
|
||
6 years ago
|
|
||
4 years ago
|
if (string.IsNullOrWhiteSpace(authInfo.Version))
|
||
|
{
|
||
4 years ago
|
authInfo.Version = device.AppVersion;
|
||
4 years ago
|
}
|
||
4 years ago
|
else if (!string.Equals(authInfo.Version, device.AppVersion, StringComparison.OrdinalIgnoreCase))
|
||
4 years ago
|
{
|
||
|
if (allowTokenInfoUpdate)
|
||
6 years ago
|
{
|
||
7 years ago
|
updateToken = true;
|
||
4 years ago
|
device.AppVersion = authInfo.Version;
|
||
6 years ago
|
}
|
||
4 years ago
|
}
|
||
6 years ago
|
|
||
4 years ago
|
if ((DateTime.UtcNow - device.DateLastActivity).TotalMinutes > 3)
|
||
4 years ago
|
{
|
||
4 years ago
|
device.DateLastActivity = DateTime.UtcNow;
|
||
4 years ago
|
updateToken = true;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
if (!device.UserId.Equals(Guid.Empty))
|
||
4 years ago
|
{
|
||
4 years ago
|
authInfo.User = _userManager.GetUserById(device.UserId);
|
||
4 years ago
|
authInfo.IsApiKey = false;
|
||
4 years ago
|
}
|
||
|
else
|
||
|
{
|
||
4 years ago
|
authInfo.IsApiKey = true;
|
||
10 years ago
|
}
|
||
4 years ago
|
|
||
|
if (updateToken)
|
||
|
{
|
||
4 years ago
|
_jellyfinDb.Devices.Update(device);
|
||
|
await _jellyfinDb.SaveChangesAsync().ConfigureAwait(false);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
return authInfo;
|
||
10 years ago
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the auth.
|
||
|
/// </summary>
|
||
|
/// <param name="httpReq">The HTTP req.</param>
|
||
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
||
4 years ago
|
private Dictionary<string, string>? GetAuthorizationDictionary(HttpContext httpReq)
|
||
10 years ago
|
{
|
||
4 years ago
|
var auth = httpReq.Request.Headers["X-Emby-Authorization"];
|
||
4 years ago
|
|
||
|
if (string.IsNullOrEmpty(auth))
|
||
|
{
|
||
4 years ago
|
auth = httpReq.Request.Headers[HeaderNames.Authorization];
|
||
4 years ago
|
}
|
||
|
|
||
|
return GetAuthorization(auth);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the auth.
|
||
|
/// </summary>
|
||
|
/// <param name="httpReq">The HTTP req.</param>
|
||
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
||
4 years ago
|
private Dictionary<string, string>? GetAuthorizationDictionary(HttpRequest httpReq)
|
||
4 years ago
|
{
|
||
4 years ago
|
var auth = httpReq.Headers["X-Emby-Authorization"];
|
||
9 years ago
|
|
||
6 years ago
|
if (string.IsNullOrEmpty(auth))
|
||
9 years ago
|
{
|
||
6 years ago
|
auth = httpReq.Headers[HeaderNames.Authorization];
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
|
return GetAuthorization(auth);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the authorization.
|
||
|
/// </summary>
|
||
|
/// <param name="authorizationHeader">The authorization header.</param>
|
||
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
||
4 years ago
|
private Dictionary<string, string>? GetAuthorization(string? authorizationHeader)
|
||
10 years ago
|
{
|
||
5 years ago
|
if (authorizationHeader == null)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
10 years ago
|
|
||
4 years ago
|
var parts = authorizationHeader.Split(' ', 2);
|
||
10 years ago
|
|
||
|
// There should be at least to parts
|
||
5 years ago
|
if (parts.Length != 2)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
10 years ago
|
|
||
4 years ago
|
var acceptedNames = new[] { "MediaBrowser", "Emby" };
|
||
8 years ago
|
|
||
10 years ago
|
// It has to be a digest request
|
||
5 years ago
|
if (!acceptedNames.Contains(parts[0], StringComparer.OrdinalIgnoreCase))
|
||
10 years ago
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
// Remove uptil the first space
|
||
10 years ago
|
authorizationHeader = parts[1];
|
||
10 years ago
|
parts = authorizationHeader.Split(',');
|
||
|
|
||
|
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||
|
|
||
|
foreach (var item in parts)
|
||
|
{
|
||
4 years ago
|
var param = item.Trim().Split('=', 2);
|
||
10 years ago
|
|
||
|
if (param.Length == 2)
|
||
|
{
|
||
4 years ago
|
var value = NormalizeValue(param[1].Trim('"'));
|
||
4 years ago
|
result[param[0]] = value;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
9 years ago
|
|
||
6 years ago
|
private static string NormalizeValue(string value)
|
||
8 years ago
|
{
|
||
4 years ago
|
return string.IsNullOrEmpty(value) ? value : WebUtility.HtmlEncode(value);
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|