|
20 | 20 | except ImportError: |
21 | 21 | has_ecdsa = False |
22 | 22 |
|
| 23 | +try: |
| 24 | + # fmt: off |
| 25 | + from jwt.contrib.algorithms.pycryptodome import RSAAlgorithm, ECAlgorithm # noqa: F811 |
| 26 | + # fmt: on |
| 27 | + |
| 28 | + has_pycryptodome = True |
| 29 | +except ImportError: |
| 30 | + has_pycryptodome = False |
| 31 | + |
23 | 32 |
|
24 | 33 | @pytest.mark.skipif( |
25 | 34 | not has_pycrypto, reason="Not supported without PyCrypto library" |
@@ -212,3 +221,188 @@ def test_ec_prepare_key_should_be_idempotent(self): |
212 | 221 | jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first) |
213 | 222 |
|
214 | 223 | assert jwt_pub_key_first == jwt_pub_key_second |
| 224 | + |
| 225 | + |
| 226 | +@pytest.mark.skipif( |
| 227 | + not has_pycryptodome, reason="Not supported without PyCryptodome library" |
| 228 | +) |
| 229 | +class TestPyCryptodomeAlgorithms: |
| 230 | + def test_rsa_should_parse_pem_public_key(self): |
| 231 | + algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
| 232 | + |
| 233 | + with open(key_path("testkey2_rsa.pub.pem"), "r") as pem_key: |
| 234 | + algo.prepare_key(pem_key.read()) |
| 235 | + |
| 236 | + def test_rsa_should_accept_unicode_key(self): |
| 237 | + algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
| 238 | + |
| 239 | + with open(key_path("testkey_rsa"), "r") as rsa_key: |
| 240 | + algo.prepare_key(force_unicode(rsa_key.read())) |
| 241 | + |
| 242 | + def test_rsa_should_reject_non_string_key(self): |
| 243 | + algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
| 244 | + |
| 245 | + with pytest.raises(TypeError): |
| 246 | + algo.prepare_key(None) |
| 247 | + |
| 248 | + def test_rsa_sign_should_generate_correct_signature_value(self): |
| 249 | + algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
| 250 | + |
| 251 | + jwt_message = force_bytes("Hello World!") |
| 252 | + |
| 253 | + expected_sig = base64.b64decode( |
| 254 | + force_bytes( |
| 255 | + "yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp" |
| 256 | + "10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl" |
| 257 | + "2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix" |
| 258 | + "sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX" |
| 259 | + "fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA" |
| 260 | + "APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA==" |
| 261 | + ) |
| 262 | + ) |
| 263 | + |
| 264 | + with open(key_path("testkey_rsa"), "r") as keyfile: |
| 265 | + jwt_key = algo.prepare_key(keyfile.read()) |
| 266 | + |
| 267 | + with open(key_path("testkey_rsa.pub"), "r") as keyfile: |
| 268 | + jwt_pub_key = algo.prepare_key(keyfile.read()) |
| 269 | + |
| 270 | + algo.sign(jwt_message, jwt_key) |
| 271 | + result = algo.verify(jwt_message, jwt_pub_key, expected_sig) |
| 272 | + assert result |
| 273 | + |
| 274 | + def test_rsa_verify_should_return_false_if_signature_invalid(self): |
| 275 | + algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
| 276 | + |
| 277 | + jwt_message = force_bytes("Hello World!") |
| 278 | + |
| 279 | + jwt_sig = base64.b64decode( |
| 280 | + force_bytes( |
| 281 | + "yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp" |
| 282 | + "10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl" |
| 283 | + "2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix" |
| 284 | + "sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX" |
| 285 | + "fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA" |
| 286 | + "APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA==" |
| 287 | + ) |
| 288 | + ) |
| 289 | + |
| 290 | + jwt_sig += force_bytes("123") # Signature is now invalid |
| 291 | + |
| 292 | + with open(key_path("testkey_rsa.pub"), "r") as keyfile: |
| 293 | + jwt_pub_key = algo.prepare_key(keyfile.read()) |
| 294 | + |
| 295 | + result = algo.verify(jwt_message, jwt_pub_key, jwt_sig) |
| 296 | + assert not result |
| 297 | + |
| 298 | + def test_rsa_verify_should_return_true_if_signature_valid(self): |
| 299 | + algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
| 300 | + |
| 301 | + jwt_message = force_bytes("Hello World!") |
| 302 | + |
| 303 | + jwt_sig = base64.b64decode( |
| 304 | + force_bytes( |
| 305 | + "yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp" |
| 306 | + "10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl" |
| 307 | + "2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix" |
| 308 | + "sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX" |
| 309 | + "fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA" |
| 310 | + "APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA==" |
| 311 | + ) |
| 312 | + ) |
| 313 | + |
| 314 | + with open(key_path("testkey_rsa.pub"), "r") as keyfile: |
| 315 | + jwt_pub_key = algo.prepare_key(keyfile.read()) |
| 316 | + |
| 317 | + result = algo.verify(jwt_message, jwt_pub_key, jwt_sig) |
| 318 | + assert result |
| 319 | + |
| 320 | + def test_rsa_prepare_key_should_be_idempotent(self): |
| 321 | + algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
| 322 | + |
| 323 | + with open(key_path("testkey_rsa.pub"), "r") as keyfile: |
| 324 | + jwt_pub_key_first = algo.prepare_key(keyfile.read()) |
| 325 | + jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first) |
| 326 | + |
| 327 | + assert jwt_pub_key_first == jwt_pub_key_second |
| 328 | + |
| 329 | + def test_ec_should_reject_non_string_key(self): |
| 330 | + algo = ECAlgorithm(ECAlgorithm.SHA256) |
| 331 | + |
| 332 | + with pytest.raises(TypeError): |
| 333 | + algo.prepare_key(None) |
| 334 | + |
| 335 | + def test_ec_should_accept_unicode_key(self): |
| 336 | + algo = ECAlgorithm(ECAlgorithm.SHA256) |
| 337 | + |
| 338 | + with open(key_path("testkey_ec"), "r") as ec_key: |
| 339 | + algo.prepare_key(force_unicode(ec_key.read())) |
| 340 | + |
| 341 | + def test_ec_sign_should_generate_correct_signature_value(self): |
| 342 | + algo = ECAlgorithm(ECAlgorithm.SHA256) |
| 343 | + |
| 344 | + jwt_message = force_bytes("Hello World!") |
| 345 | + |
| 346 | + expected_sig = base64.b64decode( |
| 347 | + force_bytes( |
| 348 | + "v163MdgPbEj2C/ZPRvPtLBtKaKitqZ4wg0Vs7mpoQb+i18flD" |
| 349 | + "eKb19cZn7h1E3tuP1CgthJvvIBdRKQf0OXlZA==" |
| 350 | + ) |
| 351 | + ) |
| 352 | + |
| 353 | + with open(key_path("testkey_ec"), "r") as keyfile: |
| 354 | + jwt_key = algo.prepare_key(keyfile.read()) |
| 355 | + |
| 356 | + with open(key_path("testkey_ec.pub"), "r") as keyfile: |
| 357 | + jwt_pub_key = algo.prepare_key(keyfile.read()) |
| 358 | + |
| 359 | + algo.sign(jwt_message, jwt_key) |
| 360 | + result = algo.verify(jwt_message, jwt_pub_key, expected_sig) |
| 361 | + assert result |
| 362 | + |
| 363 | + def test_ec_verify_should_return_false_if_signature_invalid(self): |
| 364 | + algo = ECAlgorithm(ECAlgorithm.SHA256) |
| 365 | + |
| 366 | + jwt_message = force_bytes("Hello World!") |
| 367 | + |
| 368 | + jwt_sig = base64.b64decode( |
| 369 | + force_bytes( |
| 370 | + "v163MdgPbEj2C/ZPRvPtLBtKaKitqZ4wg0Vs7mpoQb+i18flD" |
| 371 | + "eKb19cZn7h1E3tuP1CgthJvvIBdRKQf0OXlZA==" |
| 372 | + ) |
| 373 | + ) |
| 374 | + |
| 375 | + jwt_sig += force_bytes("123") # Signature is now invalid |
| 376 | + |
| 377 | + with open(key_path("testkey_ec.pub"), "r") as keyfile: |
| 378 | + jwt_pub_key = algo.prepare_key(keyfile.read()) |
| 379 | + |
| 380 | + result = algo.verify(jwt_message, jwt_pub_key, jwt_sig) |
| 381 | + assert not result |
| 382 | + |
| 383 | + def test_ec_verify_should_return_true_if_signature_valid(self): |
| 384 | + algo = ECAlgorithm(ECAlgorithm.SHA256) |
| 385 | + |
| 386 | + jwt_message = force_bytes("Hello World!") |
| 387 | + |
| 388 | + jwt_sig = base64.b64decode( |
| 389 | + force_bytes( |
| 390 | + "v163MdgPbEj2C/ZPRvPtLBtKaKitqZ4wg0Vs7mpoQb+i18flD" |
| 391 | + "eKb19cZn7h1E3tuP1CgthJvvIBdRKQf0OXlZA==" |
| 392 | + ) |
| 393 | + ) |
| 394 | + |
| 395 | + with open(key_path("testkey_ec.pub"), "r") as keyfile: |
| 396 | + jwt_pub_key = algo.prepare_key(keyfile.read()) |
| 397 | + |
| 398 | + result = algo.verify(jwt_message, jwt_pub_key, jwt_sig) |
| 399 | + assert result |
| 400 | + |
| 401 | + def test_ec_prepare_key_should_be_idempotent(self): |
| 402 | + algo = ECAlgorithm(ECAlgorithm.SHA256) |
| 403 | + |
| 404 | + with open(key_path("testkey_ec.pub"), "r") as keyfile: |
| 405 | + jwt_pub_key_first = algo.prepare_key(keyfile.read()) |
| 406 | + jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first) |
| 407 | + |
| 408 | + assert jwt_pub_key_first == jwt_pub_key_second |
0 commit comments