-
Notifications
You must be signed in to change notification settings - Fork 228
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
Support for returning refresh tokens in the external module (OAuth)? #201
Comments
If anyone would like to extend the return hash to include refresh token, I would certainly take a look at the PR and merge it in if everything looks fine. That being said, ideally we will be refactoring sorcery to depend on Omniauth for the external module, and I'm sure Omniauth already has this functionality implemented for most if not all providers. |
Hi @risafj , The If I do Maybe check if you have the scopes for Google right: config.google.scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile" So, simplistically, if you add def callback
provider = "google"
if (@user = login_from(provider))
@user.authentications.find_by(provider: provider).update_columns(refresh_token: @user_hash[:refresh_token], expires_at: Time.at(@user_hash[:expires_at]))
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
else
begin
@user = create_from(provider)
@user.authentications.find_by(provider: provider).update_columns(refresh_token: @user_hash[:refresh_token], expires_at: Time.at(@user_hash[:expires_at]))
reset_session # protect from session fixation attack
auto_login(@user)
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
rescue
redirect_to root_path, alert: "Failed to login from #{provider.titleize}!"
end
end
end |
Hi @mtomov, Thank you for the response and the code snippet! @auth_url = '/o/oauth2/auth' https://github.com/Sorcery/sorcery/blob/master/lib/sorcery/providers/google.rb#L18 So we overrode the config.google.auth_url = '/o/oauth2/auth?access_type=offline' Then, we could access the refresh token like this in the controller (because as you say, "the refresh_token is actually part of the access_token"): I raised this Github issue before learning that overriding the auth_url was an option. Now that I know, this is a non-issue for me, though I still think it may be kinder to users if offline access was enabled by default. |
I think it would make sense to add this to the wiki, and add a note in the initializer for the Google oauth config. I assume that offline access would require additional permissions from the end user, so changing the default behavior would be a breaking change for applications that don't utilize the refresh token. |
@athix That's a good point. Let me check again if the |
Sounds good, thanks @risafj! |
@risafj In my brief tests, with the default configuration (no change in |
@mtomov Hmm... I wonder what the difference between our configurations are.
I just overrode the This is the repo - please check it out if you have a chance: https://github.com/risafj/goo_oauth |
Edit: Not really, no |
Actually I figured it out. In my first try, I followed the API demo on the google api pages where there was a one-button-click way to create oauth credentials. Those turned out to be of type "other", which doesn't require setting redirect urls, and a few other settings, it doesn't persist for the user, and seems like it gives a I now went to create a new Oauth flow, and chose the web application type and, yes, I don't have a refresh token with the default settings : ) Adding Btw, also be careful not to loose the refresh token - as you only even get one per authorization. People were puzzled on that - see here googleapis/google-api-python-client#213 (comment) |
Awesome, thank you @mtomov :) |
@mladenilic I think so, yes. There's plenty of information here for anyone looking to solve this in the future, and SEO typically does a good job of finding these issues. For anyone finding this: if this doesn't cover your specific scenario, please feel free to open a new issue with additional details. |
Hi, do you have any plans to return not only the
access_token
but also therefresh_token
when logging the user in via the external module? Therefresh_token
allows theaccess_token
to be refreshed when it expires in one hour.Right now, the
@user_hash
being returned looks like this (I'm using Google as the provider), and therefresh_token
is nil.{:token=>"<long string>", :refresh_token=>nil, :expires_at=> <integer>, :expires_in=> <integer>, :user_info=>{"id"=>"<numbers>", "email"=>"email@email.com", "verified_email"=>true, "name"=>"<full name>", "given_name"=>"<first name>", "family_name"=>"<last name>", "picture"=>"<jpg url>", "locale"=>"ja", "hd"=>"<domain>"}, :uid=>"<numbers>"}
The text was updated successfully, but these errors were encountered: