Merge pull request #2984 from crobibero/api-library

Move Library Service to Jellyfin.API
This commit is contained in:
Cody Robibero 2020-06-24 12:57:39 -06:00 committed by GitHub
commit 7c1020cfc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1246 additions and 1118 deletions

View file

@ -42,11 +42,13 @@ namespace Jellyfin.Api.Auth
/// <param name="claimsPrincipal">Request claims.</param>
/// <param name="ignoreSchedule">Whether to ignore parental control.</param>
/// <param name="localAccessOnly">Whether access is to be allowed locally only.</param>
/// <param name="requiredDownloadPermission">Whether validation requires download permission.</param>
/// <returns>Validated claim status.</returns>
protected bool ValidateClaims(
ClaimsPrincipal claimsPrincipal,
bool ignoreSchedule = false,
bool localAccessOnly = false)
bool localAccessOnly = false,
bool requiredDownloadPermission = false)
{
// Ensure claim has userId.
var userId = ClaimHelpers.GetUserId(claimsPrincipal);
@ -89,6 +91,13 @@ namespace Jellyfin.Api.Auth
return false;
}
// User attempting to download without permission.
if (requiredDownloadPermission
&& !user.HasPermission(PermissionKind.EnableContentDownloading))
{
return false;
}
return true;
}

View file

@ -0,0 +1,45 @@
using System.Threading.Tasks;
using Jellyfin.Api.Auth.DefaultAuthorizationPolicy;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Library;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
namespace Jellyfin.Api.Auth.DownloadPolicy
{
/// <summary>
/// Download authorization handler.
/// </summary>
public class DownloadHandler : BaseAuthorizationHandler<DownloadRequirement>
{
/// <summary>
/// Initializes a new instance of the <see cref="DownloadHandler"/> class.
/// </summary>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
/// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
public DownloadHandler(
IUserManager userManager,
INetworkManager networkManager,
IHttpContextAccessor httpContextAccessor)
: base(userManager, networkManager, httpContextAccessor)
{
}
/// <inheritdoc />
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, DownloadRequirement requirement)
{
var validated = ValidateClaims(context.User);
if (validated)
{
context.Succeed(requirement);
}
else
{
context.Fail();
}
return Task.CompletedTask;
}
}
}

View file

@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Authorization;
namespace Jellyfin.Api.Auth.DownloadPolicy
{
/// <summary>
/// The download permission requirement.
/// </summary>
public class DownloadRequirement : IAuthorizationRequirement
{
}
}

View file

@ -29,5 +29,10 @@ namespace Jellyfin.Api.Constants
/// Policy name for escaping schedule controls.
/// </summary>
public const string IgnoreSchedule = "IgnoreSchedule";
/// <summary>
/// Policy name for requiring download permission.
/// </summary>
public const string Download = "Download";
}
}

File diff suppressed because it is too large Load diff

View file

@ -87,7 +87,7 @@ namespace Jellyfin.Api.Helpers
return Array.Empty<Guid>();
}
return value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
return Split(value, ',', true)
.Select(i => new Guid(i))
.ToArray();
}

View file

@ -0,0 +1,18 @@
namespace Jellyfin.Api.Models.LibraryDtos
{
/// <summary>
/// Library option info dto.
/// </summary>
public class LibraryOptionInfoDto
{
/// <summary>
/// Gets or sets name.
/// </summary>
public string? Name { get; set; }
/// <summary>
/// Gets or sets a value indicating whether default enabled.
/// </summary>
public bool DefaultEnabled { get; set; }
}
}

View file

@ -0,0 +1,34 @@
using System.Diagnostics.CodeAnalysis;
namespace Jellyfin.Api.Models.LibraryDtos
{
/// <summary>
/// Library options result dto.
/// </summary>
public class LibraryOptionsResultDto
{
/// <summary>
/// Gets or sets the metadata savers.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "MetadataSavers", Justification = "Imported from ServiceStack")]
public LibraryOptionInfoDto[] MetadataSavers { get; set; } = null!;
/// <summary>
/// Gets or sets the metadata readers.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "MetadataReaders", Justification = "Imported from ServiceStack")]
public LibraryOptionInfoDto[] MetadataReaders { get; set; } = null!;
/// <summary>
/// Gets or sets the subtitle fetchers.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "SubtitleFetchers", Justification = "Imported from ServiceStack")]
public LibraryOptionInfoDto[] SubtitleFetchers { get; set; } = null!;
/// <summary>
/// Gets or sets the type options.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "TypeOptions", Justification = "Imported from ServiceStack")]
public LibraryTypeOptionsDto[] TypeOptions { get; set; } = null!;
}
}

View file

@ -0,0 +1,41 @@
using System.Diagnostics.CodeAnalysis;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
namespace Jellyfin.Api.Models.LibraryDtos
{
/// <summary>
/// Library type options dto.
/// </summary>
public class LibraryTypeOptionsDto
{
/// <summary>
/// Gets or sets the type.
/// </summary>
public string? Type { get; set; }
/// <summary>
/// Gets or sets the metadata fetchers.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "MetadataFetchers", Justification = "Imported from ServiceStack")]
public LibraryOptionInfoDto[] MetadataFetchers { get; set; } = null!;
/// <summary>
/// Gets or sets the image fetchers.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "ImageFetchers", Justification = "Imported from ServiceStack")]
public LibraryOptionInfoDto[] ImageFetchers { get; set; } = null!;
/// <summary>
/// Gets or sets the supported image types.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "SupportedImageTypes", Justification = "Imported from ServiceStack")]
public ImageType[] SupportedImageTypes { get; set; } = null!;
/// <summary>
/// Gets or sets the default image options.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:ReturnArrays", MessageId = "DefaultImageOptions", Justification = "Imported from ServiceStack")]
public ImageOption[] DefaultImageOptions { get; set; } = null!;
}
}

View file

@ -0,0 +1,19 @@
namespace Jellyfin.Api.Models.LibraryDtos
{
/// <summary>
/// Media Update Info Dto.
/// </summary>
public class MediaUpdateInfoDto
{
/// <summary>
/// Gets or sets media path.
/// </summary>
public string? Path { get; set; }
/// <summary>
/// Gets or sets media update type.
/// Created, Modified, Deleted.
/// </summary>
public string? UpdateType { get; set; }
}
}

View file

@ -6,6 +6,7 @@ using System.Reflection;
using Jellyfin.Api;
using Jellyfin.Api.Auth;
using Jellyfin.Api.Auth.DefaultAuthorizationPolicy;
using Jellyfin.Api.Auth.DownloadPolicy;
using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
using Jellyfin.Api.Auth.IgnoreSchedulePolicy;
using Jellyfin.Api.Auth.LocalAccessPolicy;
@ -39,6 +40,7 @@ namespace Jellyfin.Server.Extensions
public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<IAuthorizationHandler, DefaultAuthorizationHandler>();
serviceCollection.AddSingleton<IAuthorizationHandler, DownloadHandler>();
serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrElevatedHandler>();
serviceCollection.AddSingleton<IAuthorizationHandler, IgnoreScheduleHandler>();
serviceCollection.AddSingleton<IAuthorizationHandler, LocalAccessHandler>();
@ -52,6 +54,13 @@ namespace Jellyfin.Server.Extensions
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
policy.AddRequirements(new DefaultAuthorizationRequirement());
});
options.AddPolicy(
Policies.Download,
policy =>
{
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
policy.AddRequirements(new DownloadRequirement());
});
options.AddPolicy(
Policies.FirstTimeSetupOrElevated,
policy =>

File diff suppressed because it is too large Load diff

View file

@ -14,6 +14,10 @@
<Compile Include="..\SharedVersion.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Library" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>