Skip to content

Conversation

@gopidesupavan
Copy link
Member

@gopidesupavan gopidesupavan commented Mar 6, 2025

closes #47080

I added the JWT to localStorage. Since the Axios interceptor already retrieves the token from localStorage in requests here https://github.com/apache/airflow/blob/main/airflow/ui/src/main.tsx#L50 , I thought it would be a good place to update localStorage after a LoginResponse. This way, subsequent redirects will consider the updated token from localStorage.

Tested:

Login with redirect to connections

image

After login redirected to connections page with Authorization header.

image


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

@gopidesupavan
Copy link
Member Author

Happy to take any other suggestion and will apply, i am not very good with front end stuff :)

Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

To successfully close the issue, we need to do the same for other AuthManagers (FabAuthManager, etc...).

Also we need to remove the front-end code that handles the token from the URL as it will not be needed anymore. (in /airflow/airflow/ui/src/layouts/BaseLayout.tsx)

@gopidesupavan
Copy link
Member Author

Nice.

To successfully close the issue, we need to do the same for other AuthManagers (FabAuthManager, etc...).

Also we need to remove the front-end code that handles the token from the URL as it will not be needed anymore. (in /airflow/airflow/ui/src/layouts/BaseLayout.tsx)

cool will update it :)

@vincbeck
Copy link
Contributor

vincbeck commented Mar 6, 2025

That is going to be hard to do it for other managers such as FAB auth manager. The front-end is controlled and generated by Flask itself. That's why I used querystring to pass the token but I agree that is a bad idea in terms of security. I do not necessarily have a solution though

@vincbeck
Copy link
Contributor

vincbeck commented Mar 6, 2025

The concern I have this solution as well is that each auth manager should then save the JWT token in local storage. I do not think this is a good idea. I'd like core Airflow to do it, not auth managers. Ideally I would like to have the JWT token in the request and then Airflow saving it. As described in #47080, instead of putting the JWT token in querystring, why dont we try in headers?

@gopidesupavan
Copy link
Member Author

The concern I have this solution as well is that each auth manager should then save the JWT token in local storage. I do not think this is a good idea. I'd like core Airflow to do it, not auth managers. Ideally I would like to have the JWT token in the request and then Airflow saving it. As described in #47080, instead of putting the JWT token in querystring, why dont we try in headers?

Yes vincent, something headers would be good idea, i am trying this option, will update.

@gopidesupavan
Copy link
Member Author

I have tried to use headers, but no luck, ended up doing with html render, If this is fine please let me know.

Copy link
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about other auth managers? How do they set the token?

Did we consider setting the token in a cookie that the "main" app (not the UI for the specific auth manager) then deletes once it's put in to local storage? That way the approach can be universal, rather than each auth manager needing to do something similar.

@gopidesupavan
Copy link
Member Author

What about other auth managers? How do they set the token?

Did we consider setting the token in a cookie that the "main" app (not the UI for the specific auth manager) then deletes once it's put in to local storage? That way the approach can be universal, rather than each auth manager needing to do something similar.

That also sounds like good plan ash, was only tried with headers. But let me check cookies as well.

@gopidesupavan gopidesupavan force-pushed the set-jwt-toke-to-localstorage branch from ca284e9 to 713dff1 Compare March 12, 2025 14:24
@gopidesupavan gopidesupavan changed the title Set JWT token to localStorage onsuccess login Set JWT token to localStorage from cookies Mar 12, 2025
@gopidesupavan
Copy link
Member Author

Have removed now search params token extraction as it not required i think anymore.

@vincbeck
Copy link
Contributor

What about other auth managers? How do they set the token?

Did we consider setting the token in a cookie that the "main" app (not the UI for the specific auth manager) then deletes once it's put in to local storage? That way the approach can be universal, rather than each auth manager needing to do something similar.

Strongly agree on this one. Auth managers should pass somehow the JWT token and then core Airflow saves it. We should not rely on auth managers to save the JWT tokens, this would create some problems in the long run

@gopidesupavan
Copy link
Member Author

What about other auth managers? How do they set the token?
Did we consider setting the token in a cookie that the "main" app (not the UI for the specific auth manager) then deletes once it's put in to local storage? That way the approach can be universal, rather than each auth manager needing to do something similar.

Strongly agree on this one. Auth managers should pass somehow the JWT token and then core Airflow saves it. We should not rely on auth managers to save the JWT tokens, this would create some problems in the long run

The auth manager sets the token in cookies, and the main app retrieves it using document.cookie and stores it in localStorage. Then, it sets the expiration date for the token cookie. Does this align the expectation ?

@ashb
Copy link
Member

ashb commented Mar 12, 2025

The auth manager sets the token in cookies, and the main app retrieves it using document.cookie and stores it in localStorage. Then, it sets the expiration date for the token cookie. Does this align the expectation ?

Ideally the auth_manager will return the token to the main app so that it can set the cookie. If that is not possible then we at least need to document the requirement that "if you write a custom auth manager, it must set cookie X"

@gopidesupavan gopidesupavan requested a review from potiuk as a code owner March 12, 2025 17:04
@vincbeck
Copy link
Contributor

I think this change fine now. except the tests failures related to k8s are unstable, even in CI its failing. will try to look.

Those errors are unrelated to this PR, you can ignore them

@gopidesupavan
Copy link
Member Author

I think this change fine now. except the tests failures related to k8s are unstable, even in CI its failing. will try to look.

Those errors are unrelated to this PR, you can ignore them

Okay cool, will wait for anyone to look, as this changes also include in front end.

@gopidesupavan gopidesupavan force-pushed the set-jwt-toke-to-localstorage branch from 8ace629 to 9fa2016 Compare March 14, 2025 10:52
@gopidesupavan gopidesupavan force-pushed the set-jwt-toke-to-localstorage branch from 9fa2016 to a03bc70 Compare March 14, 2025 12:35
Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! looks good. Thanks. Just one suggestion

gopidesupavan and others added 2 commits March 14, 2025 13:01
@gopidesupavan
Copy link
Member Author

Merging now its finally green :)

@gopidesupavan gopidesupavan merged commit 4ffb0a6 into apache:main Mar 14, 2025
89 checks passed
@vincbeck
Copy link
Contributor

Really nice job! 👏

@potiuk
Copy link
Member

potiuk commented Mar 16, 2025

Nice indeed

agupta01 pushed a commit to agupta01/airflow that referenced this pull request Mar 21, 2025
* Set JWT token to localStorage onsuccess login

* Set jwt to localstorage in fab

* Set jwt token to cookies from redirect

* Set jwt token to cookies from redirect

* Fix tests for tokenHandler

* Update auth manager documentation

* Update cookie to use secure property

* Use react-cookie

* Use react-cookie

* Rename cookie constant to COOKIE_NAME_JWT_TOKEN and fix k8s and docker tests

* Remove samesite param and update docs

* Fix auth manager docs error

* Remove samesite param in auth manager

* Update docs/apache-airflow/core-concepts/auth-manager/index.rst

Co-authored-by: Pierre Jeambrun <pierrejbrun@gmail.com>

* Fix static checks

---------

Co-authored-by: Pierre Jeambrun <pierrejbrun@gmail.com>
nailo2c pushed a commit to nailo2c/airflow that referenced this pull request Apr 4, 2025
* Set JWT token to localStorage onsuccess login

* Set jwt to localstorage in fab

* Set jwt token to cookies from redirect

* Set jwt token to cookies from redirect

* Fix tests for tokenHandler

* Update auth manager documentation

* Update cookie to use secure property

* Use react-cookie

* Use react-cookie

* Rename cookie constant to COOKIE_NAME_JWT_TOKEN and fix k8s and docker tests

* Remove samesite param and update docs

* Fix auth manager docs error

* Remove samesite param in auth manager

* Update docs/apache-airflow/core-concepts/auth-manager/index.rst

Co-authored-by: Pierre Jeambrun <pierrejbrun@gmail.com>

* Fix static checks

---------

Co-authored-by: Pierre Jeambrun <pierrejbrun@gmail.com>
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.

Explore alternatives to putting JWT token in query params

5 participants