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.
flyapps/fir_ser/common/utils/token.py

75 lines
2.6 KiB

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# project: 3月
# author: liuyu
# date: 2020/3/8
import logging
import random
import string
import time
import uuid
from common.cache.storage import TokenManagerCache, RedisCacheBase
3 years ago
logger = logging.getLogger(__name__)
5 years ago
3 years ago
def make_token(release_id, time_limit=60, key='', force_new=False):
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)
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)
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
def verify_token(token, release_id, success_once=False):
3 years ago
try:
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")
if success_once:
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}")
return False
3 years ago
logger.error(f"verify_token token:{token} release_id:{release_id} failed")
return False
3 years ago
def generate_token_for_medium(medium):
if medium == 'email':
3 years ago
return generate_alphanumeric_token_of_length(32)
elif medium == 'wechat':
return 'WeChat'
else:
3 years ago
return generate_numeric_token_of_length(6)
5 years ago
3 years ago
def generate_numeric_token_of_length(length, random_str=''):
return "".join([random.choice(string.digits + random_str) for _ in range(length)])
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)])