Ejemplo n.º 1
0
 def encode_auth_token(user_info):
     """
     生成认证Token
     :param user_id: int
     :param login_time: int(timestamp)
     :return: string
     """
     """
     "exp": 过期时间
     "nbf": 表示当前时间在nbf里的时间之前,则Token不被接受
     "iss": token签发者
     "aud": 接收者
     "iat": 发行时间
     """
     try:
         payload = {
             'exp':
             datetime.datetime.now() +
             datetime.timedelta(days=7, hours=0, seconds=0),
             # 'exp': datetime.datetime.now() + datetime.timedelta(seconds=10),  # test
             'iat':
             datetime.datetime.now(),
             'iss':
             'ctpbee',
             'data':
             user_info
         }
         return jwt.encode(payload, JWT_SECRET_KEY, algorithm='HS256')
     except Exception as e:
         log.error(e)
         return e
Ejemplo n.º 2
0
 def decode_auth_token(auth_token):
     """
     验证Token
     :param auth_token:
     :return: dict|string
     """
     try:
         payload = jwt.decode(auth_token,
                              JWT_SECRET_KEY,
                              leeway=datetime.timedelta(seconds=10))
         # payload = jwt.decode(auth_token, JWT_SECRET_KEY, options={'verify_exp': False})# 取消过期时间验证
         if 'data' in payload and 'userid' in payload['data']:
             return payload
         else:
             raise jwt.InvalidTokenError
     except jwt.ExpiredSignatureError:
         log.error('Token过期')
         return "token error"  # 'Token过期'
     except jwt.InvalidTokenError:
         log.error('无效Token')
         return "token error"  # '无效Token'
Ejemplo n.º 3
0
 def identify(request):
     """
     用户鉴权
     :return: {'success':True,data='',msg=''}
     """
     auth_header = request.headers.get('Authorization')
     if auth_header:
         auth_tokenArr = auth_header.split(" ")
         if not auth_tokenArr or auth_tokenArr[0] != 'JWT' or len(
                 auth_tokenArr) != 2:
             result = false_return(msg='请传递正确的验证头信息')
         else:
             auth_token = auth_tokenArr[1]
             if auth_token == G.mongo_token:
                 log.info('mongodb_token')
                 return true_return(msg='请求成功')
             payload = Auth.decode_auth_token(auth_token)
             if isinstance(payload, str):
                 result = false_return(msg=payload)
             else:
                 user = G.current_user
                 if not user or user and user['userid'] != payload['data'][
                         'userid']:
                     log.error('找不到该用户信息')
                     result = false_return(msg='token error')
                 else:
                     if user['login_time'] == payload['data']['login_time']:
                         result = true_return(msg='请求成功')
                     else:
                         log.error('Token已过期')
                         result = false_return(msg='token error')
     else:
         log.error('未提供token')
         result = false_return(msg='token error')
     return result
Ejemplo n.º 4
0
from colour_printing.default import log
from colour_printing import Switch, cprint, Back, Fore, Mode

log.info("hello world!")
# Switch.signal=False #关闭

# Switch.filter.append('SUCCESS') #过滤

log.error("hello world!")
log.success("hello world!")

log.warn("hello world!")
log.debug("hello world!")
# 颜料
# 打印色彩字符
print()
cprint('default')
cprint('hello', fore=Fore.RED)
# 或者
print()
s1 = cprint('I', fore=Fore.YELLOW, show=False)
s2 = cprint('LOVE', 'China', fore=Fore.RED, show=False)
cprint(s1, s2[0], s2[1])