mirror of
https://github.com/Lidarr/Lidarr
synced 2025-12-09 18:03:26 +01:00
68 lines
3.2 KiB
C#
68 lines
3.2 KiB
C#
using System;
|
|
using System.Text.RegularExpressions;
|
|
using Diacritical;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NzbDrone.Core.Authentication;
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
namespace Lidarr.Http.Authentication
|
|
{
|
|
public static class AuthenticationBuilderExtensions
|
|
{
|
|
private static readonly Regex CookieNameRegex = new Regex(@"[^a-z0-9]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
public static AuthenticationBuilder AddApiKey(this AuthenticationBuilder authenticationBuilder, string name, Action<ApiKeyAuthenticationOptions> options)
|
|
{
|
|
return authenticationBuilder.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(name, options);
|
|
}
|
|
|
|
public static AuthenticationBuilder AddNone(this AuthenticationBuilder authenticationBuilder, string name)
|
|
{
|
|
return authenticationBuilder.AddScheme<AuthenticationSchemeOptions, NoAuthenticationHandler>(name, options => { });
|
|
}
|
|
|
|
public static AuthenticationBuilder AddExternal(this AuthenticationBuilder authenticationBuilder, string name)
|
|
{
|
|
return authenticationBuilder.AddScheme<AuthenticationSchemeOptions, NoAuthenticationHandler>(name, options => { });
|
|
}
|
|
|
|
public static AuthenticationBuilder AddAppAuthentication(this IServiceCollection services)
|
|
{
|
|
services.AddOptions<CookieAuthenticationOptions>(AuthenticationType.Forms.ToString())
|
|
.Configure<IConfigFileProvider>((options, configFileProvider) =>
|
|
{
|
|
// Replace diacritics and replace non-word characters to ensure cookie name doesn't contain any valid URL characters not allowed in cookie names
|
|
var instanceName = configFileProvider.InstanceName;
|
|
instanceName = instanceName.RemoveDiacritics();
|
|
instanceName = CookieNameRegex.Replace(instanceName, string.Empty);
|
|
|
|
options.Cookie.Name = $"{instanceName}Auth";
|
|
options.AccessDeniedPath = "/login?loginFailed=true";
|
|
options.LoginPath = "/login";
|
|
options.ExpireTimeSpan = TimeSpan.FromDays(7);
|
|
options.SlidingExpiration = true;
|
|
options.ReturnUrlParameter = "returnUrl";
|
|
});
|
|
|
|
return services.AddAuthentication()
|
|
.AddNone(AuthenticationType.None.ToString())
|
|
.AddExternal(AuthenticationType.External.ToString())
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
.AddCookie(AuthenticationType.Basic.ToString())
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
|
.AddCookie(AuthenticationType.Forms.ToString())
|
|
.AddApiKey("API", options =>
|
|
{
|
|
options.HeaderName = "X-Api-Key";
|
|
options.QueryName = "apikey";
|
|
})
|
|
.AddApiKey("SignalR", options =>
|
|
{
|
|
options.HeaderName = "X-Api-Key";
|
|
options.QueryName = "access_token";
|
|
});
|
|
}
|
|
}
|
|
}
|