diff options
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 8dbf39ff..fd80d649 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -32,6 +32,7 @@ namespace slskd using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; + using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.DataProtection; @@ -69,6 +70,7 @@ namespace slskd using Soulseek; using Utility.CommandLine; using Utility.EnvironmentVariables; + using static slskd.Authentication.ApiKeyAuthenticationHandler; using IOFile = System.IO.File; /// <summary> @@ -207,6 +209,9 @@ namespace slskd [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } + [Argument('k', "generate-api-key", "generate a random API key")] + private static bool GenerateApiKey { get; set; } + [Argument('n', "no-logo", "suppress logo on startup")] private static bool NoLogo { get; set; } @@ -263,6 +268,12 @@ namespace slskd return; } + if (GenerateApiKey) + { + Log.Information($"API Key: {Cryptography.Random.GetBytes(32).ToBase62String()}"); + return; + } + // the application isn't being run in command mode. derive the application directory value // and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; @@ -600,6 +611,14 @@ namespace slskd }, }; }); + + services.AddAuthentication(ApiKeyAuthentication.AuthenticationScheme) + .AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthentication.AuthenticationScheme, options => + { + options.EnableSignalRSupport = true; + options.SignalRRoutePrefix = "/hub"; + options.Role = Role.Administrator; + }); } else { @@ -805,6 +824,7 @@ namespace slskd .MinimumLevel.Override("Microsoft", LogEventLevel.Error) .MinimumLevel.Override("System.Net.Http.HttpClient", OptionsAtStartup.Debug ? LogEventLevel.Warning : LogEventLevel.Fatal) .MinimumLevel.Override("slskd.Authentication.PassthroughAuthenticationHandler", LogEventLevel.Warning) + .MinimumLevel.Override("slskd.Authentication.ApiKeyAuthenticationHandler", LogEventLevel.Warning) .Enrich.WithProperty("InstanceName", OptionsAtStartup.InstanceName) .Enrich.WithProperty("InvocationId", InvocationId) .Enrich.WithProperty("ProcessId", ProcessId) @@ -945,8 +965,8 @@ namespace slskd var cert = X509.Generate(subject: AppName, password, X509KeyStorageFlags.Exportable); IOFile.WriteAllBytes(filename, cert.Export(X509ContentType.Pkcs12, password)); - Log.Information($"Password: {password}"); Log.Information($"Certificate exported to {filename}"); + Log.Information($"Password: {password}"); } private static void PrintCommandLineArguments(Type targetType) |
