Skip to content

Conversation

lemoer
Copy link
Contributor

@lemoer lemoer commented Sep 5, 2025

Details

As described in issue #15747, the authentication with a JWT for the flow executor was broken. This PR fixes that. Furthermore, a unit test is added that catches this error.

I am happy to receive some feedback from the authentik devs. This mostly a proof of concept to show a way how this issue COULD be solved. If another solution is preferred, we could also go for that.


Checklist

  • Local tests pass (ak test authentik/)
  • The code has been formatted (make lint-fix)

@lemoer lemoer requested a review from a team as a code owner September 5, 2025 19:40
Copy link

netlify bot commented Sep 5, 2025

Deploy Preview for authentik-docs canceled.

Name Link
🔨 Latest commit 65801b0
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/68bb42b8e300360008c5e14a

Copy link

netlify bot commented Sep 5, 2025

Deploy Preview for authentik-storybook canceled.

Name Link
🔨 Latest commit 65801b0
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/68bb42b81f7cc30007c6c196

Copy link

netlify bot commented Sep 5, 2025

Deploy Preview for authentik-integrations canceled.

Name Link
🔨 Latest commit 65801b0
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/68bb42b8c602bd00097dfe94

@lemoer lemoer force-pushed the pr_hotfix_flow_executor_jwt branch 2 times, most recently from ac784cc to 377e77c Compare September 5, 2025 19:43
Copy link

codecov bot commented Sep 5, 2025

Codecov Report

❌ Patch coverage is 88.33333% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.73%. Comparing base (baeb892) to head (65801b0).
⚠️ Report is 168 commits behind head on main.

Files with missing lines Patch % Lines
authentik/flows/views/executor.py 76.92% 6 Missing ⚠️
authentik/rbac/middleware.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16646      +/-   ##
==========================================
- Coverage   92.78%   92.73%   -0.06%     
==========================================
  Files         838      838              
  Lines       45290    45401     +111     
==========================================
+ Hits        42024    42101      +77     
- Misses       3266     3300      +34     
Flag Coverage Δ
e2e 46.47% <20.00%> (-0.13%) ⬇️
integration 23.43% <5.00%> (-0.10%) ⬇️
unit 90.93% <88.33%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lemoer lemoer force-pushed the pr_hotfix_flow_executor_jwt branch 2 times, most recently from 3f19d31 to 3fbd5ab Compare September 5, 2025 20:03
With this commit, we move the session authentication logic from
the dispatch() method to perform_authentication() method, which
is now also overridden from the APIView parent class.

The major difference here is the order in which things are called
now.

The most important point is that the django request object is
wrapped by the django rest framework in APIView.dispatch() by
calling APIView.initialize_request(). This method returns a
wrapped version of the request (of type rest_framework.Request).
This wrapper class implicitly performs the authentication
from the authentication_classes, when request.user is accessed.

Before this commit, the original django request object
was passed to FlowPlanner._check_authentication() before
it was wrapped into a rest_framework.Request, which led
to the authentication_classes not been recognized.

The call chain was:
    FlowExecutorView.dispatch()
      FlowExecutorView._initiate_plan()
        FlowPlanner.plan()
          FlowPlanner._check_authentication()
      APIView.dispatch()
    APIView.initialize_request()

With this commit, the flow session logic is moved to
FlowExecutorView.perform_authentication() such that
the wrapped request instance is properly passed down
to FlowPlanner._check_authentication()

The new call chain is then:
    FlowExecutorView.dispatch()
      APIView.dispatch()
        APIView.initialize_request()
        APIView.initial()
          FlowExecutorView.perform_authentication()
            FlowExecutorView._initiate_plan()
              FlowPlanner.plan()
                FlowPlanner._check_authentication()

Since we can not return HttpResponses from
FlowExecutorView.perform_authentication() some exception
handlers remain in the dispatch() method. Additionally,
two new exceptions are introduced which are raised
in perform_authentication() and caught in dispatch() to
be transformed into HttpResponses.

Fixes goauthentik#15747
But otherwise, I got random errors that request doesn't have the property
request_id. Somebody with more insight should figure out why this
happens and how to handle it properly.
@lemoer lemoer force-pushed the pr_hotfix_flow_executor_jwt branch from 3fbd5ab to 65801b0 Compare September 5, 2025 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant