| 294 | |
| 295 | @pytest.mark.skipif(RSAKey is None, reason="RSA is not available") |
| 296 | class TestRSA: |
| 297 | def test_jwk_set(self, jwk_set): |
| 298 | # Would raise a JWSError if validation failed. |
| 299 | payload = jws.verify(google_id_token, jwk_set, ALGORITHMS.RS256) |
| 300 | iss = json.loads(payload.decode("utf-8"))["iss"] |
| 301 | assert iss == "https://accounts.google.com" |
| 302 | |
| 303 | def test_jwk_set_failure(self, jwk_set): |
| 304 | # Remove the key that was used to sign this token. |
| 305 | del jwk_set["keys"][1] |
| 306 | with pytest.raises(JWSError): |
| 307 | payload = jws.verify(google_id_token, jwk_set, ALGORITHMS.RS256) # noqa: F841 |
| 308 | |
| 309 | def test_RSA256(self, payload): |
| 310 | token = jws.sign(payload, rsa_private_key, algorithm=ALGORITHMS.RS256) |
| 311 | assert jws.verify(token, rsa_public_key, ALGORITHMS.RS256) == payload |
| 312 | |
| 313 | def test_RSA384(self, payload): |
| 314 | token = jws.sign(payload, rsa_private_key, algorithm=ALGORITHMS.RS384) |
| 315 | assert jws.verify(token, rsa_public_key, ALGORITHMS.RS384) == payload |
| 316 | |
| 317 | def test_RSA512(self, payload): |
| 318 | token = jws.sign(payload, rsa_private_key, algorithm=ALGORITHMS.RS512) |
| 319 | assert jws.verify(token, rsa_public_key, ALGORITHMS.RS512) == payload |
| 320 | |
| 321 | def test_wrong_alg(self, payload): |
| 322 | token = jws.sign(payload, rsa_private_key, algorithm=ALGORITHMS.RS256) |
| 323 | with pytest.raises(JWSError): |
| 324 | jws.verify(token, rsa_public_key, ALGORITHMS.RS384) |
| 325 | |
| 326 | def test_wrong_key(self, payload): |
| 327 | token = jws.sign(payload, rsa_private_key, algorithm=ALGORITHMS.RS256) |
| 328 | with pytest.raises(JWSError): |
| 329 | jws.verify(token, rsa_public_key, ALGORITHMS.HS256) |
| 330 | |
| 331 | def test_private_verify_raises_warning(self, payload): |
| 332 | token = jws.sign(payload, rsa_private_key, algorithm="RS256") |
| 333 | |
| 334 | # verify with public |
| 335 | jws.verify(token, rsa_public_key, algorithms="RS256") |
| 336 | |
| 337 | with warnings.catch_warnings(record=True) as w: |
| 338 | # verify with private raises warning |
| 339 | jws.verify(token, rsa_private_key, algorithms="RS256") |
| 340 | |
| 341 | assert ("Attempting to verify a message with a private key. " "This is not recommended.") == str( |
| 342 | w[-1].message |
| 343 | ) |
| 344 | |
| 345 | |
| 346 | ec_private_key = """-----BEGIN EC PRIVATE KEY----- |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…