Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Unable to load PEM file. #857

Open
XariZaru opened this issue May 8, 2024 · 0 comments
Open

ValueError: Unable to load PEM file. #857

XariZaru opened this issue May 8, 2024 · 0 comments
Labels

Comments

@XariZaru
Copy link

XariZaru commented May 8, 2024

I received the following issue:

Some algorithms requires "pip install cryptography". See https://pyjwt.readthedocs.io/en/latest/installation.html#cryptographic-dependencies-optional
Traceback (most recent call last):
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\algorithms.py", line 350, in prepare_key
    RSAPrivateKey, load_pem_private_key(key_bytes, password=None)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 494, in _handle_key_loading_error
    raise ValueError(
ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', [<OpenSSLError(code=503841036, lib=60, reason=524556, reason_text=unsupported)>])

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\msal\oauth2cli\assertion.py", line 114, in create_normal_assertion
    str_or_bytes = jwt.encode(  # PyJWT 1 returns bytes, PyJWT 2 returns str
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\api_jwt.py", line 73, in encode
    return api_jws.encode(
           ^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\api_jws.py", line 160, in encode
    key = alg_obj.prepare_key(key)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\algorithms.py", line 353, in prepare_key
    return cast(RSAPublicKey, load_pem_public_key(key_bytes))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to load PEM file. See https://cryptography.io/en/latest/faq/#why-can-t-i-import-my-pem-file for more details. MalformedFraming```

My .PEM has a format similar to below:

```python
-----BEGIN CERTIFICATE-----
fawefaojsfdojiwefoajsdkljskdfasf=wejfasidofjlksdjfalskdjfkwef
asdkfjeiorjqipofkdkdkdfowifeasdfdkfas
sadfoijweoifjaskldfjlskd
-----END CERTIFICATE-----

This is my ClientContext code

cert_credentials = {
    "tenant": TENANT,
    "client_id": CLIENT_ID,
    "thumbprint": THUMBPRINT,
    "private_key": cert_pem_content
}

print(os.getcwd())

ctx = ClientContext("https://COMPANY.sharepoint.com/").with_client_certificate(**cert_credentials)
current_web = ctx.web.get().execute_query()
print("{0}".format(current_web.url))
@vgrem vgrem added the question label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants