You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.6 KiB
75 lines
2.6 KiB
5 years ago
|
#!/usr/bin/env python
|
||
|
# -*- coding:utf-8 -*-
|
||
|
# project: 3月
|
||
|
# author: liuyu
|
||
|
# date: 2020/3/8
|
||
3 years ago
|
import logging
|
||
5 years ago
|
import random
|
||
3 years ago
|
import string
|
||
5 years ago
|
import time
|
||
3 years ago
|
import uuid
|
||
|
|
||
3 years ago
|
from common.cache.storage import TokenManagerCache, RedisCacheBase
|
||
5 years ago
|
|
||
3 years ago
|
logger = logging.getLogger(__name__)
|
||
5 years ago
|
|
||
5 years ago
|
|
||
3 years ago
|
def make_token(release_id, time_limit=60, key='', force_new=False):
|
||
3 years ago
|
token_cache = TokenManagerCache(key, release_id)
|
||
|
token_key, token = token_cache.get_storage_key_and_cache()
|
||
3 years ago
|
if token and not force_new:
|
||
|
logger.debug(
|
||
|
f"make_token cache exists get token:{token} release_id:{release_id} force_new:{force_new} token_key:{token_key}")
|
||
|
return token
|
||
|
else:
|
||
|
random_str = uuid.uuid1().__str__().split("-")[0:-1]
|
||
|
user_ran_str = uuid.uuid5(uuid.NAMESPACE_DNS, release_id).__str__().split("-")
|
||
|
user_ran_str.extend(random_str)
|
||
|
token = "".join(user_ran_str)
|
||
3 years ago
|
token_cache.set_storage_cache({
|
||
|
"atime": time.time() + time_limit,
|
||
|
"data": release_id
|
||
|
}, time_limit)
|
||
|
RedisCacheBase(token).set_storage_cache({
|
||
3 years ago
|
"atime": time.time() + time_limit,
|
||
|
"data": release_id
|
||
|
}, time_limit)
|
||
3 years ago
|
token_cache.set_storage_cache(token, time_limit - 1)
|
||
3 years ago
|
logger.debug(
|
||
|
f"make_token cache not exists get token:{token} release_id:{release_id} force_new:{force_new} token_key:{token_key}")
|
||
|
return token
|
||
5 years ago
|
|
||
5 years ago
|
|
||
3 years ago
|
def verify_token(token, release_id, success_once=False):
|
||
3 years ago
|
try:
|
||
3 years ago
|
token_cache = RedisCacheBase(token)
|
||
|
token, values = token_cache.get_storage_key_and_cache()
|
||
3 years ago
|
if values and release_id == values.get("data", None):
|
||
|
logger.debug(f"verify_token token:{token} release_id:{release_id} success")
|
||
3 years ago
|
if success_once:
|
||
3 years ago
|
token_cache.del_storage_cache()
|
||
3 years ago
|
return True
|
||
|
except Exception as e:
|
||
|
logger.error(f"verify_token token:{token} release_id:{release_id} failed Exception:{e}")
|
||
5 years ago
|
return False
|
||
3 years ago
|
logger.error(f"verify_token token:{token} release_id:{release_id} failed")
|
||
|
return False
|
||
5 years ago
|
|
||
|
|
||
3 years ago
|
def generate_token_for_medium(medium):
|
||
5 years ago
|
if medium == 'email':
|
||
3 years ago
|
return generate_alphanumeric_token_of_length(32)
|
||
5 years ago
|
elif medium == 'wechat':
|
||
|
return 'WeChat'
|
||
|
else:
|
||
3 years ago
|
return generate_numeric_token_of_length(6)
|
||
5 years ago
|
|
||
5 years ago
|
|
||
3 years ago
|
def generate_numeric_token_of_length(length, random_str=''):
|
||
4 years ago
|
return "".join([random.choice(string.digits + random_str) for _ in range(length)])
|
||
5 years ago
|
|
||
5 years ago
|
|
||
3 years ago
|
def generate_alphanumeric_token_of_length(length):
|
||
5 years ago
|
return "".join(
|
||
|
[random.choice(string.digits + string.ascii_lowercase + string.ascii_uppercase) for _ in range(length)])
|