diff options
author | Guillaume Lacasa <guillaumelacasa@hotmail.com> | 2016-06-29 12:59:03 +0200 |
---|---|---|
committer | Guillaume Lacasa <guillaumelacasa@hotmail.com> | 2016-06-29 12:59:03 +0200 |
commit | dc68717cf5bc471bbcfc08906a84e22199327575 (patch) | |
tree | cdeab1684df3555ea9a8dc9d10209f7632010209 | |
parent | 8e80d5923232b6b5852873a8c54fc3d8bbbc9a96 (diff) | |
download | TwoStepsAuthenticator-1.1.zip TwoStepsAuthenticator-1.1.tar.gz TwoStepsAuthenticator-1.1.tar.bz2 |
Default UsedCodeManager specific to dotnet core1.1
-rw-r--r-- | .vs/restore.dg | 3 | ||||
-rw-r--r-- | TwoStepsAuthenticator.DotnetCore/UsedCodesManager.cs | 92 | ||||
-rw-r--r-- | TwoStepsAuthenticator.TestApp/ViewModel.cs | 2 | ||||
-rw-r--r-- | TwoStepsAuthenticator.nuspec | 3 |
4 files changed, 93 insertions, 7 deletions
diff --git a/.vs/restore.dg b/.vs/restore.dg index 53ba924..e59e4de 100644 --- a/.vs/restore.dg +++ b/.vs/restore.dg @@ -1 +1,2 @@ -#:C:\Git\TwoStepsAuthenticator\TwoStepsAuthenticator.DotnetCore\TwoStepsAuthenticator.DotnetCore.xproj +#:C:\Git\TwoStepsAuthenticator\TwoStepsAuthenticator.DotnetCore.TestApp\TwoStepsAuthenticator.DotnetCore.TestApp.xproj +C:\Git\TwoStepsAuthenticator\TwoStepsAuthenticator.DotnetCore.TestApp\TwoStepsAuthenticator.DotnetCore.TestApp.xproj|C:\Git\TwoStepsAuthenticator\TwoStepsAuthenticator.DotnetCore\TwoStepsAuthenticator.DotnetCore.xproj diff --git a/TwoStepsAuthenticator.DotnetCore/UsedCodesManager.cs b/TwoStepsAuthenticator.DotnetCore/UsedCodesManager.cs index 168b934..3b21e35 100644 --- a/TwoStepsAuthenticator.DotnetCore/UsedCodesManager.cs +++ b/TwoStepsAuthenticator.DotnetCore/UsedCodesManager.cs @@ -1,18 +1,102 @@ using System; using System.Collections.Generic; +using System.Threading; namespace TwoStepsAuthenticator { public class UsedCodesManager : IUsedCodesManager { - public void AddCode(long timestamp, string code, object user) + internal sealed class UsedCode { - throw new NotImplementedException(); + public UsedCode(long timestamp, String code, object user) + { + this.UseDate = DateTime.Now; + this.Code = code; + this.Timestamp = timestamp; + this.User = user; + } + + internal DateTime UseDate { get; private set; } + internal long Timestamp { get; private set; } + internal String Code { get; private set; } + internal object User { get; private set; } + + public override bool Equals(object obj) + { + if (Object.ReferenceEquals(this, obj)) + { + return true; + } + + var other = obj as UsedCode; + return (other != null) && this.Code.Equals(other.Code) && this.Timestamp.Equals(other.Timestamp) && this.User.Equals(other.User); + } + public override string ToString() + { + return String.Format("{0}: {1}", Timestamp, Code); + } + public override int GetHashCode() + { + return Code.GetHashCode() + (Timestamp.GetHashCode() + User.GetHashCode() * 17) * 17; + } } - public bool IsCodeUsed(long timestamp, string code, object user) + private readonly Queue<UsedCode> codes; + private readonly System.Threading.ReaderWriterLockSlim rwlock = new System.Threading.ReaderWriterLockSlim(); + private readonly Timer cleaner; + + public UsedCodesManager() { - throw new NotImplementedException(); + codes = new Queue<UsedCode>(); + var delay = (int)TimeSpan.FromMinutes(5).TotalMilliseconds; + cleaner = new Timer(cleaner_Elapsed, null, delay, delay); + } + + void cleaner_Elapsed(object state) + { + var timeToClean = DateTime.Now.AddMinutes(-5); + + try + { + rwlock.EnterWriteLock(); + + while (codes.Count > 0 && codes.Peek().UseDate < timeToClean) + { + codes.Dequeue(); + } + } + finally + { + rwlock.ExitWriteLock(); + } + } + + public void AddCode(long timestamp, String code, object user) + { + try + { + rwlock.EnterWriteLock(); + + codes.Enqueue(new UsedCode(timestamp, code, user)); + } + finally + { + rwlock.ExitWriteLock(); + } + } + + public bool IsCodeUsed(long timestamp, String code, object user) + { + try + { + rwlock.EnterWriteLock(); + + return codes.Contains(new UsedCode(timestamp, code, user)); + } + finally + { + rwlock.ExitWriteLock(); + } } } diff --git a/TwoStepsAuthenticator.TestApp/ViewModel.cs b/TwoStepsAuthenticator.TestApp/ViewModel.cs index adc07a4..6bb7389 100644 --- a/TwoStepsAuthenticator.TestApp/ViewModel.cs +++ b/TwoStepsAuthenticator.TestApp/ViewModel.cs @@ -98,7 +98,7 @@ namespace TwoStepsAuthenticatorTestApp var auth = new TwoStepsAuthenticator.TimeAuthenticator(); Code = auth.GetCode(this.Key); - auth.CheckCode(key, Code); + auth.CheckCode(key, Code, "user"); } } } diff --git a/TwoStepsAuthenticator.nuspec b/TwoStepsAuthenticator.nuspec index 43f9bfa..c057a9d 100644 --- a/TwoStepsAuthenticator.nuspec +++ b/TwoStepsAuthenticator.nuspec @@ -2,7 +2,7 @@ <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata> <id>TwoStepsAuthenticator</id> - <version>1.0.1</version> + <version>1.1.0.0</version> <title>TwoStepsAuthenticator</title> <authors>Guillaume Lacasa</authors> <licenseUrl>https://raw.githubusercontent.com/glacasa/TwoStepsAuthenticator/master/LICENSE</licenseUrl> @@ -12,5 +12,6 @@ </metadata> <files> <file src="TwoStepsAuthenticator\bin\Release\TwoStepsAuthenticator.dll" target="lib\net45\TwoStepsAuthenticator.dll" /> + <file src="TwoStepsAuthenticator.DotnetCore\bin\Release\netstandard1.6\TwoStepsAuthenticator.DotnetCore.dll" target="lib\netcore\TwoStepsAuthenticator.dll" /> </files> </package>
\ No newline at end of file |