django - 修复 : InvalidAlgorithmError: The specified alg value is not allowed while trying to decode encoded jwt token in Python

标签 django python-3.x django-rest-framework pyjwt

我正在尝试解码从授权服务收到的 token 。问题是当我尝试解码它时,我得到 InvalidAlgorithmError: the specified alg value is not allowed .

当你看下面的图片时。我可以从 解码 token jwt.io 站点并查看有效负载。

我正在使用 PyJwt图书馆。您将在下面找到我的实现。

jwt.io 站点中的解码 token

enter image description here

实现

import jwt 

    encoded = "eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJERVZFTE9QRVIiLCJ1c2VyZnVsbG5hbWUiOiJFcmljIE0gS2FyaW1pIiwidXNlcm5hbWUiOiJlcmljIiwidXNlcmlkIjoiMjkiLCJleHAiOjE1NzM0ODE0MzIsImlzcyI6IkVyaWMiLCJhdWQiOiJSZWFkZXJzIn0.tTQckIZGYNHE667NXrxT4YwT4DNZ01u3P3b3IMFyWR4"

    key = "somekeyrequiredtodecode"

    decoded = jwt.decode(encoded,key, algorithms=['HS256'])  

完整堆栈跟踪
~/Desktop/APIs/ncc-api/env/lib/python3.6/site-packages/jwt/api_jws.py in decode(self, jwt, key, verify, algorithms, options, **kwargs)
    154         elif verify_signature:
    155             self._verify_signature(payload, signing_input, header, signature,
--> 156                                    key, algorithms)
    157 
    158         return payload

~/Desktop/APIs/ncc-api/env/lib/python3.6/site-packages/jwt/api_jws.py in _verify_signature(self, payload, signing_input, header, signature, key, algorithms)
    214 
    215         if algorithms is not None and alg not in algorithms:
--> 216             raise InvalidAlgorithmError('The specified alg value is not allowed')
    217 
    218         try:

InvalidAlgorithmError: The specified alg value is not allowed

In [7]: v = jwt.decode(key, s, algorithms=['HS256'])                                                                                                                                                          
---------------------------------------------------------------------------
InvalidAlgorithmError                     Traceback (most recent call last)
<ipython-input-7-a9465dfcaa4b> in <module>
----> 1 v = jwt.decode(key, s, algorithms=['HS256'])

~/Desktop/APIs/ncc-api/env/lib/python3.6/site-packages/jwt/api_jwt.py in decode(self, jwt, key, verify, algorithms, options, **kwargs)
     90 
     91         decoded = super(PyJWT, self).decode(
---> 92             jwt, key=key, algorithms=algorithms, options=options, **kwargs
     93         )
     94 

~/Desktop/APIs/ncc-api/env/lib/python3.6/site-packages/jwt/api_jws.py in decode(self, jwt, key, verify, algorithms, options, **kwargs)
    154         elif verify_signature:
    155             self._verify_signature(payload, signing_input, header, signature,
--> 156                                    key, algorithms)
    157 
    158         return payload

~/Desktop/APIs/ncc-api/env/lib/python3.6/site-packages/jwt/api_jws.py in _verify_signature(self, payload, signing_input, header, signature, key, algorithms)
    214 
    215         if algorithms is not None and alg not in algorithms:
--> 216             raise InvalidAlgorithmError('The specified alg value is not allowed')
    217 
    218         try:
InvalidAlgorithmError: The specified alg value is not allowed

最佳答案

在某些(不推荐)情况下,您不需要验证签名。
如果是这种情况,请使用:

jwt.decode(encoded_str, options={"verify_signature": False})
https://pyjwt.readthedocs.io/en/stable/usage.html#reading-the-claimset-without-validation

关于django - 修复 : InvalidAlgorithmError: The specified alg value is not allowed while trying to decode encoded jwt token in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58801564/

相关文章:

python - 用于基于函数的 View 的 Django Rest Framework

python - Django 中的 HttpResponseRedirect 没有重定向到页面views.py 文件

django - 如何使用 Django admin 使用 Celery 创建自定义任务

python - 我需要一个具有类继承的 Python 3 类组合结构。我该如何实现这个目标?

python - django-rest-framework 非基于 orm 的过滤

django-rest-framework - 当我在序列化器的 Perform_validation 方法中时如何获取 id 属性

javascript - Django - 使用脚本实时获取 HTML 中两个元素之间的 "total"

jQuery AJAX,json数据成为Django中的关键,无法解析数据

Django 在单独的数据库上测试给出 "OperationalError: no such table: auth_user"

python - 我怎样才能要求从元类的抽象方法中调用 super() ?