-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Token based authentication with no sessions #183
Comments
@c0mrade - thanks for the detailed report. The session is used to persist information between a user leaving to authenticate on an external provider's site (github, facebook, etc.) and returning (to the API) with the response token and user account info. So I think sessions may be required to use OmniAuth. Someone please correct me if I'm wrong. |
I just found a stackoverflow post that seems to confirm this, and it offers a possible workaround. |
yeah if the sessions are needed for that only, then sounds ok. I can just continue with middlewares I was using before, I don't like the solution from stackoverflow. I guess then for people requiring more info about the user for remote site, better to set the session store to db. Thanks for your time, really appreciate it. I'll close this one. |
I'm in the same situation too- I only use rails-api and consume things in a stateless manner. I have inserted the middleware for now, but it'd be preferable if you could just not use session with omniauth. |
@kurtisnelson - please post an issue with omniauth. |
It worked for me using
it's ugly and I don't like it but it works |
Doing this at ApplicationController:
And adding this in application.rb:
As @betoharres says, it's ugly but works for now. |
How can I make sure that a cookie is never sent in a response's headers when doing this? I'm developing a React Native app and making requests using the fetch API. Apparently RN is automatically sending a cookie in any request as soon as he gets one, which is of course a problem on their side. Pushing authentication requests with a cookie makes the API throw 401s, so it is a problem to give out cookies in the first place and I'd like to solve it at the API level. Any idea on how I can make Omniauth happy while still not distributing cookies around? |
I had problems with all the solutions detailed in this issue. The problem was as follows:
What worked for me was following the instructions from https://github.com/omniauth/omniauth#integrating-omniauth-into-your-rails-api: Into
|
I've read through the thread but I'm not sure I get it. So the conclusion is just “it's impossible to have token based authentication with no sessions”, right? The only solution is “don't do it”? |
@FranklinYu tokens based auth with no sessions works out of the box. But if going to use OAuth and you are lazy enough to write own solution (or no one pay you :) ), you have to enable sessions, because of https://github.com/omniauth/omniauth gem |
@mpugach Thank you. That conclusion aligns with omniauth/omniauth#899; I'm just disappointed that no one ever try to wrap their “session-less OAuth” in a gem similar to OmniAuth. |
@FranklinYu they both are open to pull requests 😉 |
@FranklinYu I think it's ok in open source. Many people implement their features by themself (if no ready to use realizations exists), and only somewhen later one of them force it to be open, supporting it. Actually, there's a lot of things to think out and to reconsider to say "It's ready, it has nice api interface. I can easily write documentation for it, with examples. I have time for it. And when I'll go for a walk, I will not be beaten near my home by someone coz my code wasn't good enough". So we still don't see such a gem :D |
I have a realization based on this explanation. This should greatly help you guys (as this was for me). Omniauth gem is a thing of past days, now (with |
@jerrygreen I have come across two projects that need such implementation. I plan to read the OmniAuth implementation, and seriously decide whether I have the time to extract some logic into a gem (if OmniAuth doesn't like this “non-session configuration idea”). |
@FranklinYu actually this OmniAuth gem needs a lot of changes for this case. Too many changes. It's designed to be used with typical rails website. With rendering html, etc. Not for |
@jerrygreen, do you have an update on the current state of this? I am, like many others here, converting my app from an HTML generating app to an API only thing... I want to continue using facebook login, etc - but from what I gather from this thread the omniauth gem needs some serious changes? Is there a drop-in replacement or do I need to do some manual work? |
@emilevictor @gibo appears to be working on this here: Maybe you two could finish up the work together if they're having any issues? We'd welcome your testing/approval of existing pull requests, or new ones! |
@emilevictor as far as I know there's still no "ready to use" implementations (handled by single gem). Only custom ones. Omniauth still need changes. btw, if you just want to make things work, look again at one of my previous messages The link in there might help you - https://stackoverflow.com/questions/19989391/authenticate-user-using-omniauth-and-facebook-for-a-rails-api/30419460#30419460 This also gives you a better understanding of how all of these facebook login/sharing things are done |
The point is, OAuth protocol implicitly rely on session (because it needs redirect). So if you want to OAuth, you have to enable session. But if you only need Google and Facebook (not even Twitter!), then it's possible to do pure API with POST request (instead of redirect with GET), but then it is no longer OAuth (so no OmniAuth any more). I believe Google call it Auth2, but I doubt that there is even a well-defined, stable protocol for it. According to my own brief research it is just some magic behind Google/Facebook SDK, and even they don't share the communication pattern with each other. I really wish this new pattern become stable and standardized and widely accepted by more identity providers, now that SPA becomes increasingly popular with the help of modern front-end frameworks. Most websites only need authentication (log in with Google) instead of authorization (get Google+ status). As always, correct me if I'm wrong. |
Ah I see, this appears to be a much larger issue! Yeah it would be really cool if we could roll support of that into this gem :). Or have another gem that this gem relies on. |
Thanks @FranklinYu for explaining this so well. This issue comes up a lot - maybe we should add a note to the readme? |
@lynndylanhurley You're welcome! Actually I myself is new to this field, but the Auth2 is really attractive, as it is possible to avoids CSRF (Cross Site Request Forgery) at all (so we don't need the I'm not sure what to add to README... Telling users that OAuth is not possible without session? Or invite users to join this thread? |
A lot of users are creating new rails projects with the I was thinking of linking to this part of the omniauth/omniauth readme: |
@lynndylanhurley nah, this readme section have ~0 sense. This will work in case you render html with rails-api. Nobody does that. People are making json requests to rails-api. You can't get redirected with that. You can't open popups with that. Omniauth is supporting redirects & popus but it can't handle json requests. You don't have "a magic trick" here.
And uh... In 2 and 3 cases, since this is an open-source way & many people will tend to use it, you probably should create a client lib to make it more friendly to use (or you should write a big good doc with examples of how to use it) I spent tens of hours on that story. I ended up with custom lib to handle facebook authentication. To be more precise, 2 libs. One for server (for rails-api). One for client (for single page react application). Facebook truly has a little bit of magic behind his SDK so the client lib is just a wrapper of the official SDK (in my exact case this was my wrapper of unofficial wrapper of official SDK). Yet again: You don't have "a magic trick" here. |
Rails is going to be a dead framework if problems with API-based token authentication aren't figured out. With the problems many of us have had, the community either needs to rally around a new gem for API-based token authentication, or rewrite a new one leveraging devise that won't break by default when conflicting with devise's route namespace. I needed a little bit of vanilla html But sadly, getting these two endpoints to play nice with eachother has become a burden due to issues with sessions. It would be amazing if I could get browser-compatible implementation of Bearer Token so that both |
@KelseyDH - I'm not sure I understand the issue. Is it that DTA and devise can't use the same endpoints for authentication? |
@lynndylanhurley The only way to get |
@KelseyDH the issue is that devise and DTA work differently, and it's difficult to have different functionality from different gems that lives in the same namespace and controller. Are you suggesting that we duplicate all of the functionality of devise on top of the existing functionality of this gem? |
@lynndylanhurley Not necessarily. Stepping back: the reason I had to bring vanilla Maybe there's an answer to this problem, but I haven't found one, so I had to bring in I acknowledge there could be another solution to this, but I'm unaware of what: Establish dead simple documentation + steps for how password resets can be handled in |
I agree with you. I have something in the works but work is crazy so I can't give an ETA. If you have the time we're happy to accept a PR. |
Hello everybody,
I'm trying to accomplish the following :
My only config is :
So I've got a form setup on my client angular app, which when the login form is submitted using path
/api/v1/auth/sign_in
and that path is defined in my api like this :And khabum! The error in the log :
Then I think to myself what session, this is an API not a web app! and then I take a look at my middleware which looks like this, yeah no sessions, cookies etc:
I'm using the
rails-api
gem which provides more lightweight version of rails with no session etc.This is how my application controller looks like (Ignoring the namespace) :
So since the create action is processed by this gems session controller
devise_token_auth/sessions#create
, I tried to create my own controller override to see what's going on with this. And I did this :So now I had my new controller, very simple :
However again same error with the sessions and omniauth, code never reaches my sessions controller, caused by my initializer :
So I try to remove the initializer and observed that now the code actually reaches my controller and that I can authenticate if I add some code to it.
So there is no way (at the moment), for non session based authentication to work in combination with google/facebook.
But I'm thinking I should be able to do (two different type of users):
But there is workaround for this (to support both types of auth), and it is (add session middleware to make omniauth happy), this is my application.rb :
Am I missing something here? Why do I need the session at all, any suggestion how to go around this? Should I even go a round I mean ..
This might be what @nicolas-besnard was talking about in seperate threads or not..
The text was updated successfully, but these errors were encountered: