|
@@ -6,13 +6,24 @@
|
|
|
@Email : zhangdongming@asj6.wecom.work
|
|
|
@Software: PyCharm
|
|
|
"""
|
|
|
+import time
|
|
|
import traceback
|
|
|
-
|
|
|
+import redis
|
|
|
import boto3
|
|
|
import botocore
|
|
|
from boto3.session import Session
|
|
|
from botocore import client
|
|
|
from botocore.exceptions import ClientError
|
|
|
+import jwt
|
|
|
+import datetime
|
|
|
+
|
|
|
+SERVER_HOST = 'backendserver.5tgle2.0001.usw1.cache.amazonaws.com' # 国外节点
|
|
|
+OAUTH_ACCESS_TOKEN_SECRET = 'a+jbgnw%@1%zy^=@dn62%'
|
|
|
+OAUTH_REFRESH_TOKEN_SECRET = 'r+jbgnw%@1%zy^=@dn62%'
|
|
|
+# access_token超时
|
|
|
+OAUTH_ACCESS_TOKEN_TIME = datetime.timedelta(days=30)
|
|
|
+# refresh_token超时
|
|
|
+OAUTH_REFRESH_TOKEN_TIME = datetime.timedelta(days=30)
|
|
|
|
|
|
|
|
|
class AmazonS3Util:
|
|
@@ -237,3 +248,236 @@ class AmazonS3Util:
|
|
|
return obj['Contents']
|
|
|
except Exception as e:
|
|
|
return []
|
|
|
+
|
|
|
+
|
|
|
+class RedisObject:
|
|
|
+
|
|
|
+ def __init__(self, db=0):
|
|
|
+ self.POOL = redis.ConnectionPool(host=SERVER_HOST, port=6379, db=db)
|
|
|
+ self.CONN = redis.Redis(connection_pool=self.POOL)
|
|
|
+
|
|
|
+ def set_data(self, key, val, expire=0):
|
|
|
+ try:
|
|
|
+ self.CONN.set(key, val)
|
|
|
+ if expire > 0:
|
|
|
+ self.CONN.expire(key, expire)
|
|
|
+ except Exception as e:
|
|
|
+ return False
|
|
|
+ else:
|
|
|
+ return True
|
|
|
+
|
|
|
+ def get_data(self, key):
|
|
|
+ try:
|
|
|
+ val = self.CONN.get(key)
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ return False
|
|
|
+ else:
|
|
|
+ if val:
|
|
|
+ return val.decode('utf-8')
|
|
|
+ else:
|
|
|
+ return False
|
|
|
+
|
|
|
+ def del_data(self, key):
|
|
|
+ try:
|
|
|
+ val = self.CONN.delete(key)
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ return False
|
|
|
+ else:
|
|
|
+ return True
|
|
|
+
|
|
|
+ def get_size(self):
|
|
|
+ return self.CONN.dbsize()
|
|
|
+
|
|
|
+ # 向列表插入数据
|
|
|
+ def rpush(self, name, val):
|
|
|
+ self.CONN.rpush(name, val)
|
|
|
+
|
|
|
+ def lpop(self, name):
|
|
|
+ val = self.CONN.lpop(name)
|
|
|
+ if val:
|
|
|
+ return val.decode('utf-8')
|
|
|
+ else:
|
|
|
+ return False
|
|
|
+
|
|
|
+ # 获取列表长度
|
|
|
+ def llen(self, name):
|
|
|
+ return self.CONN.llen(name=name)
|
|
|
+
|
|
|
+ # 获取列表所有数据
|
|
|
+ def lrange(self, name, start, end):
|
|
|
+ return self.CONN.lrange(name, start, end)
|
|
|
+
|
|
|
+ # 删除列表指定数据
|
|
|
+ def lrem(self, name, num, value):
|
|
|
+ """
|
|
|
+ num:列表方向,删除个数(0:所有)
|
|
|
+ value:删除的值
|
|
|
+ """
|
|
|
+ return self.CONN.lrem(name, num, value)
|
|
|
+
|
|
|
+ def get_ttl(self, key):
|
|
|
+ ttl = self.CONN.ttl(key)
|
|
|
+ if ttl:
|
|
|
+ return ttl
|
|
|
+ else:
|
|
|
+ return 0
|
|
|
+
|
|
|
+ def get_keys(self, key):
|
|
|
+ keys = self.CONN.keys(key)
|
|
|
+ if keys:
|
|
|
+ return keys
|
|
|
+ else:
|
|
|
+ return False
|
|
|
+
|
|
|
+ def set_ex_data(self, key, val, expire=0):
|
|
|
+ try:
|
|
|
+ self.CONN.setex(name=key, time=expire, value=val)
|
|
|
+ except Exception as e:
|
|
|
+ return False
|
|
|
+ else:
|
|
|
+ return True
|
|
|
+
|
|
|
+ def set_hash_data(self, key, kwargs):
|
|
|
+ self.CONN.hmset(key, kwargs)
|
|
|
+
|
|
|
+ def get_hash_data(self, key, file):
|
|
|
+ return self.CONN.hmget(key, file)
|
|
|
+
|
|
|
+ def get_all_hash_data(self, key):
|
|
|
+ return self.CONN.hgetall(key)
|
|
|
+
|
|
|
+
|
|
|
+class TokenObject:
|
|
|
+
|
|
|
+ def __init__(self, token=None, returntpye='currency'):
|
|
|
+ if token == 'local':
|
|
|
+ token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTg0MzUxODk2MjgyMTM4MDAxMzgwMDAiLCJsYW5nIjoiZW4iLCJ1c2VyIjoiMTM2ODAzMTc1OTYiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1ODcyNzcwNjB9.c0LV_XyxwbzUlYqMJqx7vw9f19Jv-0kGnUHuu_go-mo'
|
|
|
+ if token == 'test':
|
|
|
+ token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJleHAiOjE1Njk5OTg4OTYsInVzZXJJRCI6IjE1MTU2NDI2MjMzNzkzOTUxMzgwMDEzODAwMSIsImxhbmciOiJlbiIsIm1fY29kZSI6IjEyMzQxMzI0MzIxNCJ9.VAQtT9AbCCfXcrNj9DL5cvVasMDoI7AP8ptgU1GoMu8'
|
|
|
+ self.token = token
|
|
|
+ self.lang = None
|
|
|
+ self.userID = None
|
|
|
+ self.user = ''
|
|
|
+ self.code = 0
|
|
|
+ self.valid()
|
|
|
+ self.returntpye = returntpye
|
|
|
+
|
|
|
+ def valid(self):
|
|
|
+ if self.token is None:
|
|
|
+ self.code = 309
|
|
|
+ return
|
|
|
+ try:
|
|
|
+ self.token = self.token.replace("Bearer ", "")
|
|
|
+
|
|
|
+ res = jwt.decode(self.token, OAUTH_ACCESS_TOKEN_SECRET, algorithms='HS256')
|
|
|
+ self.userID = res.get('userID', None)
|
|
|
+ self.lang = res.get('lang', None)
|
|
|
+ self.user = res.get('user', '')
|
|
|
+ # 刷新登录时间
|
|
|
+ # if self.userID:
|
|
|
+ # print(self.user)
|
|
|
+ # redisObj = RedisObject(db=3)
|
|
|
+ # redisObj.set_data(key=self.userID, val=self.user, expire=300)
|
|
|
+
|
|
|
+ except jwt.ExpiredSignatureError as e:
|
|
|
+ self.code = 309
|
|
|
+ return
|
|
|
+ except Exception as e:
|
|
|
+ self.code = 309
|
|
|
+ return
|
|
|
+ else:
|
|
|
+ if not self.userID:
|
|
|
+ self.code = 309
|
|
|
+ return
|
|
|
+ else:
|
|
|
+ if self.userID:
|
|
|
+ self.code = 0
|
|
|
+ return res
|
|
|
+ else:
|
|
|
+ self.code = 309
|
|
|
+ return
|
|
|
+
|
|
|
+ def generate(self, data=None):
|
|
|
+ if data is None:
|
|
|
+ data = {}
|
|
|
+ try:
|
|
|
+ access_expire = int(OAUTH_ACCESS_TOKEN_TIME.total_seconds())
|
|
|
+ refresh_expire = int(OAUTH_REFRESH_TOKEN_TIME.total_seconds())
|
|
|
+ now_stamp = int(time.time())
|
|
|
+ access_data = data
|
|
|
+ refresh_data = data
|
|
|
+ access_data['exp'] = access_expire + now_stamp
|
|
|
+ refresh_data['exp'] = refresh_expire + now_stamp
|
|
|
+ access_token = jwt.encode(access_data,
|
|
|
+ OAUTH_ACCESS_TOKEN_SECRET,
|
|
|
+ algorithm='HS256')
|
|
|
+ refresh_token = jwt.encode(
|
|
|
+ refresh_data,
|
|
|
+ OAUTH_REFRESH_TOKEN_SECRET,
|
|
|
+ algorithm='HS256')
|
|
|
+
|
|
|
+ res = {
|
|
|
+ 'access_token': access_token,
|
|
|
+ 'access_expire': access_expire,
|
|
|
+ 'refresh_expire': refresh_expire,
|
|
|
+ 'refresh_token': refresh_token,
|
|
|
+ }
|
|
|
+
|
|
|
+ if self.returntpye == 'pc':
|
|
|
+ res = {
|
|
|
+ 'token': access_token,
|
|
|
+ 'access_expire': access_expire,
|
|
|
+ 'refresh_expire': refresh_expire,
|
|
|
+ 'refresh_token': refresh_token,
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ self.code = 309
|
|
|
+ print(repr(e))
|
|
|
+ else:
|
|
|
+ self.code = 0
|
|
|
+ return res
|
|
|
+
|
|
|
+ def encryption(self, data=None):
|
|
|
+ if data is None:
|
|
|
+ data = {}
|
|
|
+ try:
|
|
|
+ access_expire = int(OAUTH_ACCESS_TOKEN_TIME.total_seconds())
|
|
|
+ refresh_expire = int(OAUTH_REFRESH_TOKEN_TIME.total_seconds())
|
|
|
+ now_stamp = int(time.time())
|
|
|
+ access_data = data
|
|
|
+ refresh_data = data
|
|
|
+ access_data['exp'] = access_expire + now_stamp
|
|
|
+ refresh_data['exp'] = refresh_expire + now_stamp
|
|
|
+ access_token = jwt.encode(access_data,
|
|
|
+ OAUTH_ACCESS_TOKEN_SECRET,
|
|
|
+ algorithm='HS256')
|
|
|
+ return access_token
|
|
|
+ except Exception as e:
|
|
|
+ self.code = 309
|
|
|
+ print(repr(e))
|
|
|
+
|
|
|
+ def refresh(self):
|
|
|
+ if not self.token:
|
|
|
+ self.code = 309
|
|
|
+ return
|
|
|
+ try:
|
|
|
+ res = jwt.decode(self.token, OAUTH_REFRESH_TOKEN_SECRET, algorithms='HS256')
|
|
|
+ except jwt.ExpiredSignatureError as e:
|
|
|
+ print('过期')
|
|
|
+ print(repr(e))
|
|
|
+ self.code = 309
|
|
|
+ except Exception as e:
|
|
|
+ self.code = 309
|
|
|
+ print(repr(e))
|
|
|
+ else:
|
|
|
+ self.code = 0
|
|
|
+ userID = res.get('userID', '')
|
|
|
+ user = res.get('user', '')
|
|
|
+ lang = self.lang
|
|
|
+ self.userID = userID
|
|
|
+ self.user = user
|
|
|
+ refreshRes = self.generate(data={'userID': userID, 'lang': lang, 'user': user})
|
|
|
+ return refreshRes
|