Python JWT解码-指定与算法配对的多个秘密列表



Python JWT允许尝试使用多种算法进行解码:

jwt.decode(encoded, 'secret', algorithms=['HS512', 'HS256'])

有什么方法可以将这个算法列表与秘密列表配对使用吗?在我的用例中,有一些回退算法,但它们各自使用不同的密钥。

您总是可以使用try-catch逻辑来回退,比如:

def jwt_decode(encoded, pairs):
for alg, secret in pairs:
try:
return jwt.decode(encoded, secret, alg)
except jwt.SomeError:
pass
raise jwt.SomeError

这些SomeError应替换为正确的错误。

然后使用此jwt_decode功能回退:

jwt_decode(encoded, [('HS512', 'secret-1'), ('HS256', 'secret-2')])

最新更新