// // ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ // ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ // █__ --█ █__ --█ ◄█ - █ // █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ // ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ // │ Copyright (c) JP Dillingham. // │ // │ This program is free software: you can redistribute it and/or modify // │ it under the terms of the GNU Affero General Public License as published // │ by the Free Software Foundation, version 3. // │ // │ This program is distributed in the hope that it will be useful, // │ but WITHOUT ANY WARRANTY; without even the implied warranty of // │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // │ GNU Affero General Public License for more details. // │ // │ You should have received a copy of the GNU Affero General Public License // │ along with this program. If not, see https://www.gnu.org/licenses/. // │ // │ This program is distributed with Additional Terms pursuant to Section 7 // │ of the AGPLv3. See the LICENSE file in the root directory of this // │ project for the complete terms and conditions. // │ // │ https://slskd.org // │ // ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ // │ SPDX-FileCopyrightText: JP Dillingham // │ SPDX-License-Identifier: AGPL-3.0-only // ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ // using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace slskd.Authentication { using System; using System.Security.Principal; using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Serilog; /// /// API key authentication. /// public static class ApiKeyAuthentication { /// /// Gets the API key authentication scheme name. /// public static string AuthenticationScheme { get; } = "ApiKey"; } /// /// Handles API key authentication. /// public class ApiKeyAuthenticationHandler : AuthenticationHandler { /// /// Initializes a new instance of the class. /// /// An options monitor. /// The security service. /// A logger factory. /// A url encoder. public ApiKeyAuthenticationHandler( IOptionsMonitor apiKeyOptionsMonitor, SecurityService securityService, ILoggerFactory logger, UrlEncoder urlEncoder) : base(apiKeyOptionsMonitor, logger, urlEncoder) { Security = securityService; } private ILogger Log { get; } = Serilog.Log.ForContext(); private SecurityService Security { get; } /// /// Authenticates via API key. /// /// A successful authentication result containing a ticket for the API key. protected override async Task HandleAuthenticateAsync() { await Task.Yield(); if (!Request.Headers.TryGetValue("X-API-Key", out var headerKeyValue)) { return AuthenticateResult.NoResult(); } try { var key = Security.AuthenticateWithApiKey(headerKeyValue, Request.HttpContext.Connection.RemoteIpAddress); var identity = new GenericIdentity(key.Name); var principal = new GenericPrincipal(identity, new[] { key.Role.ToString() }); var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), ApiKeyAuthentication.AuthenticationScheme); return AuthenticateResult.Success(ticket); } catch (Exception ex) { Log.Warning("Unauthorized request from IP address {IP}: {Message}", Request.HttpContext.Connection.RemoteIpAddress, ex.Message); return AuthenticateResult.Fail(ex); } } } /// /// API key authentication options. /// #pragma warning disable S2094 // Classes should not be empty public class ApiKeyAuthenticationOptions : AuthenticationSchemeOptions #pragma warning restore S2094 // Classes should not be empty { } }