python简单实现jwt

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python简单实现jwt相关的知识,希望对你有一定的参考价值。

参考技术A jwt应用如下图所示

1.用户请求登录服务器

2。服务器接到请求生成一个jwt-token

3.把这个jwt-token发回到前端

4.每次请求的时候带这个token和uid

5.收到jwt-token首先比较对不对,完后用secret解密后再次比较内部信息对不对,是否被更改过。

6.认证通过就可以请求别的接口返回对应的response了

优点

因为json的通用性,所以JWT是可以进行跨语言支持的,像JAVA,javascript,NodeJS,php等很多语言都可以使用。

因为有了payload部分,所以JWT可以在自身存储一些其他业务逻辑所必要的非敏感信息。

便于传输,jwt的构成非常简单,字节占用很小,所以它是非常便于传输的。

它不需要在服务端保存会话信息, 所以它易于应用的扩展

安全相关

不应该在jwt的payload部分存放敏感信息,因为该部分是客户端可解密的部分。

保护好secret私钥,该私钥非常重要。

如果可以,请使用https协议

参考:
https://blog.csdn.net/houmenghu/article/details/99181326

https://www.cnblogs.com/lowmanisbusy/p/10930856.html

https://blog.csdn.net/houmenghu/article/details/99181326

https://www.jb51.net/article/186759.htm

https://www.cnblogs.com/djfboai/p/10670714.html

JSON Web Token (JWT) Python 的实现

Docker registry 认证使用的是JWT,Go实现的,研究了一整天,这段代码帮大忙了。

 

import jwt

from cryptography.hazmat.backends import default_backend
from itsdangerous import base64_decode
from Crypto.PublicKey import RSA


secret = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIE6a1NyEFe7qCDFrvWFZiAlY1ttE5596w5dLjNSaHlKGv8AXbKg/f8yKY9fKAJ5BKoeWEkPPjpn1t9QQAZYzqH9KNOFigMU8pSaRUxjI2dDvwmu8ZH6EExY+RfrPjQGmeliK18iFzFgBtf0eH3NAW3Pf71OZZz+cuNnVtE9lrYQIDAQAB"
secretDer = base64_decode(secret)
sshrsaSecret = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCIE6a1NyEFe7qCDFrvWFZiAlY1ttE5596w5dLjNSaHl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dfX0sIm5hbWUiOiIiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhZG1pbiJ9.O7e8dkv0k-2HCjMdZFXIxLhypVyRPwIdrQsYTMwC1996wbsjIw1L3OjDSzJKXcx0U9YrVeRM4yMVlFg40uJDC-9IsKZ8nr5dl_da8SzgpAkempxpas3girST2U9uvY56m2Spp6-EFInvMSb6k4t1L49_Q7R2g0DOlKzxgQd87LY"

############### Test using PEM key (with ----- lines)
try:
    access_token_json = jwt.decode(access_token, key=secretPEM)
except Exception as e:
    print "Not working using PEM key with ----: ", e
else:
    print "It worked!"

############### Test using PEM key (without ----- lines)
try:
    access_token_json = jwt.decode(access_token, key=secret)
except Exception as e:
    print "Not working using PEM key without ----: ", e
else:
    print "It worked!"

############### Test using DER key
try:
    access_token_json = jwt.decode(access_token, key=secretDer)
except Exception as e:
    print "Not working using DER key: ", e
else:
    print "It worked!"

############### Test using DER key #2
try:
    public_key = default_backend().load_der_public_key(secretDer)
    access_token_json = jwt.decode(access_token, key=public_key)
except Exception as e:
    print "Not working using DER key #2: ", e
else:
    print "It worked!"

############### Test using SSH style key
try:
    access_token_json = jwt.decode(access_token, key=sshrsaSecret)
except Exception as e:
    print "Not working using SSH style key: ", e
else:
    print "It worked!"

############### Test using RSA numbers
class Numbers:
    pass

numbers = Numbers()
public_key = RSA.importKey(secretDer)
numbers.e = public_key.key.e
numbers.n = public_key.key.n
# yet another way to generated valid key object
public_key = default_backend().load_rsa_public_numbers(numbers)
print public_key
try:
    access_token_json = jwt.decode(access_token, key=public_key)
except Exception as e:
    print "Not working using RSA numbers: ", e
else:
    print "It worked!"
###############

 

原文地址:http://stackoverflow.com/questions/29650495/how-to-verify-a-jwt-using-python-pyjwt-with-public-key

 

其他有用的资源:

JWT的说明文档:http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#jtiDef

jwt-simple :https://www.npmjs.com/package/jwt-simple

 

以上是关于python简单实现jwt的主要内容,如果未能解决你的问题,请参考以下文章

python实现简单登陆代码

用python实现简单的调度场算法

python实现简单购物车系统(练习)

python 实现简单购物车

python实现最简单的进度条

Python实现简单的三次登录