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

Is there any way to implement HTTP Only Cookies? #47

Open
gczh opened this issue Apr 4, 2021 · 9 comments
Open

Is there any way to implement HTTP Only Cookies? #47

gczh opened this issue Apr 4, 2021 · 9 comments
Labels
enhancement New feature or request

Comments

@gczh
Copy link

gczh commented Apr 4, 2021

Implemented this gem for JWT auth and it works like a charm, even with GraphQL.

However, using localstorage to store the Bearer token isn't very secure. Using HTTP Only Cookies seem to be a reasonable solution to that security issue. However, it seems that api_guard only supports the use of HTTP Headers to retrieve the tokens (including the refresh tokens)

Would it be possible to override the controllers to implement support for using HTTP Only cookies to retrieve the bearer and refresh tokens?

@gczh gczh added the doubt Need help on the usage label Apr 4, 2021
@gczh
Copy link
Author

gczh commented Apr 4, 2021

Managed to do some workarounds for now but it's a shoddy approach.

CleanShot 2021-04-04 at 12 10 05@2x

Overrode AuthenticationController and set the access + refresh tokens after it's generated and set in the headers by api_guard.

I took a look at the gem's core code further and I think we could probably add support for HTTP Only Cookie as an alternative to using Request Headers.

My suggestion is to:

  • Refactor create_token_and_set_header(resource, resource_name) to create_token_and_set_in_strategy
  • Add a create_token_and_set_in_strategy to allow users to specify which strategy they'd like: Http Only Cookie or Request Headers, or both
  • Allow users to specify the configuration in api_guard.rb initializer file. Some users might want to have both Request Headers and http only cookie support?

Would love to give this a try if you're open to it.

@Gokul595
Copy link
Owner

Gokul595 commented Apr 5, 2021

@gczh Thanks for your suggestion, it looks good.

We need to support three ways of sending tokens in response:

I am also thinking about using the access & refresh tokens from cookies (if present) for authenticating the request when Authorization header is missing in the request. It would be better if you can add this too. We are accessing the tokens in below listed places:

  • ApiGuard::JwtAuth::Authentication#authenticate_and_set_resources
  • ApiGuard::TokensController#find_refresh_token
  • lib/generators/api_guard/controllers/templates/tokens_controller.rb

Let me know if you are willing to do these changes.

@gczh
Copy link
Author

gczh commented Apr 5, 2021

Happy to tackle this if you're up to code review my changes(:

@Gokul595
Copy link
Owner

Gokul595 commented Apr 7, 2021

Yes. I can. Please proceed 👍

@Gokul595 Gokul595 added enhancement New feature or request and removed doubt Need help on the usage labels Apr 8, 2021
@gczh
Copy link
Author

gczh commented Apr 9, 2021

Yes. I can. Please proceed 👍

Will work on this!

@mdodell
Copy link

mdodell commented May 29, 2022

Is there any update on this, cc @Gokul595?

@hassanrbh
Copy link

I got refresh tokens in cookies working in my startup, I am gonna fork the repository and start working on it :)

@hassanrbh
Copy link

and also I think we need to make the http cookie implementation default ?

@hassanrbh
Copy link

#63 here is the implementation and also something is wrong in repository, needs Cognitive Complexity of 5, but I am exceeding in it, I write a custom tokenscontroller and registration controller and also authentication controller, to get yourself ready as fast as possible, is combining the three response methods, I am storing the refresh token in the cookie with ( jit as a keyword) and leaving the access token in the headers because in the frontend, I will store it, in the state using redux and access it from the state and do my logic and when you signed in or signed up, I am returning the access token in the response body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants