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

PyJWKClient doesn't support custom SSL contexts #789

Closed
apastel opened this issue Aug 3, 2022 · 7 comments
Closed

PyJWKClient doesn't support custom SSL contexts #789

apastel opened this issue Aug 3, 2022 · 7 comments
Labels
stale Issues without activity for more than 60 days

Comments

@apastel
Copy link

apastel commented Aug 3, 2022

PyJWKClient doesn't support custom SSL contexts when calling fetch_data() to get the JWK set.

Expected Result

get_signing_key_from_jwt() could accept a SSLContext as a parameter to support authorization servers that may require custom SSL configurations, for example a server in a test environment that uses self-signed certs or requires a custom CA bundle.

Actual Result

For example, attempting get_signing_key_from_jwt() in a test environment that uses self-signed certs raises urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)>

Monkeypatched Solution

from jwt import PyJWKClient

def get_signing_key(encoded_jwt):
    PyJWKClient.fetch_data = fetch_data_ssl_verify
    jwk_client = PyJWKClient("https://acme.auth.com")
    signing_key = jwk_client.get_signing_key_from_jwt(encoded_jwt)
    return signing_key.key

def fetch_data_ssl_verify(self):
    import urllib.request
    import json
    import ssl

    ctx = ssl.create_default_context()
    ctx.load_verify_locations(cafile="/opt/certs/rootCA.pem")
    with urllib.request.urlopen(self.uri, context=ctx) as response:
        return json.load(response)

get_signing_key(encoded_jwt)

As shown, it's possible to pass a context to urllib.request.urlopen to allow specifying a custom SSLContext to enable working with servers that might be in a development environment or otherwise non-conforming to the default SSL options.

@apastel apastel changed the title PwJWKClient doesn't support custom SSL contexts PyJWKClient doesn't support custom SSL contexts Aug 3, 2022
@github-actions
Copy link

github-actions bot commented Oct 3, 2022

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

@github-actions github-actions bot added the stale Issues without activity for more than 60 days label Oct 3, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2022
@ChrisMeeusen
Copy link

Was this ever implemented? I"m running into this same issue because my corporate FW is doing SSL decryption. Otherwise I can look to another library.

@apastel
Copy link
Author

apastel commented Nov 17, 2022

@ChrisMeeusen Not that I know of. I'm not sure why it didn't get a response. I'm currently using my monkeypatch solution above. Maybe they ignored it because there's that workaround.

@ChrisMeeusen
Copy link

@ChrisMeeusen Not that I know of. I'm not sure why it didn't get a response. I'm currently using my monkeypatch solution above. Maybe they ignored it because there's that workaround.

Thanks for the response, when you say you're using the monkeypatch solution does that mean you basically pulled down all the source code for this library and build it along side your application? There is no way to just update that single file right? Sorry I'm pretty new to Python.

@apastel
Copy link
Author

apastel commented Nov 17, 2022

@ChrisMeeusen Thankfully no, you don't have to re-build the library. In Python you can essentially replace a library function by redefining it. So the code that I've shown above is all that's needed.

The top two answers here are a good reference: https://stackoverflow.com/questions/5626193/what-is-monkey-patching

@Ilnur786
Copy link

Ilnur786 commented Nov 7, 2023

I met the same issue and found that PyJWKClient receives "ssl_context" parameter.

ssl_ctx = ssl.create_default_context()
ssl_ctx.load_verify_locations(cafile="/opt/certs/rootCA.pem")

PyJWKClient("https://acme.auth.com", ssl_context=ssl_ctx)

That solves this problem

@apastel
Copy link
Author

apastel commented Nov 7, 2023

Ah, excellent. Looks like this feature was added in this merge request and then released in 2.8.0. Thanks for pointing that out!
#891

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issues without activity for more than 60 days
Projects
None yet
Development

No branches or pull requests

3 participants