easyctf-2017/server/api/utils.py

50 lines
1.6 KiB
Python
Raw Normal View History

2015-12-23 03:44:51 +00:00
import datetime
2015-12-23 23:23:18 +00:00
import json
2015-12-23 03:44:51 +00:00
import random
2016-01-12 03:54:26 +00:00
import re
2016-01-16 16:41:16 +00:00
import requests
2015-12-23 03:44:51 +00:00
import string
2015-12-23 23:23:18 +00:00
import traceback
2016-01-06 06:15:57 +00:00
import unicodedata
2015-12-23 03:44:51 +00:00
2016-01-16 16:41:16 +00:00
from flask import current_app as app
2015-12-23 23:23:18 +00:00
from functools import wraps
2015-12-23 03:44:51 +00:00
from werkzeug.security import generate_password_hash, check_password_hash
2016-01-12 03:54:26 +00:00
__check_email_format = lambda email: re.match(".+@.+\..{2,}", email) is not None
__check_ascii = lambda s: all(c in string.printable for c in s)
2016-01-16 21:12:32 +00:00
__check_alphanumeric = lambda s: all(c in string.digits + string.ascii_uppercase + string.ascii_lowercase for c in s)
2016-01-12 03:54:26 +00:00
2015-12-23 03:44:51 +00:00
def hash_password(s):
2015-12-23 23:23:18 +00:00
return generate_password_hash(s)
2015-12-23 03:44:51 +00:00
def check_password(hashed_password, try_password):
2015-12-23 23:23:18 +00:00
return check_password_hash(hashed_password, try_password)
2015-12-23 03:44:51 +00:00
2016-01-06 06:15:57 +00:00
def generate_string(length=32, alpha=string.hexdigits):
return "".join([random.choice(alpha) for x in range(length)])
2015-12-23 03:44:51 +00:00
def unix_time_millis(dt):
2015-12-23 23:23:18 +00:00
epoch = datetime.datetime.utcfromtimestamp(0)
return (dt - epoch).total_seconds() * 1000.0
2015-12-23 03:44:51 +00:00
def get_time_since_epoch():
2015-12-23 23:23:18 +00:00
return unix_time_millis(datetime.datetime.now())
2016-01-06 06:15:57 +00:00
def flat_multi(multidict):
flat = {}
for key, values in multidict.items():
value = values[0] if type(values) == list and len(values) == 1 else values
2016-01-12 03:54:26 +00:00
flat[key] = value.encode("utf-8")
2016-01-16 16:41:16 +00:00
return flat
def send_email(recipient, subject, body):
api_key = app.config["MG_API_KEY"]
data = {"from": "EasyCTF Administrator <%s>" % (app.config["ADMIN_EMAIL"]),
"to": recipient,
"subject": subject,
"text": body
}
auth = ("api", api_key)
return requests.post("https://api.mailgun.net/v3/%s/messages" % (app.config["MG_HOST"]), auth=auth, data=data)