python 产生token及token验证的方法
作者:yeah,youcandoit 时间:2023-05-19 17:25:13
1、前言
最近在做微信公众号开发在进行网页授权时,微信需要用户自己在授权url中带上一个类似token的state的参数,以防止跨站攻击。
在经过再三思考之后,自己试着实现一个产生token和验证token的方案。接下就把code贴出来。希望读者指导一下。
2、产生token
原理:
通过hmac sha1 算法产生用户给定的key和token的最大过期时间戳的一个消息摘要,将这个消息摘要和最大过期时间戳通过":"拼接起来,再进行base64编码,生成最终的token
实现:
import time
import base64
import hmac
def generate_token(key, expire=3600):
r'''
@Args:
key: str (用户给定的key,需要用户保存以便之后验证token,每次产生token时的key 都可以是同一个key)
expire: int(最大有效时间,单位为s)
@Return:
state: str
'''
ts_str = str(time.time() + expire)
ts_byte = ts_str.encode("utf-8")
sha1_tshexstr = hmac.new(key.encode("utf-8"),ts_byte,'sha1').hexdigest()
token = ts_str+':'+sha1_tshexstr
b64_token = base64.urlsafe_b64encode(token.encode("utf-8"))
return b64_token.decode("utf-8")
3、验证token
原理:
将token进行base64解码,通过token得到token最大过期时间戳和消息摘要。判断token是否过期。
如没过期才将 从token中的取得最大过期时间戳进行hmac sha1 算法运算(注意这里的key要与产生token的key要相同),最后将产生的摘要与通过token取得消息摘要进行对比, 如果两个摘要相等,则token有效,否则token无效 。
实现:
import time
import base64
import hmac
def certify_token(key, token):
r'''
@Args:
key: str
token: str
@Returns:
boolean
'''
token_str = base64.urlsafe_b64decode(state).decode('utf-8')
token_list = token_str.split(':')
if len(token_list) != 2:
return False
ts_str = token_list[0]
if float(ts_str) < time.time():
# token expired
return False
known_sha1_tsstr = token_list[1]
sha1 = hmac.new(key.encode("utf-8"),ts_str.encode('utf-8'),'sha1')
calc_sha1_tsstr = sha1.hexdigest()
if calc_sha1_tsstr != known_sha1_tsstr:
# token certification failed
return False
# token certification success
return True
4、用法
key = "JD98Dskw=23njQndW9D"
# 一小时后过期
token = generate_token(key, 3600)
certify_token(key, token)
5、Note!!!
本代码只能在python3.x 中运行。
来源:http://www.cnblogs.com/whereareyoufrom/p/5753365.html
标签:python,token
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中Pyspider爬虫框架的基本使用详解
2021-04-01 06:00:31
![](https://img.aspxhome.com/file/2023/3/94053_0s.png)
python使用matplotlib显示图像失真的解决方案
2021-03-30 22:31:02
![](https://img.aspxhome.com/file/2023/6/73006_0s.jpg)
记一次因线上mysql优化器误判引起慢查询事件
2024-01-26 18:14:41
![](https://img.aspxhome.com/file/2023/3/67523_0s.png)
Mysql逗号拼接字符串的关联查询以及统计问题
2024-01-28 12:15:03
python中的数据结构比较
2023-07-20 10:58:35
oracle 数据库连接分析
2009-07-28 10:42:00
python读取txt数据的操作步骤
2022-12-27 10:36:31
![](https://img.aspxhome.com/file/2023/2/97102_0s.png)
Golang开发gRPC服务入门介绍
2024-02-14 09:56:20
![](https://img.aspxhome.com/file/2023/7/105127_0s.jpg)
python中mechanize库的简单使用示例
2021-05-22 14:32:12
Elasticsearch之倒排索引及索引操作
2023-10-28 00:38:38
举例讲解Python设计模式编程中的访问者与观察者模式
2021-06-10 06:38:16
浅谈javascript:两种注释,声明变量,定义函数
2024-04-16 09:06:06
![](https://img.aspxhome.com/file/2023/4/136674_0s.jpg)
PHP MVC框架中类的自动加载机制实例分析
2023-11-04 09:18:09
![](https://img.aspxhome.com/file/2023/4/55844_0s.png)
Perl eval函数使用实例
2022-12-21 05:25:55
属性与 @property 方法让你的python更高效
2023-02-02 08:20:18
DSN和DSN-Less两种数据库连接方式哪一种更好?
2009-10-28 18:26:00
Django REST Swagger实现指定api参数
2023-03-10 05:48:58
![](https://img.aspxhome.com/file/2023/5/76945_0s.jpg)
vue使用Print.js打印页面样式不出现的解决
2024-05-02 17:02:00
![](https://img.aspxhome.com/file/2023/9/130079_0s.png)
Python如何避免文件同名产生覆盖
2021-02-05 21:16:34
![](https://img.aspxhome.com/file/2023/4/85364_0s.png)
基于python判断字符串括号是否闭合{}[]()
2022-03-25 15:58:45