1using System;
2using System.Web;
3using Microsoft.AspNetCore.Authentication;
4using Microsoft.AspNetCore.Authentication.Cookies;
5using Microsoft.Extensions.DependencyInjection;
6using NzbDrone.Core.Authentication;
7using NzbDrone.Core.Configuration;
8
9namespace Radarr.Http.Authentication
10{
11 public static class AuthenticationBuilderExtensions
12 {
13 public static AuthenticationBuilder AddApiKey(this AuthenticationBuilder authenticationBuilder, string name, Action<ApiKeyAuthenticationOptions> options)
…lines 14–28 unchanged (15 lines)
29 {
30 return authenticationBuilder.AddScheme<AuthenticationSchemeOptions, NoAuthenticationHandler>(name, options => { });
31 }
32
33 public static AuthenticationBuilder AddAppAuthentication(this IServiceCollection services)
34 {
35 services.AddOptions<CookieAuthenticationOptions>(AuthenticationType.Forms.ToString())
36 .Configure<IConfigFileProvider>((options, configFileProvider) =>
37 {
38 // Url Encode the cookie name to account for spaces or other invalid characters in the configured instance name
39 var instanceName = HttpUtility.UrlEncode(configFileProvider.InstanceName);
40
41 options.Cookie.Name = $"{instanceName}Auth";
42 options.AccessDeniedPath = "/login?loginFailed=true";
43 options.LoginPath = "/login";
44 options.ExpireTimeSpan = TimeSpan.FromDays(7);
45 options.SlidingExpiration = true;
46 options.ReturnUrlParameter = "returnUrl";
47 });
48
49 return services.AddAuthentication()
50 .AddNone(AuthenticationType.None.ToString())
51 .AddExternal(AuthenticationType.External.ToString())
52 .AddBasic(AuthenticationType.Basic.ToString())
53 .AddCookie(AuthenticationType.Forms.ToString())
54 .AddApiKey("API", options =>
55 {
56 options.HeaderName = "X-Api-Key";
57 options.QueryName = "apikey";
58 })
59 .AddApiKey("SignalR", options =>
…lines 60–67 unchanged (8 lines)