-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-1628: Make login time the same for registered and unregistered u…
…sers (#2839)
- Loading branch information
1 parent
d9408cc
commit 95bb09d
Showing
4 changed files
with
94 additions
and
29 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/VirtoCommerce.Platform.Core/Security/DelayedResponse.cs
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,49 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
|
||
namespace VirtoCommerce.Platform.Core.Security; | ||
|
||
// This class allows to measure the duration of a succeeded response and delay subsequent failed responses to prevent timing attacks. | ||
public class DelayedResponse | ||
{ | ||
private const int _minDelay = 150; // milliseconds | ||
private static readonly ConcurrentDictionary<string, int> _delaysByName = new(); | ||
|
||
private readonly string _name; | ||
private readonly Stopwatch _stopwatch; | ||
private readonly Task _failedDelayTask; | ||
private readonly Task _succeededDelayTask; | ||
|
||
public static DelayedResponse Create(params string[] nameParts) | ||
{ | ||
return new DelayedResponse(string.Join(".", nameParts)); | ||
} | ||
|
||
public DelayedResponse(string name) | ||
{ | ||
_name = name; | ||
_stopwatch = Stopwatch.StartNew(); | ||
_delaysByName.TryAdd(name, 0); | ||
var failedDelay = Math.Max(_minDelay, _delaysByName[name]); | ||
_failedDelayTask = Task.Delay(failedDelay); | ||
_succeededDelayTask = Task.Delay(_minDelay); | ||
} | ||
|
||
public Task FailAsync() | ||
{ | ||
return _failedDelayTask; | ||
} | ||
|
||
public Task SucceedAsync() | ||
{ | ||
if (_stopwatch.IsRunning) | ||
{ | ||
_stopwatch.Stop(); | ||
_delaysByName[_name] = (int)_stopwatch.ElapsedMilliseconds; | ||
} | ||
|
||
return _succeededDelayTask; | ||
} | ||
} |
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
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