Skip to content

Commit

Permalink
Working implementation of PasswordManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Eden committed Nov 28, 2016
1 parent 20728a6 commit 8e96533
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Quondam/PasswordManager.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
using System;
using System.Web.Security;

namespace MartinEden.Quondam
{
public class PasswordManager : IPasswordManager
{
private IClock clock;
private UserStore store;

internal static readonly TimeSpan MaximumPasswordAge = TimeSpan.FromSeconds(30);

public PasswordManager()
: this(new RealClock()) { }
internal PasswordManager(IClock clock)
{
this.clock = clock;
store = new UserStore();
}

public string GenerateOneTimePassword(string username)
{
throw new NotImplementedException();
string password = Membership.GeneratePassword(16, 0);
store.StorePassword(username, password, clock.Now);
return password;
}

public bool ValidatePassword(string username, string password)
{
throw new NotImplementedException();
var record = store.GetPassword(username);
if (record != null && record.IsFresh(clock.Now) && record.Password == password)
{
store.ClearPassword(username);
return true;
}
return false;
}
}
}
21 changes: 21 additions & 0 deletions Quondam/PasswordRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace MartinEden.Quondam
{
internal class PasswordRecord
{
internal readonly string Password;
internal readonly DateTime Timestamp;

internal PasswordRecord(string password, DateTime timestamp)
{
Password = password;
Timestamp = timestamp;
}

internal bool IsFresh(DateTime now)
{
return (now - Timestamp) <= PasswordManager.MaximumPasswordAge;
}
}
}
3 changes: 3 additions & 0 deletions Quondam/Quondam.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -40,11 +41,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PasswordRecord.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RealClock.cs" />
<Compile Include="IClock.cs" />
<Compile Include="IPasswordManager.cs" />
<Compile Include="PasswordManager.cs" />
<Compile Include="UserStore.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
35 changes: 35 additions & 0 deletions Quondam/UserStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace MartinEden.Quondam
{
internal class UserStore
{
private Dictionary<string, PasswordRecord> records;

internal UserStore()
{
records = new Dictionary<string, PasswordRecord>();
}

internal void StorePassword(string username, string password, DateTime timestamp)
{
records[username] = new PasswordRecord(password, timestamp);
}

internal PasswordRecord GetPassword(string username)
{
PasswordRecord record;
if (records.TryGetValue(username, out record))
{
return record;
}
return null;
}

internal void ClearPassword(string username)
{
records.Remove(username);
}
}
}

0 comments on commit 8e96533

Please sign in to comment.