From 050be311e68bb316cfc40fbd23bfd5a812e1a060 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 14 Jun 2021 20:03:36 -0500 Subject: move stuff around --- .../Authentication/PassthroughAuthentication.cs | 91 ++++++++++++++++++++++ src/slskd/Common/Compute.cs | 8 ++ src/slskd/Common/Cryptography/Pbkdf2.cs | 2 +- src/slskd/Common/Cryptography/X509.cs | 2 +- src/slskd/Common/Extensions.cs | 31 -------- src/slskd/Common/UserEndPointCache.cs | 61 --------------- src/slskd/Common/Validation/Extensions.cs | 48 ++++++++++++ .../Common/Validation/X509CertificateAttribute.cs | 2 +- 8 files changed, 150 insertions(+), 95 deletions(-) create mode 100644 src/slskd/Common/Authentication/PassthroughAuthentication.cs delete mode 100644 src/slskd/Common/UserEndPointCache.cs create mode 100644 src/slskd/Common/Validation/Extensions.cs (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Authentication/PassthroughAuthentication.cs b/src/slskd/Common/Authentication/PassthroughAuthentication.cs new file mode 100644 index 00000000..5f186aea --- /dev/null +++ b/src/slskd/Common/Authentication/PassthroughAuthentication.cs @@ -0,0 +1,91 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// 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, either version 3 of the License, or +// (at your option) any later version. +// +// 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/. +// + +namespace slskd.Authentication +{ + using System.Security.Principal; + using System.Text.Encodings.Web; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authentication; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; + + /// + /// Passthrough authentication. + /// + public static class PassthroughAuthentication + { + /// + /// Gets the Passthrough authentication scheme name. + /// + public static string AuthenticationScheme { get; } = "Passthrough"; + } + + /// + /// Handles passthrough authentication. + /// + public class PassthroughAuthenticationHandler : AuthenticationHandler + { + /// + /// Initializes a new instance of the class. + /// + /// An options monitor. + /// A logger factory. + /// A url encoder. + /// A system clock interface. + public PassthroughAuthenticationHandler(IOptionsMonitor optionsMonitor, ILoggerFactory logger, UrlEncoder urlEncoder, ISystemClock systemClock) + : base(optionsMonitor, logger, urlEncoder, systemClock) + { + } + + /// + /// Authenticates using the configured and . + /// + /// A successful authentication result containing a default ticket. + protected override Task HandleAuthenticateAsync() + { + var identity = new GenericIdentity(Options.Username); + var principal = new GenericPrincipal(identity, new[] { Options.Role.ToString() }); + var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), PassthroughAuthentication.AuthenticationScheme); + + return Task.FromResult(AuthenticateResult.Success(ticket)); + } + } + + /// + /// Passthrough authentication options. + /// + public class PassthroughAuthenticationOptions : AuthenticationSchemeOptions + { + /// + /// Initializes a new instance of the class. + /// + public PassthroughAuthenticationOptions() + { + } + + /// + /// Gets or sets the username for the passed-through authentication ticket. + /// + public string Username { get; set; } = "Anonymous"; + + /// + /// Gets or sets the role for the passed-through authentication ticket. + /// + public Role Role { get; set; } = Role.Administrator; + } +} diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs index 172de252..3d913d27 100644 --- a/src/slskd/Common/Compute.cs +++ b/src/slskd/Common/Compute.cs @@ -18,6 +18,8 @@ namespace slskd { using System; + using System.Security.Cryptography; + using System.Text; /// /// Computational functions. @@ -35,5 +37,11 @@ namespace slskd return (clampedDelay, jitter); } + + public static string Sha1Hash(string str) + { + using var sha1 = new SHA1Managed(); + return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty); + } } } \ No newline at end of file diff --git a/src/slskd/Common/Cryptography/Pbkdf2.cs b/src/slskd/Common/Cryptography/Pbkdf2.cs index f6509a73..3c3246d6 100644 --- a/src/slskd/Common/Cryptography/Pbkdf2.cs +++ b/src/slskd/Common/Cryptography/Pbkdf2.cs @@ -15,7 +15,7 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // -namespace slskd.Security +namespace slskd.Cryptography { using System.Security.Cryptography; using Microsoft.AspNetCore.Cryptography.KeyDerivation; diff --git a/src/slskd/Common/Cryptography/X509.cs b/src/slskd/Common/Cryptography/X509.cs index 95743cc0..6356d1e1 100644 --- a/src/slskd/Common/Cryptography/X509.cs +++ b/src/slskd/Common/Cryptography/X509.cs @@ -15,7 +15,7 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // -namespace slskd.Common.Cryptography +namespace slskd.Cryptography { using System; using System.Security.Cryptography; diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 32cbf443..a7a89a71 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -54,17 +54,6 @@ namespace slskd return !type.IsGenericType ? type.Name : type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(a => a.ToColloquialString())) + ">"; } - /// - /// Returns the SHA1 hash of the given string. - /// - /// The string to hash. - /// The hash. - public static string Sha1(this string str) - { - using var sha1 = new SHA1Managed(); - return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty); - } - /// /// Formats byte to nearest size (KB, MB, etc.) /// @@ -148,25 +137,5 @@ namespace slskd { return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); } - - /// - /// Validates options. - /// - /// The options instance to validate. - /// The result of the validation, if invalid. - /// A value indicating whether the instance is valid. - public static bool TryValidate(this Options options, out CompositeValidationResult result) - { - result = null; - var results = new List(); - - if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true)) - { - result = new CompositeValidationResult("Invalid configuration", results); - return false; - } - - return true; - } } } \ No newline at end of file diff --git a/src/slskd/Common/UserEndPointCache.cs b/src/slskd/Common/UserEndPointCache.cs deleted file mode 100644 index 29164cc4..00000000 --- a/src/slskd/Common/UserEndPointCache.cs +++ /dev/null @@ -1,61 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// 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, either version 3 of the License, or -// (at your option) any later version. -// -// 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/. -// - -namespace slskd -{ - using System; - using System.Net; - using Microsoft.Extensions.Caching.Memory; - using Soulseek; - - /// - /// Caches user EndPoints. - /// - public class UserEndPointCache : IUserEndPointCache - { - /// - /// Initializes a new instance of the class. - /// - public UserEndPointCache() - { - Cache = new MemoryCache(new MemoryCacheOptions()); - } - - private IMemoryCache Cache { get; } - - /// - /// Caches or updates an entry. - /// - /// The username for which to cache the endpoint. - /// The endpoint to cache. - public void AddOrUpdate(string username, IPEndPoint endPoint) - { - Cache.Set(username, endPoint, TimeSpan.FromSeconds(60)); - } - - /// - /// Gets the cached endpoint for the specified , if it exists. - /// - /// The username for which to retrieve the endpoint. - /// The cached endpoint, if it exists. - /// A value indicating whether the endpoint was found in the cache. - public bool TryGet(string username, out IPEndPoint endPoint) - { - return Cache.TryGetValue(username, out endPoint); - } - } -} \ No newline at end of file diff --git a/src/slskd/Common/Validation/Extensions.cs b/src/slskd/Common/Validation/Extensions.cs new file mode 100644 index 00000000..b7e396a3 --- /dev/null +++ b/src/slskd/Common/Validation/Extensions.cs @@ -0,0 +1,48 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// 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, either version 3 of the License, or +// (at your option) any later version. +// +// 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/. +// + +namespace slskd.Validation +{ + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + + /// + /// Extensions. + /// + public static class Extensions + { + /// + /// Validates options. + /// + /// The options instance to validate. + /// The result of the validation, if invalid. + /// A value indicating whether the instance is valid. + public static bool TryValidate(this Options options, out CompositeValidationResult result) + { + result = null; + var results = new List(); + + if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true)) + { + result = new CompositeValidationResult("Invalid configuration", results); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/src/slskd/Common/Validation/X509CertificateAttribute.cs b/src/slskd/Common/Validation/X509CertificateAttribute.cs index 1f0849f9..7a080882 100644 --- a/src/slskd/Common/Validation/X509CertificateAttribute.cs +++ b/src/slskd/Common/Validation/X509CertificateAttribute.cs @@ -18,7 +18,7 @@ namespace slskd.Validation { using System.ComponentModel.DataAnnotations; - using slskd.Common.Cryptography; + using slskd.Cryptography; using static slskd.Options.WebOptions.HttpsOptions; /// -- cgit v1.2.3