Skip to content

Commit

Permalink
Add StringPrivateKeySource
Browse files Browse the repository at this point in the history
  • Loading branch information
derwasp authored and Denys Zhuravel committed Aug 2, 2018
1 parent 9bad63b commit 28cc702
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
14 changes: 1 addition & 13 deletions GitHubJwt/EnvironmentVariablePrivateKeySource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Text;

namespace GitHubJwt
{
Expand All @@ -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();
}
}
}
16 changes: 16 additions & 0 deletions GitHubJwt/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
26 changes: 26 additions & 0 deletions GitHubJwt/StringPrivateKeySource.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 28cc702

Please sign in to comment.