From 28cc702d89ab6b76fa3afcc526d209837abdaead Mon Sep 17 00:00:00 2001 From: Denys Zhuravel Date: Thu, 2 Aug 2018 18:59:40 +0200 Subject: [PATCH] Add StringPrivateKeySource --- .../EnvironmentVariablePrivateKeySource.cs | 14 +--------- GitHubJwt/StringExtensions.cs | 16 ++++++++++++ GitHubJwt/StringPrivateKeySource.cs | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 GitHubJwt/StringExtensions.cs create mode 100644 GitHubJwt/StringPrivateKeySource.cs diff --git a/GitHubJwt/EnvironmentVariablePrivateKeySource.cs b/GitHubJwt/EnvironmentVariablePrivateKeySource.cs index bf68bad..1f5c40d 100644 --- a/GitHubJwt/EnvironmentVariablePrivateKeySource.cs +++ b/GitHubJwt/EnvironmentVariablePrivateKeySource.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Text; namespace GitHubJwt { @@ -15,20 +14,9 @@ public EnvironmentVariablePrivateKeySource(string environmentVariableName) public TextReader GetPrivateKeyReader() { - var privateKeyPem = HydrateEnvVarPem( - Environment.GetEnvironmentVariable(environmentVariableName)); + var privateKeyPem = Environment.GetEnvironmentVariable(environmentVariableName).HydrateRsaVariable(); return new StringReader(privateKeyPem); } - private static string HydrateEnvVarPem(string input) - { - var stringBuilder = new StringBuilder(); - - stringBuilder.AppendLine("-----BEGIN RSA PRIVATE KEY-----"); - stringBuilder.AppendLine(input); - stringBuilder.AppendLine("-----END RSA PRIVATE KEY-----"); - - return stringBuilder.ToString(); - } } } diff --git a/GitHubJwt/StringExtensions.cs b/GitHubJwt/StringExtensions.cs new file mode 100644 index 0000000..0bc8b1a --- /dev/null +++ b/GitHubJwt/StringExtensions.cs @@ -0,0 +1,16 @@ +using System.Text; + +namespace GitHubJwt +{ + internal static class StringExtensions + { + public static string HydrateRsaVariable(this string input) + { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.AppendLine("-----BEGIN RSA PRIVATE KEY-----"); + stringBuilder.AppendLine(input); + stringBuilder.AppendLine("-----END RSA PRIVATE KEY-----"); + return stringBuilder.ToString(); + } + } +} diff --git a/GitHubJwt/StringPrivateKeySource.cs b/GitHubJwt/StringPrivateKeySource.cs new file mode 100644 index 0000000..a04665f --- /dev/null +++ b/GitHubJwt/StringPrivateKeySource.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; +using System.Text; + +namespace GitHubJwt +{ + public class StringPrivateKeySource : IPrivateKeySource + { + protected readonly string Key; + + public StringPrivateKeySource(string key) + { + if (string.IsNullOrEmpty(key)) + { + throw new ArgumentNullException(nameof(key)); + } + + Key = key; + } + + public TextReader GetPrivateKeyReader() + { + return new StringReader(Key.HydrateRsaVariable()); + } + } +}