From c69871a66e8d7c054d4652fbb0390d66a92b1c6a Mon Sep 17 00:00:00 2001 From: Grzegorz Kucmierz Date: Tue, 3 Dec 2019 15:48:47 +0100 Subject: [PATCH] Implement brute force protection --- implement-brute-force-protection/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 implement-brute-force-protection/index.js diff --git a/implement-brute-force-protection/index.js b/implement-brute-force-protection/index.js new file mode 100644 index 0000000..f32d890 --- /dev/null +++ b/implement-brute-force-protection/index.js @@ -0,0 +1,20 @@ +// https://www.codewars.com/kata/implement-brute-force-protection/javascript + +const bruteForceDetected = (() => { + const failedAttemps = {}; + return loginReq => { + if (loginReq.successful) { + if (loginReq.sourceIP in failedAttemps) { + failedAttemps[loginReq.sourceIP] = 0; + } + return false; + } + if (loginReq.sourceIP in failedAttemps) { + const cnt = failedAttemps[loginReq.sourceIP] + 1; + failedAttemps[loginReq.sourceIP] = cnt; + return 20 <= cnt; + } + failedAttemps[loginReq.sourceIP] = 1; + return false; + }; +})();