Skip to content

Commit 92e295e

Browse files
committed
fix: clear throttle on verify
1 parent 4487e2f commit 92e295e

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/backends/redis/lua/msTokenCreate.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ if throttle > 0 then
4545
end
4646

4747
local function insertToken(key, encoded)
48-
redis.call("HMSET", key, "id", id, "action", action, "uid", uid, "secret", secret, "created", created, "settings", secretSettings, "metadata", metadata, "related", encoded);
48+
redis.call(
49+
"HMSET", key,
50+
"id", id, "action", action, "uid", uid, "secret", secret, "created", created,
51+
"settings", secretSettings, "metadata", metadata, "related", encoded,
52+
"throttleKey", throttleKey -- needed later to delete throttle lock on success verification
53+
);
4954
-- put ttl if required
5055
if ttl > 0 then
5156
redis.call("EXPIRE", key, ttl);

src/backends/redis/lua/msVerifyToken.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ local secretKey = KEYS[1];
22
local timeVerified = ARGV[1];
33
local erase = ARGV[2];
44

5+
local function isempty(s)
6+
return s == false or s == nil or s == '';
7+
end
8+
59
if redis.call('exists', secretKey) ~= 1 then
610
return redis.error_reply("404");
711
end
@@ -19,7 +23,14 @@ if isFirstVerification == 1 or erase == 'true' then
1923

2024
-- if we have erase, we don't need to write anything
2125
if erase == 'true' then
26+
local throttleKey = redis.call('hget', secretKey, 'throttleKey');
27+
2228
redis.call('del', unpack(related));
29+
30+
-- delete throttle lock
31+
if not isempty(throttleKey) then
32+
redis.call("DEL", throttleKey);
33+
end
2334
else
2435
-- otherwise we need to attach xtra data
2536
for i,key in ipairs(related) do

src/backends/redis/redis.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class RedisBackend {
4040
created: Number,
4141
verified: Number,
4242
isFirstVerification: Boolean,
43+
throttleKey: String,
4344
};
4445

4546
// static instance of error

test/integration.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,20 @@ describe('TokenManager', () => {
456456
});
457457
});
458458

459+
it('completes challenge, clears lock by default', async () => {
460+
const createOpts = {
461+
id: ID,
462+
action: ACTION,
463+
ttl: 3,
464+
throttle: 1,
465+
};
466+
467+
const result = await manager.create(createOpts);
468+
await manager.verify(result.secret);
469+
470+
await assert.doesNotReject(() => manager.create(createOpts));
471+
});
472+
459473
it('completes challenge with unencrypted secret', async () => {
460474
const result = await manager.create({
461475
id: ID,

0 commit comments

Comments
 (0)