forked from nightscout/cgm-remote-monitor
-
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.
Refactor core auth (nightscout#6596)
* Auth resolve now supports async/await * Read only tokens can be used for authentication and the UI shows privileges for these accounts correctly * Failed attempt at authenticating an API_SECRET or token delays subsequent auth attempt by 5000 ms
- Loading branch information
1 parent
6c90c0b
commit 6cea5b9
Showing
18 changed files
with
430 additions
and
262 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
function init () { | ||
|
||
const ipDelayList = {}; | ||
|
||
const DELAY_ON_FAIL = 5000; | ||
const FAIL_AGE = 60000; | ||
|
||
const sleep = require('util').promisify(setTimeout); | ||
|
||
ipDelayList.addFailedRequest = function addFailedRequest (ip) { | ||
const ipString = String(ip); | ||
let entry = ipDelayList[ipString]; | ||
const now = Date.now(); | ||
if (!entry) { | ||
ipDelayList[ipString] = now + DELAY_ON_FAIL; | ||
return; | ||
} | ||
if (now >= entry) { entry = now; } | ||
ipDelayList[ipString] = entry + DELAY_ON_FAIL; | ||
}; | ||
|
||
ipDelayList.shouldDelayRequest = function shouldDelayRequest (ip) { | ||
const ipString = String(ip); | ||
const entry = ipDelayList[ipString]; | ||
let now = Date.now(); | ||
if (entry) { | ||
if (now < entry) { | ||
return entry - now; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
ipDelayList.requestSucceeded = function requestSucceeded (ip) { | ||
const ipString = String(ip); | ||
if (ipDelayList[ipString]) { | ||
delete ipDelayList[ipString]; | ||
} | ||
}; | ||
|
||
// Clear items older than a minute | ||
|
||
setTimeout(function clearList () { | ||
for (var key in ipDelayList) { | ||
if (ipDelayList.hasOwnProperty(key)) { | ||
if (Date.now() > ipDelayList[key] + FAIL_AGE) { | ||
delete ipDelayList[key]; | ||
} | ||
} | ||
} | ||
}, 30000); | ||
|
||
return ipDelayList; | ||
} | ||
|
||
module.exports = init; |
Oops, something went wrong.