jpadilla / pyjwt

JSON Web Token implementation in Python
https://pyjwt.readthedocs.io
MIT License
5.1k stars 682 forks source link

Got error: Algorithm 'ES256' could not be found. Do you have cryptography installed? #941

Closed denisshvayko closed 6 months ago

denisshvayko commented 8 months ago

I'm using PyJWT to connect to the Apple API. But at the same time I get the error whether I have crypthogaphy installed. I've already completely removed and installed packages a couple of times, but it doesn't help. I can't figure out what I'm doing wrong.

{ "cryptography": { "version": "42.0.1" }, "implementation": { "name": "CPython", "version": "3.7.3" }, "platform": { "release": "4.19.0-9-amd64", "system": "Linux" }, "pyjwt": { "version": "2.8.0" } }

JamesParrott commented 8 months ago

Shortly after the release of pyjwt 2.8.0, Python 3.7 was dropped:

Add support for Python 3.12 (#910)

Otherwise save this into a test script and run it in the env where you have cryptography installed, to see if anything in the whole import block fails for you, which would select the fallback has_crypto = False in pyjwt.

import_pyjwt_cryptography_deps.py


try:
    from cryptography.exceptions import InvalidSignature
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives import hashes
    from cryptography.hazmat.primitives.asymmetric import padding
    from cryptography.hazmat.primitives.asymmetric.ec import (
        ECDSA,
        SECP256K1,
        SECP256R1,
        SECP384R1,
        SECP521R1,
        EllipticCurve,
        EllipticCurvePrivateKey,
        EllipticCurvePrivateNumbers,
        EllipticCurvePublicKey,
        EllipticCurvePublicNumbers,
    )
    from cryptography.hazmat.primitives.asymmetric.ed448 import (
        Ed448PrivateKey,
        Ed448PublicKey,
    )
    from cryptography.hazmat.primitives.asymmetric.ed25519 import (
        Ed25519PrivateKey,
        Ed25519PublicKey,
    )
    from cryptography.hazmat.primitives.asymmetric.rsa import (
        RSAPrivateKey,
        RSAPrivateNumbers,
        RSAPublicKey,
        RSAPublicNumbers,
        rsa_crt_dmp1,
        rsa_crt_dmq1,
        rsa_crt_iqmp,
        rsa_recover_prime_factors,
    )
    from cryptography.hazmat.primitives.serialization import (
        Encoding,
        NoEncryption,
        PrivateFormat,
        PublicFormat,
        load_pem_private_key,
        load_pem_public_key,
        load_ssh_public_key,
    )

    has_crypto = True
except ModuleNotFoundError:
    has_crypto = False

print(f'has_crypto: {has_crypto}')
JamesParrott commented 8 months ago

I can reproduce the same issue in Python 3.7.3, and encountered the common "Rust not available" problem with cryptography source distributions. But this was on windows, so perhaps is not the same underlying cause as yourself.

Even though Pyjwt 2.8.0 and cryptography 42.0.1 both support Python 3.7, and even though lots of py37 cryptography 42.0.1 binary wheels are provided on PyPi, these require a later version of pip (v23 works):

python -m pip install --upgrade pip

An old default version Python 3.7 pip (v19) pulls the cryptography source distribution, which in recent years has required Rust to compile.

This fails to install cryptography entirely, which presumably you were able to do, to read off the version number? Or perhaps you did you not notice the dependency you actually wanted failed to install, as the rest of pyjwt installed successfully?

Collecting cryptography
  Downloading https://files.pythonhosted.org/packages/3f/ed/a233522ab5201b988a482cbb19ae3b63bef8ad2ad3e11fc5216b7053b2e4/cryptography-42.0.1.tar.gz (672kB)
    100% |████████████████████████████████| 675kB 1.1MB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done

...

  If you are using an outdated pip version, it is possible a prebuilt wheel is available for this package but pip is not able to install from it. Installing from the wheel would avoid the need for a Rust compiler.

  To update pip, run:

      pip install --upgrade pip

  and then retry package installation.

  If you did intend to build this package from source, try installing a Rust compiler from your system package manager and ensure it is on the PATH during installation. Alternatively, rustup (available at https://rustup.rs) is the recommended way to download and update the Rust compiler toolchain.

  This package requires Rust >=1.63.0.

  ----------------------------------------
  Failed building wheel for cryptography
github-actions[bot] commented 6 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days