Merge pull request #521 from javicacheiro/fix_jwcrypto

Tests break with jwcrypto>=1.3
This commit is contained in:
Samuel Mannehed 2022-05-26 15:59:39 +02:00 committed by GitHub
commit 33910d758d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -4,7 +4,7 @@
import unittest import unittest
from unittest.mock import patch, mock_open, MagicMock from unittest.mock import patch, mock_open, MagicMock
from jwcrypto import jwt from jwcrypto import jwt, jwk
from websockify.token_plugins import ReadOnlyTokenFile, JWTTokenApi, TokenRedis from websockify.token_plugins import ReadOnlyTokenFile, JWTTokenApi, TokenRedis
@ -56,7 +56,7 @@ class JWSTokenTestCase(unittest.TestCase):
def test_asymmetric_jws_token_plugin(self): def test_asymmetric_jws_token_plugin(self):
plugin = JWTTokenApi("./tests/fixtures/public.pem") plugin = JWTTokenApi("./tests/fixtures/public.pem")
key = jwt.JWK() key = jwk.JWK()
private_key = open("./tests/fixtures/private.pem", "rb").read() private_key = open("./tests/fixtures/private.pem", "rb").read()
key.import_from_pem(private_key) key.import_from_pem(private_key)
jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port"}) jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port"})
@ -71,7 +71,7 @@ class JWSTokenTestCase(unittest.TestCase):
def test_asymmetric_jws_token_plugin_with_illigal_key_exception(self): def test_asymmetric_jws_token_plugin_with_illigal_key_exception(self):
plugin = JWTTokenApi("wrong.pub") plugin = JWTTokenApi("wrong.pub")
key = jwt.JWK() key = jwk.JWK()
private_key = open("./tests/fixtures/private.pem", "rb").read() private_key = open("./tests/fixtures/private.pem", "rb").read()
key.import_from_pem(private_key) key.import_from_pem(private_key)
jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port"}) jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port"})
@ -85,7 +85,7 @@ class JWSTokenTestCase(unittest.TestCase):
def test_jwt_valid_time(self, mock_time): def test_jwt_valid_time(self, mock_time):
plugin = JWTTokenApi("./tests/fixtures/public.pem") plugin = JWTTokenApi("./tests/fixtures/public.pem")
key = jwt.JWK() key = jwk.JWK()
private_key = open("./tests/fixtures/private.pem", "rb").read() private_key = open("./tests/fixtures/private.pem", "rb").read()
key.import_from_pem(private_key) key.import_from_pem(private_key)
jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port", 'nbf': 100, 'exp': 200 }) jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port", 'nbf': 100, 'exp': 200 })
@ -102,7 +102,7 @@ class JWSTokenTestCase(unittest.TestCase):
def test_jwt_early_time(self, mock_time): def test_jwt_early_time(self, mock_time):
plugin = JWTTokenApi("./tests/fixtures/public.pem") plugin = JWTTokenApi("./tests/fixtures/public.pem")
key = jwt.JWK() key = jwk.JWK()
private_key = open("./tests/fixtures/private.pem", "rb").read() private_key = open("./tests/fixtures/private.pem", "rb").read()
key.import_from_pem(private_key) key.import_from_pem(private_key)
jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port", 'nbf': 100, 'exp': 200 }) jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port", 'nbf': 100, 'exp': 200 })
@ -117,7 +117,7 @@ class JWSTokenTestCase(unittest.TestCase):
def test_jwt_late_time(self, mock_time): def test_jwt_late_time(self, mock_time):
plugin = JWTTokenApi("./tests/fixtures/public.pem") plugin = JWTTokenApi("./tests/fixtures/public.pem")
key = jwt.JWK() key = jwk.JWK()
private_key = open("./tests/fixtures/private.pem", "rb").read() private_key = open("./tests/fixtures/private.pem", "rb").read()
key.import_from_pem(private_key) key.import_from_pem(private_key)
jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port", 'nbf': 100, 'exp': 200 }) jwt_token = jwt.JWT({"alg": "RS256"}, {'host': "remote_host", 'port': "remote_port", 'nbf': 100, 'exp': 200 })
@ -132,7 +132,7 @@ class JWSTokenTestCase(unittest.TestCase):
plugin = JWTTokenApi("./tests/fixtures/symmetric.key") plugin = JWTTokenApi("./tests/fixtures/symmetric.key")
secret = open("./tests/fixtures/symmetric.key").read() secret = open("./tests/fixtures/symmetric.key").read()
key = jwt.JWK() key = jwk.JWK()
key.import_key(kty="oct",k=secret) key.import_key(kty="oct",k=secret)
jwt_token = jwt.JWT({"alg": "HS256"}, {'host': "remote_host", 'port': "remote_port"}) jwt_token = jwt.JWT({"alg": "HS256"}, {'host': "remote_host", 'port': "remote_port"})
jwt_token.make_signed_token(key) jwt_token.make_signed_token(key)
@ -147,7 +147,7 @@ class JWSTokenTestCase(unittest.TestCase):
plugin = JWTTokenApi("wrong_sauce") plugin = JWTTokenApi("wrong_sauce")
secret = open("./tests/fixtures/symmetric.key").read() secret = open("./tests/fixtures/symmetric.key").read()
key = jwt.JWK() key = jwk.JWK()
key.import_key(kty="oct",k=secret) key.import_key(kty="oct",k=secret)
jwt_token = jwt.JWT({"alg": "HS256"}, {'host': "remote_host", 'port': "remote_port"}) jwt_token = jwt.JWT({"alg": "HS256"}, {'host': "remote_host", 'port': "remote_port"})
jwt_token.make_signed_token(key) jwt_token.make_signed_token(key)
@ -159,8 +159,8 @@ class JWSTokenTestCase(unittest.TestCase):
def test_asymmetric_jwe_token_plugin(self): def test_asymmetric_jwe_token_plugin(self):
plugin = JWTTokenApi("./tests/fixtures/private.pem") plugin = JWTTokenApi("./tests/fixtures/private.pem")
private_key = jwt.JWK() private_key = jwk.JWK()
public_key = jwt.JWK() public_key = jwk.JWK()
private_key_data = open("./tests/fixtures/private.pem", "rb").read() private_key_data = open("./tests/fixtures/private.pem", "rb").read()
public_key_data = open("./tests/fixtures/public.pem", "rb").read() public_key_data = open("./tests/fixtures/public.pem", "rb").read()
private_key.import_from_pem(private_key_data) private_key.import_from_pem(private_key_data)

View File

@ -103,10 +103,10 @@ class JWTTokenApi(BasePlugin):
def lookup(self, token): def lookup(self, token):
try: try:
from jwcrypto import jwt from jwcrypto import jwt, jwk
import json import json
key = jwt.JWK() key = jwk.JWK()
try: try:
with open(self.source, 'rb') as key_file: with open(self.source, 'rb') as key_file: