From 848262831fe6281d448afd3bdcf802bbc643fbd1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 13 Dec 2025 14:19:52 -0600 Subject: remove ISecurityService interface, add logging to api key handler --- src/slskd/Common/Authentication/ApiKeyAuthentication.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/slskd/Common/Authentication/ApiKeyAuthentication.cs') diff --git a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs index 1aaa4a4f..fb209478 100644 --- a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs +++ b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs @@ -15,6 +15,7 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace slskd.Authentication @@ -24,7 +25,7 @@ namespace slskd.Authentication using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; - using Microsoft.Extensions.Logging; + using Serilog; /// /// API key authentication. @@ -51,7 +52,7 @@ namespace slskd.Authentication /// A url encoder. public ApiKeyAuthenticationHandler( IOptionsMonitor apiKeyOptionsMonitor, - ISecurityService securityService, + SecurityService securityService, ILoggerFactory logger, UrlEncoder urlEncoder) : base(apiKeyOptionsMonitor, logger, urlEncoder) @@ -59,7 +60,8 @@ namespace slskd.Authentication Security = securityService; } - private ISecurityService Security { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); + private SecurityService Security { get; } /// /// Authenticates via API key. @@ -86,6 +88,7 @@ namespace slskd.Authentication } catch (Exception ex) { + Log.Warning(ex, "Unauthorized: {Message}", ex.Message); return AuthenticateResult.Fail(ex); } } -- cgit v1.2.3 From fc1aecbe54b0077d0fde20f9f8e6350312344692 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 13 Dec 2025 14:36:09 -0600 Subject: tweak logging --- src/slskd/Common/Authentication/ApiKeyAuthentication.cs | 2 +- src/slskd/Core/Security/SecurityService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Common/Authentication/ApiKeyAuthentication.cs') diff --git a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs index fb209478..e2733442 100644 --- a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs +++ b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs @@ -88,7 +88,7 @@ namespace slskd.Authentication } catch (Exception ex) { - Log.Warning(ex, "Unauthorized: {Message}", ex.Message); + Log.Warning("Unauthorized request from IP address {IP}: {Message}", Request.HttpContext.Connection.RemoteIpAddress, ex.Message); return AuthenticateResult.Fail(ex); } } diff --git a/src/slskd/Core/Security/SecurityService.cs b/src/slskd/Core/Security/SecurityService.cs index 8b3afa44..e98dd5eb 100644 --- a/src/slskd/Core/Security/SecurityService.cs +++ b/src/slskd/Core/Security/SecurityService.cs @@ -115,7 +115,7 @@ public class SecurityService .Select(cidr => IPAddressRange.Parse(cidr)) .Any(range => range.Contains(callerIpAddress))) { - throw new OutOfRangeException($"IP Address {callerIpAddress} not included in CIDR(s) for API key {record.Key}: {record.Value.Cidr}"); + throw new OutOfRangeException($"IP address {callerIpAddress} not included in CIDR range(s) for API key {record.Key}; allowed: {record.Value.Cidr}"); } return (record.Key, record.Value.Role.ToEnum()); -- cgit v1.2.3