2020-08-30 09:32:14 -06:00
|
|
|
|
using System;
|
|
|
|
|
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
2020-06-01 11:03:08 -06:00
|
|
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Models
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Server Cors Policy.
|
|
|
|
|
|
/// </summary>
|
2020-08-30 09:32:14 -06:00
|
|
|
|
public class ServerCorsPolicy
|
2020-06-01 11:03:08 -06:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Default policy name.
|
|
|
|
|
|
/// </summary>
|
2020-08-30 09:32:14 -06:00
|
|
|
|
public const string DefaultPolicyName = nameof(ServerCorsPolicy);
|
2020-06-01 11:03:08 -06:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-30 09:32:14 -06:00
|
|
|
|
/// Initializes a new instance of the <see cref="ServerCorsPolicy"/> class.
|
2020-06-01 11:03:08 -06:00
|
|
|
|
/// </summary>
|
2020-08-30 09:32:14 -06:00
|
|
|
|
/// <param name="corsHosts">The configured cors hosts.</param>
|
|
|
|
|
|
public ServerCorsPolicy(string[] corsHosts)
|
2020-06-01 11:03:08 -06:00
|
|
|
|
{
|
2020-08-30 09:32:14 -06:00
|
|
|
|
var builder = new CorsPolicyBuilder()
|
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
|
.AllowAnyHeader();
|
2020-06-01 11:03:08 -06:00
|
|
|
|
|
2020-08-30 09:32:14 -06:00
|
|
|
|
// No hosts configured or only default configured.
|
|
|
|
|
|
if (corsHosts.Length == 0
|
|
|
|
|
|
|| (corsHosts.Length == 1
|
|
|
|
|
|
&& string.Equals(corsHosts[0], "*", StringComparison.Ordinal)))
|
|
|
|
|
|
{
|
|
|
|
|
|
builder.AllowAnyOrigin();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
builder.WithOrigins(corsHosts)
|
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
|
}
|
2020-06-01 11:03:08 -06:00
|
|
|
|
|
2020-08-30 09:32:14 -06:00
|
|
|
|
Policy = builder.Build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the cors policy.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public CorsPolicy Policy { get; }
|
2020-06-01 11:03:08 -06:00
|
|
|
|
}
|
2020-08-30 09:32:14 -06:00
|
|
|
|
}
|