当前位置: 首页>>代码示例>>Python>>正文


Python Signer.validate方法代码示例

本文整理汇总了Python中itsdangerous.Signer.validate方法的典型用法代码示例。如果您正苦于以下问题:Python Signer.validate方法的具体用法?Python Signer.validate怎么用?Python Signer.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在itsdangerous.Signer的用法示例。


在下文中一共展示了Signer.validate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: User

# 需要导入模块: from itsdangerous import Signer [as 别名]
# 或者: from itsdangerous.Signer import validate [as 别名]
class User():
    def __init__(self,secret_key):
        #initialize signer
        self.s = Signer(secret_key)
        

    def deserialize(self,request):
        #it runs in framework BEFORE_REQUEST function
        self.cookies = request.cookies
        #check stat in cookie and verify it
        stat_str = str(self.cookies.get('stat'))
        stat_signature = str(self.cookies.get('stat_signature'))
        stat_token = stat_str+'.'+stat_signature
        

        if stat_str=='None' or self.s.validate(stat_token)==False:
            self._create_new_stat(request)
        #if validation success
        else:
            self.stat = json.loads(stat_str)        

        #increase kolvo_visits
        self._increase_kolvo_visits()

        #add actual IP for checking log_token
        self.stat['ip'] = request.environ['REMOTE_ADDR']
        self.log_token = str(self.cookies.get('log_token'))

        #initialize DB
        self._db_init()


    def serialize(self,response):
        #it runs in framework AFTER_REQUEST function
        stat_str = json.dumps(self.stat)

        sign_str = self.s.sign(stat_str)
        last_elem = len(sign_str.split('.'))
        #because dots in IP
        self.stat_signature = sign_str.split('.')[last_elem-1]

        response.set_cookie('stat',value=stat_str)
        response.set_cookie('stat_signature',value=self.stat_signature)

        if self.log_token!='None':
            response.set_cookie('log_token',value=self.log_token)
	else:
            response.set_cookie('log_token','',expires=0)
 


        #increase visits
        return response

    def _create_new_stat(self,request):
        self.stat = {}
        

        self.stat['ip'] = request.environ['REMOTE_ADDR']
        self.stat['first_reg'] = int(time.time())
        self.stat['kolvo_visits'] = 1
        self.stat['_id'] = 'yap'+str(int(time.time()))
        self.stat['last_visit'] = int(time.time()/86400)


    def _increase_kolvo_visits(self):
        new_day = int(time.time()/86400)
        if self.stat.get('last_visit')==None:
            self.stat['last_visit'] = int(time.time()/86400)

        if int(self.stat.get('last_visit'))<new_day:
            self.stat['kolvo_visits'] = self.stat['kolvo_visits']+1
            self.stat['last_visit'] = new_day



    def check_auth(self,roles):

        if self.log_token=='None':
            return 'Error User:Not Login, Please Log IN'

        if self.s.validate(self.log_token)!=True:
            return 'Error User:Not Valid Login Token. Please, Log IN'

        
        self.session = self._get_json(self.log_token)
        if self.session['ip']!=self.stat['ip']:
            return 'Error User:Not valid IP. Please, log IN'
        
        
        if (int(time.time()) - int(self.session['ts']))>int(self.session['live_time']):
            return 'Error User:Session nor fresh, please Log IN'

        #check permission
        for each in self.session['roles']:
            if each in roles:
                return True



#.........这里部分代码省略.........
开发者ID:andrii333,项目名称:flask_user,代码行数:103,代码来源:flask_user.py


注:本文中的itsdangerous.Signer.validate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。