-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
cooldown.py
42 lines (32 loc) · 1.23 KB
/
cooldown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import random
import string
import time
from flask import session
import keystore
from shared import app
def _gen_cooldown_id():
return "captcha_cooldown_" + "".join(random.choices(string.hexdigits, k=10))
@app.context_processor
def cooldown_processor():
kwargs = {"on_captcha_cooldown": on_captcha_cooldown}
return dict(**kwargs)
def on_captcha_cooldown():
captcha_cooldown = False
cooldown_id = session.get("cooldown_id")
if app.config.get("CAPTCHA_COOLDOWN") and cooldown_id:
key_client = keystore.Keystore()
if key_client.exists(cooldown_id):
current_timestamp = int(time.time())
cooldown_timestamp = int(key_client.get(cooldown_id))
if current_timestamp - cooldown_timestamp < app.config["CAPTCHA_COOLDOWN"]:
captcha_cooldown = True
return captcha_cooldown
def refresh_captcha_cooldown():
if app.config.get("CAPTCHA_COOLDOWN"):
cooldown_id = session.get("cooldown_id")
if cooldown_id is None:
cooldown_id = _gen_cooldown_id()
current_timestamp = str(int(time.time()))
key_client = keystore.Keystore()
key_client.set(cooldown_id, current_timestamp)
session["cooldown_id"] = cooldown_id