http权限代码优化
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import logging
|
import logging
|
||||||
import datetime
|
from datetime import datetime, timedelta
|
||||||
import time
|
|
||||||
import hashlib
|
import hashlib
|
||||||
from odoo import models
|
from odoo import models
|
||||||
from odoo.http import request
|
from odoo.http import request
|
||||||
@@ -10,8 +9,6 @@ __author__ = 'jinling.yang'
|
|||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class AuthenticationError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class AuthenticationError(Exception):
|
class AuthenticationError(Exception):
|
||||||
pass
|
pass
|
||||||
@@ -29,16 +26,18 @@ class Http(models.AbstractModel):
|
|||||||
# 查询密钥
|
# 查询密钥
|
||||||
factory_secret = request.env['res.partner'].sudo().search(
|
factory_secret = request.env['res.partner'].sudo().search(
|
||||||
[('sf_token', '=', datas['HTTP_TOKEN'])], limit=1)
|
[('sf_token', '=', datas['HTTP_TOKEN'])], limit=1)
|
||||||
logging.info('factory_secret:%s' % factory_secret)
|
|
||||||
if not factory_secret:
|
if not factory_secret:
|
||||||
raise AuthenticationError('无效的token')
|
raise AuthenticationError('无效的token')
|
||||||
timestamp_str = int(time.time())
|
|
||||||
# 设置API接口请求时间,不能超过5秒
|
# 设置API接口请求时间,不能超过5秒
|
||||||
deltime = datetime.timedelta(seconds=5)
|
# deltime = datetime.timedelta(seconds=30)
|
||||||
# if abs(int(datas['HTTP_TIMESTAMP'])-timestamp_str) > deltime.seconds:
|
# if abs(int(datas['HTTP_TIMESTAMP']) - timestamp_str) > deltime.seconds:
|
||||||
# raise AuthenticationError('请求已过期')
|
# raise AuthenticationError('请求已过期')
|
||||||
# 获得sha1_str加密字符串
|
|
||||||
post_time = int(datas['HTTP_TIMESTAMP'])
|
post_time = int(datas['HTTP_TIMESTAMP'])
|
||||||
|
datetime_post = datetime.fromtimestamp(post_time)
|
||||||
|
datetime_now = datetime.now().replace(microsecond=0)
|
||||||
|
datetime_del = datetime_now + timedelta(seconds=5)
|
||||||
|
if datetime_post > datetime_del:
|
||||||
|
raise AuthenticationError('请求已过期')
|
||||||
check_str = '%s%s%s' % (datas['HTTP_TOKEN'], post_time, factory_secret.sf_secret_key)
|
check_str = '%s%s%s' % (datas['HTTP_TOKEN'], post_time, factory_secret.sf_secret_key)
|
||||||
check_sf_str = hashlib.sha1(check_str.encode('utf-8')).hexdigest()
|
check_sf_str = hashlib.sha1(check_str.encode('utf-8')).hexdigest()
|
||||||
if check_sf_str != datas['HTTP_CHECKSTR']:
|
if check_sf_str != datas['HTTP_CHECKSTR']:
|
||||||
|
|||||||
Reference in New Issue
Block a user