aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Security
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2020-12-30 21:16:50 -0600
committerJP Dillingham <jpdillingham@gmail.com>2020-12-30 21:16:50 -0600
commit8f1068ac3135bcccf09374bc787ef22049b43461 (patch)
tree5d80a254f5ee7b7c6b3956f6a7307591bc5a93e2 /src/slskd/Security
parentec4a85e4e8b64a0ae91c494e3c8ef520f0a8b8c3 (diff)
downloadslskd-8f1068ac3135bcccf09374bc787ef22049b43461.tar.gz
copy files from example
Diffstat (limited to 'src/slskd/Security')
-rw-r--r--src/slskd/Security/PBKDF2.cs28
-rw-r--r--src/slskd/Security/PassthroughAuthentication.cs40
2 files changed, 68 insertions, 0 deletions
diff --git a/src/slskd/Security/PBKDF2.cs b/src/slskd/Security/PBKDF2.cs
new file mode 100644
index 00000000..4a857135
--- /dev/null
+++ b/src/slskd/Security/PBKDF2.cs
@@ -0,0 +1,28 @@
+namespace slskd.Security
+{
+ using Microsoft.AspNetCore.Cryptography.KeyDerivation;
+ using System.Security.Cryptography;
+
+ /// <summary>
+ /// PBKDF2/RFC 2898 implementation
+ /// </summary>
+ public static class PBKDF2
+ {
+ /// <summary>
+ /// Gets a 256 bit (32 byte) key derived from the specified <paramref name="password"/> using PBKDF2/RFC 2898
+ /// </summary>
+ /// <param name="password"></param>
+ /// <returns></returns>
+ public static byte[] GetKey(string password)
+ {
+ byte[] salt = new byte[16];
+ int iterations = 1000;
+
+ using (var rng = RandomNumberGenerator.Create())
+ {
+ rng.GetBytes(salt);
+ return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32);
+ }
+ }
+ }
+}
diff --git a/src/slskd/Security/PassthroughAuthentication.cs b/src/slskd/Security/PassthroughAuthentication.cs
new file mode 100644
index 00000000..69b94df5
--- /dev/null
+++ b/src/slskd/Security/PassthroughAuthentication.cs
@@ -0,0 +1,40 @@
+namespace slskd.Security
+{
+ using Microsoft.AspNetCore.Authentication;
+ using Microsoft.Extensions.Logging;
+ using Microsoft.Extensions.Options;
+ using System.Security.Principal;
+ using System.Text.Encodings.Web;
+ using System.Threading.Tasks;
+
+ public class PassthroughAuthentication
+ {
+ public static string AuthenticationScheme { get; } = "Passthrough";
+ }
+
+ public class PassthroughAuthenticationHandler : AuthenticationHandler<PassthroughAuthenticationOptions>
+ {
+ public PassthroughAuthenticationHandler(IOptionsMonitor<PassthroughAuthenticationOptions> optionsMonitor, ILoggerFactory logger, UrlEncoder urlEncoder, ISystemClock systemClock)
+ : base(optionsMonitor, logger, urlEncoder, systemClock)
+ {
+ }
+
+ protected override Task<AuthenticateResult> HandleAuthenticateAsync()
+ {
+ var identity = new GenericIdentity(Options.Username);
+ var principal = new GenericPrincipal(identity, new[] { "User" });
+ var ticket = new AuthenticationTicket(principal , new AuthenticationProperties(), PassthroughAuthentication.AuthenticationScheme);
+
+ return Task.FromResult(AuthenticateResult.Success(ticket));
+ }
+ }
+
+ public class PassthroughAuthenticationOptions : AuthenticationSchemeOptions
+ {
+ public PassthroughAuthenticationOptions()
+ {
+ }
+
+ public string Username { get; set; }
+ }
+}