From cd6874b3dd54c6b3e501439d308d591e4b2aada5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 5 Oct 2022 18:30:31 -0500 Subject: add auth policies --- src/slskd/Program.cs | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index c9417ed8..43d802b1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -579,6 +579,22 @@ namespace slskd if (!OptionsAtStartup.Web.Authentication.Disabled) { + services.AddAuthorization(options => + { + options.AddPolicy(AuthPolicy.JwtOnly, policy => + { + policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + + options.AddPolicy(AuthPolicy.Any, policy => + { + policy.AuthenticationSchemes.Add(ApiKeyAuthentication.AuthenticationScheme); + policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + }); + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { @@ -610,20 +626,27 @@ namespace slskd return Task.CompletedTask; }, }; + }) + .AddScheme(ApiKeyAuthentication.AuthenticationScheme, options => + { + options.EnableSignalRSupport = true; + options.SignalRRoutePrefix = "/hub"; + options.Role = Role.Administrator; }); - - //services.AddAuthentication(ApiKeyAuthentication.AuthenticationScheme) - // .AddScheme(ApiKeyAuthentication.AuthenticationScheme, options => - // { - // options.EnableSignalRSupport = true; - // options.SignalRRoutePrefix = "/hub"; - // options.Role = Role.Administrator; - // }); } else { Log.Warning("Authentication of web requests is DISABLED"); + services.AddAuthorization(options => + { + options.AddPolicy(AuthPolicy.Any, policy => + { + policy.AuthenticationSchemes.Add(PassthroughAuthentication.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + }); + services.AddAuthentication(PassthroughAuthentication.AuthenticationScheme) .AddScheme(PassthroughAuthentication.AuthenticationScheme, options => { -- cgit v1.2.3