| | | 1 | | using System; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using MediaBrowser.Controller.Configuration; |
| | | 4 | | using Microsoft.AspNetCore.Cors.Infrastructure; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.Server.Configuration |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Cors policy provider. |
| | | 11 | | /// </summary> |
| | | 12 | | public class CorsPolicyProvider : ICorsPolicyProvider |
| | | 13 | | { |
| | | 14 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="CorsPolicyProvider"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface |
| | | 20 | | public CorsPolicyProvider(IServerConfigurationManager serverConfigurationManager) |
| | | 21 | | { |
| | 172 | 22 | | _serverConfigurationManager = serverConfigurationManager; |
| | 172 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public Task<CorsPolicy?> GetPolicyAsync(HttpContext context, string? policyName) |
| | | 27 | | { |
| | 0 | 28 | | var corsHosts = _serverConfigurationManager.Configuration.CorsHosts; |
| | 0 | 29 | | var builder = new CorsPolicyBuilder() |
| | 0 | 30 | | .AllowAnyMethod() |
| | 0 | 31 | | .AllowAnyHeader(); |
| | | 32 | | |
| | | 33 | | // No hosts configured or only default configured. |
| | 0 | 34 | | if (corsHosts.Length == 0 |
| | 0 | 35 | | || (corsHosts.Length == 1 |
| | 0 | 36 | | && string.Equals(corsHosts[0], CorsConstants.AnyOrigin, StringComparison.Ordinal))) |
| | | 37 | | { |
| | 0 | 38 | | builder.AllowAnyOrigin(); |
| | | 39 | | } |
| | | 40 | | else |
| | | 41 | | { |
| | 0 | 42 | | builder.WithOrigins(corsHosts) |
| | 0 | 43 | | .AllowCredentials(); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | return Task.FromResult<CorsPolicy?>(builder.Build()); |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | } |