jupyterhub / oauthenticator

OAuth + JupyterHub Authenticator = OAuthenticator
https://oauthenticator.readthedocs.io
BSD 3-Clause "New" or "Revised" License
414 stars 366 forks source link

[OpenShift] Fix fetching of default openshift_auth_api_url #694

Closed consideRatio closed 1 year ago

consideRatio commented 1 year ago

Fixes #693, a regression in oauthenticator v16 introduced by replacing requests to make a sync web request with tornado's HTTPClient. With it, we can re-use tornado configured OAuthenticator.http_request_kwargs instead of configuring something similar for requests.

This code was extracted and tested to work from a ipython prompt locally with adjustments like this, and also by running a jupyterhub. The @default function is called when a user signs in for the first time, not before tornado's event loop has started.

import json
import concurrent.futures
from tornado.httpclient import HTTPClient, HTTPRequest

auth_info_url = f"https://demo.c2id.com/.well-known/oauth-authorization-server"

def fetch_auth_info():
    client = HTTPClient()
    req = HTTPRequest(auth_info_url)
    resp = client.fetch(req)
    resp_json = json.loads(resp.body.decode("utf8", "replace"))
    return resp_json

with concurrent.futures.ThreadPoolExecutor() as executor:
    future = executor.submit(fetch_auth_info)
    return_value = future.result()
    print(return_value.get("issuer"))

If there is a smoother way to use the existing async OAuthenticator.httpfetch function from this sync context, doing something like asyncio.run, we should probably do that - but for example asyncio.run doesn't end up working as HTTPClient conflicts with the current event loop.

consideRatio commented 1 year ago

Thank you @manics @minrk !!