Skip to content

Commit

Permalink
feat: allow $IPFS_PASS to hold the passphrase (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored Apr 9, 2019
1 parent 55bb6ea commit 2f283bd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/articles/envvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ are used to control the behaviour of the library
| HOME | The user's home directory |
| HOMEPATH | same as `HOME` |
| IPFS_PATH | The folder of the IPFS [repository](repository.md) |
| IPFS_PASS | Contains the passphrase for the [key chain](key.md) |
24 changes: 24 additions & 0 deletions src/IpfsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ public partial class IpfsEngine : ICoreApi, IService, IDisposable
SecureString passphrase;
ConcurrentBag<Func<Task>> stopTasks = new ConcurrentBag<Func<Task>>();

/// <summary>
/// Creates a new instance of the <see cref="IpfsEngine"/> class
/// with the IPFS_PASS environment variable.
/// </summary>
/// <remarks>
/// Th passphrase must be in the IPFS_PASS environment variable.
/// </remarks>
public IpfsEngine()
{
var s = Environment.GetEnvironmentVariable("IPFS_PASS");
if (s == null)
throw new Exception("The IPFS_PASS environement variable is missing.");

passphrase = new SecureString();
foreach (var c in s)
{
this.passphrase.AppendChar(c);
}
Init();
}

/// <summary>
/// Creates a new instance of the <see cref="IpfsEngine"/> class
/// with the specified passphrase.
Expand Down Expand Up @@ -64,6 +85,9 @@ public IpfsEngine(char[] passphrase)
/// <param name="passphrase">
/// The password used to access the keychain.
/// </param>
/// <remarks>
/// A copy of the <paramref name="passphrase"/> is made.
/// </remarks>
public IpfsEngine(SecureString passphrase)
{
this.passphrase = passphrase.Copy();
Expand Down
28 changes: 28 additions & 0 deletions test/IpfsEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ public async Task SecureString_Passphrase()
await ipfs.KeyChain();
}

[TestMethod]
public async Task IpfsPass_Passphrase()
{
var secret = "this is not a secure pass phrase";
var ipfs = new IpfsEngine(secret.ToCharArray());
ipfs.Options = TestFixture.Ipfs.Options;
await ipfs.KeyChain();

Environment.SetEnvironmentVariable("IPFS_PASS", secret);
try
{
ipfs = new IpfsEngine();
ipfs.Options = TestFixture.Ipfs.Options;
await ipfs.KeyChain();
}
finally
{
Environment.SetEnvironmentVariable("IPFS_PASS", null);
}
}

[TestMethod]
public async Task Wrong_Passphrase()
{
Expand All @@ -51,6 +72,13 @@ public async Task Wrong_Passphrase()
});
}

[TestMethod]
[ExpectedException(typeof(Exception))]
public void IpfsPass_Missing()
{
var _ = new IpfsEngine();
}

[TestMethod]
public async Task Can_Start_And_Stop()
{
Expand Down

0 comments on commit 2f283bd

Please sign in to comment.