-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working implementation of PasswordManager
- Loading branch information
Martin Eden
committed
Nov 28, 2016
1 parent
20728a6
commit 8e96533
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |