diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-14 20:03:36 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-06-14 20:03:36 -0500 |
| commit | 050be311e68bb316cfc40fbd23bfd5a812e1a060 (patch) | |
| tree | 1476cba9e90845f224bbc0a380e4633697e1306e /src/slskd/Common | |
| parent | 4cf8f8fa862b08131e9cc51c4bad3d6c9838342b (diff) | |
| download | slskd-050be311e68bb316cfc40fbd23bfd5a812e1a060.tar.gz | |
move stuff around
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/Authentication/PassthroughAuthentication.cs | 91 | ||||
| -rw-r--r-- | src/slskd/Common/Compute.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Common/Cryptography/Pbkdf2.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Common/Cryptography/X509.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 31 | ||||
| -rw-r--r-- | src/slskd/Common/UserEndPointCache.cs | 61 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/Extensions.cs | 48 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/X509CertificateAttribute.cs | 2 |
8 files changed, 150 insertions, 95 deletions
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 file="PassthroughAuthentication.cs" company="slskd Team"> +// 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/. +// </copyright> + +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; + + /// <summary> + /// Passthrough authentication. + /// </summary> + public static class PassthroughAuthentication + { + /// <summary> + /// Gets the Passthrough authentication scheme name. + /// </summary> + public static string AuthenticationScheme { get; } = "Passthrough"; + } + + /// <summary> + /// Handles passthrough authentication. + /// </summary> + public class PassthroughAuthenticationHandler : AuthenticationHandler<PassthroughAuthenticationOptions> + { + /// <summary> + /// Initializes a new instance of the <see cref="PassthroughAuthenticationHandler"/> class. + /// </summary> + /// <param name="optionsMonitor">An options monitor.</param> + /// <param name="logger">A logger factory.</param> + /// <param name="urlEncoder">A url encoder.</param> + /// <param name="systemClock">A system clock interface.</param> + public PassthroughAuthenticationHandler(IOptionsMonitor<PassthroughAuthenticationOptions> optionsMonitor, ILoggerFactory logger, UrlEncoder urlEncoder, ISystemClock systemClock) + : base(optionsMonitor, logger, urlEncoder, systemClock) + { + } + + /// <summary> + /// Authenticates using the configured <see cref="PassthroughAuthenticationOptions.Username"/> and <see cref="PassthroughAuthenticationOptions.Role"/>. + /// </summary> + /// <returns>A successful authentication result containing a default ticket.</returns> + protected override Task<AuthenticateResult> 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)); + } + } + + /// <summary> + /// Passthrough authentication options. + /// </summary> + public class PassthroughAuthenticationOptions : AuthenticationSchemeOptions + { + /// <summary> + /// Initializes a new instance of the <see cref="PassthroughAuthenticationOptions"/> class. + /// </summary> + public PassthroughAuthenticationOptions() + { + } + + /// <summary> + /// Gets or sets the username for the passed-through authentication ticket. + /// </summary> + public string Username { get; set; } = "Anonymous"; + + /// <summary> + /// Gets or sets the role for the passed-through authentication ticket. + /// </summary> + 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; /// <summary> /// 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/. // </copyright> -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/. // </copyright> -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 @@ -55,17 +55,6 @@ namespace slskd } /// <summary> - /// Returns the SHA1 hash of the given string. - /// </summary> - /// <param name="str">The string to hash.</param> - /// <returns>The hash.</returns> - public static string Sha1(this string str) - { - using var sha1 = new SHA1Managed(); - return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty); - } - - /// <summary> /// Formats byte to nearest size (KB, MB, etc.) /// </summary> /// <param name="value">The value to format.</param> @@ -148,25 +137,5 @@ namespace slskd { return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); } - - /// <summary> - /// Validates options. - /// </summary> - /// <param name="options">The options instance to validate.</param> - /// <param name="result">The result of the validation, if invalid.</param> - /// <returns>A value indicating whether the instance is valid.</returns> - public static bool TryValidate(this Options options, out CompositeValidationResult result) - { - result = null; - var results = new List<ValidationResult>(); - - 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 file="UserEndPointCache.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd -{ - using System; - using System.Net; - using Microsoft.Extensions.Caching.Memory; - using Soulseek; - - /// <summary> - /// Caches user EndPoints. - /// </summary> - public class UserEndPointCache : IUserEndPointCache - { - /// <summary> - /// Initializes a new instance of the <see cref="UserEndPointCache"/> class. - /// </summary> - public UserEndPointCache() - { - Cache = new MemoryCache(new MemoryCacheOptions()); - } - - private IMemoryCache Cache { get; } - - /// <summary> - /// Caches or updates an entry. - /// </summary> - /// <param name="username">The username for which to cache the endpoint.</param> - /// <param name="endPoint">The endpoint to cache.</param> - public void AddOrUpdate(string username, IPEndPoint endPoint) - { - Cache.Set(username, endPoint, TimeSpan.FromSeconds(60)); - } - - /// <summary> - /// Gets the cached endpoint for the specified <paramref name="username"/>, if it exists. - /// </summary> - /// <param name="username">The username for which to retrieve the endpoint.</param> - /// <param name="endPoint">The cached endpoint, if it exists.</param> - /// <returns>A value indicating whether the endpoint was found in the cache.</returns> - 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 file="Extensions.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Validation +{ + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + + /// <summary> + /// Extensions. + /// </summary> + public static class Extensions + { + /// <summary> + /// Validates options. + /// </summary> + /// <param name="options">The options instance to validate.</param> + /// <param name="result">The result of the validation, if invalid.</param> + /// <returns>A value indicating whether the instance is valid.</returns> + public static bool TryValidate(this Options options, out CompositeValidationResult result) + { + result = null; + var results = new List<ValidationResult>(); + + 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; /// <summary> |
