authlib icon indicating copy to clipboard operation
authlib copied to clipboard

Starlette integration OIDC issues (with regard to response_mode and response_type.)

Open nnhanndakara opened this issue 1 year ago • 6 comments

Describe the bug

The Starlette integration's OAuth2 code (at integrations/starlette_client) currently has a number of issues:

  1. StarletteOAuth2App.authorize_access_token as currently written isn't capable of handling an OIDC callback when response_mode is set to form_post. Ignoring 2 below for a moment, the fix for this is pretty easy, and something like this should suffice (in the spirit of the Flask integration):

    if request.method == "GET":
        params = {
            "code": request.query_params.get("code"),
            "state": request.query_params.get("state"),
        }
    else:
        async with request.form() as form:
            params = {
                "code": form.get("code"),
                "state": form.get("state"),
            }
    
  2. In my testing so far, commit a53173e2e4edd09e4e25e82432050e6af11e6873 breaks on any callback where response_type does not result in code in the callback. In my case, I'm asking only for id_token, so code should not be present (only id_token and state.) Raising on code being missing in this case seems like an error, if I'm not mistaken?

  3. Additionally, I'm not asking for an access token at all (which would require token in response_type, I think?) I've not configured an access token endpoint for this reason. I don't think the service I'm authenticating against even supports one, as all I need is the id_token. Should requesting a token be optional in this case?

Error Stacks

I'll have to back out my changes to get the first one, but for number 3 it's:

  File "/Volumes/work_workspace/Projects/aacc-lti_forms/src/lti_forms/app.py", line 171, in launch                                                                           
    token = await client.authorize_access_token(request)                                                                                                                     
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                     
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/authlib/integrations/starlette_client/apps.py", line 95, in authorize_access_token
    token = await self.fetch_access_token(**params, **kwargs)                                                                                                                
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/authlib/integrations/base_client/async_app.py", line 133, in fetch_access_token   
    token = await client.fetch_token(token_endpoint, **params)                                                                                                               
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                               
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 163, in _fetch_token    
    resp = await self.post(                                                                                                                                                  
           ^^^^^^^^^^^^^^^^                                                                                                                                                  
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/httpx/_client.py", line 1859, in post                                             
    return await self.request(                                                                                                                                               
           ^^^^^^^^^^^^^^^^^^^                                                                                                                                               
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 119, in request         
    return await super().request(method, url, auth=auth, **kwargs)                                                                                                           
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                           
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/httpx/_client.py", line 1527, in request                                          
    request = self.build_request(                                                                                                                                            
              ^^^^^^^^^^^^^^^^^^^                                                                                                                                            
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/httpx/_client.py", line 366, in build_request                                     
    url = self._merge_url(url)                                                                                                                                               
          ^^^^^^^^^^^^^^^^^^^^                                                                                                                                               
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/httpx/_client.py", line 396, in _merge_url                                        
    merge_url = URL(url)                                                                                                                                                     
                ^^^^^^^^                                                                                                                                                     
  File "/Volumes/work_workspace/Projects/aacc-lti_forms/.venv/lib/python3.12/site-packages/httpx/_urls.py", line 121, in __init__                                            
    raise TypeError(                                                                                                                                                         
TypeError: Invalid type for url.  Expected str or httpx.URL, got <class 'NoneType'>: None                                                                                    

To Reproduce

I don't have a small example of this as I just encountered it, but I essentially followed the examples, just adding the response_mode and response_type values requested by the application.

Expected behavior

The response is validated, and id_token parsed so that I can finish session setup.

Environment:

  • OS: macOS 26
  • Python Version: 3.12.11
  • Authlib Version: 1.6.1 (from pip) patched as described in 1 for 1 and 3. Then HEAD to check if the issues in question have been fixed, which lead to 2.

Additional context

This is for authentication against an application that uses OIDC specifically for the authentication piece. The overarching standard governing its use is LTI 1.3 (it's for higher ed.)

nnhanndakara avatar Aug 07 '25 16:08 nnhanndakara

Thanks for the report.

lepture avatar Aug 08 '25 03:08 lepture

@kaorihinata does the fix released in v1.6.2 solve you issue?

azmeuk avatar Aug 24 '25 12:08 azmeuk

@azmeuk

No, as the lack of support for POST requests (with response_mode set to form_post) in 1 has not been addressed. This will fix 2 though, and I've tested the fix for that after applying my own fix for 1 (as described in 1, based on what the Flask integration already does.) The problem is that, for my particular use case, I will never make it past line 88 in HEAD, as the custom flow I'm using relies on an existing session external to the OAuth2 flow which allows me to immediately receive the id_token when submitting a valid request to the authorization endpoint (with response_type set to id_token.) As such, calling fetch_access_token will never succeed, as I have defined neither a token request endpoint, nor do I have a code to use for requesting one.

That said, I don't know if these integrations even should support my use case, if the intention is to keep them simple as example code. What do you think?

Edit: To clarify, I have already implemented the correct flow using Authlib's underlying libraries, which is why I'm not sure supporting it is useful to the project. It is a somewhat odd case.

nnhanndakara avatar Aug 24 '25 19:08 nnhanndakara

Ok. Sorry I just saw that Hsiaoming had provided a PR and did not re-read your OP in detail. I opened #812 to align the Starlette client with the Flask and Django client regarding to POST responses, as you suggested.

Additionally, I'm not asking for an access token at all (which would require token in response_type, I think?) I've not configured an access token endpoint for this reason. I don't think the service I'm authenticating against even supports one, as all I need is the id_token. Should requesting a token be optional in this case? Edit: To clarify, I have already implemented the correct flow using Authlib's underlying libraries, which is why I'm not sure supporting it is useful to the project. It is a somewhat odd case.

🤔 I think Authlib should support such cases, where access_token are not asked by clients.

azmeuk avatar Aug 28 '25 21:08 azmeuk

That would certainly make my life much easier. There's significantly less going on in my case as the incoming request already contains the id_token, so it's just a matter of jwt.decode, .validate, translate scopes, then go on with my life, so rolling that all into a line would be nice.

nnhanndakara avatar Aug 29 '25 02:08 nnhanndakara

Points 1 and 2 are fixed (#812 and 1.6.2). Point 3 is still open: the client integrations only complete the authorization code flow.

With response_type=id_token, code id_token or id_token token, authorize_access_token only reads code and state. The id_token and access_token of the authorization response are ignored, that ID token is not validated (nonce, at_hash, c_hash), and the client still calls the token endpoint even when there is no code.

Supporting the implicit and hybrid flows (OpenID Connect Core §3.2 and §3.3) would need to:

  • read the response parameters according to the response mode;
  • validate the ID token of the authorization response with ImplicitIDToken or HybridIDToken;
  • skip the token request when there is no code;
  • for the hybrid flow, merge both responses and check that both ID tokens have the same iss and sub (§3.3.2.12).

azmeuk avatar Nov 06 '26 17:11 azmeuk