Created
January 20, 2016 14:15
-
-
Save ssuprunenko/2493dec585c9e7a38439 to your computer and use it in GitHub Desktop.
Refresh Google OAuth2 access token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GoogleAuthService | |
def initialize(identity) | |
@identity = identity | |
@oauth2_token = @identity.token | |
@oauth2_refresh_token = @identity.refresh_token | |
end | |
def refresh_token! | |
new_token = token.refresh! | |
@identity.update( | |
token: new_token.token, | |
refresh_token: new_token.refresh_token, | |
expires_at: Time.at(new_token.expires_at) | |
) unless new_token.expired? | |
end | |
private | |
def strategy | |
OmniAuth::Strategies::GoogleOauth2.new( | |
nil, | |
ENV['GOOGLE_CLIENT_ID'], | |
ENV['GOOGLE_CLIENT_SECRET'] | |
) | |
end | |
def client | |
strategy.client | |
end | |
def token | |
OAuth2::AccessToken.new( | |
client, | |
@oauth2_token, | |
refresh_token: @oauth2_refresh_token | |
) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: identities | |
# | |
# id :integer not null, primary key | |
# provider :string | |
# uid :string | |
# user_id :integer | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# profile_link :string | |
# token :string | |
# refresh_token :string | |
# expires_at :integer | |
# | |
class Identity < ActiveRecord::Base | |
belongs_to :user | |
validates :provider, :user_id, :uid, presence: true | |
def self.find_from_omniauth(auth) | |
find_by(provider: auth.provider, uid: auth.uid) | |
end | |
def self.create_from_omniauth(auth, user) | |
if auth.provider == 'facebook' | |
profile_link = 'https://facebook.com/' + auth.uid | |
else | |
profile_link = auth.info.try(:urls).try(:first).try(:[], 1) | |
end | |
Identity.create!(user: user, | |
provider: auth.provider, | |
uid: auth.uid, | |
profile_link: profile_link, | |
token: auth.credentials.token, | |
refresh_token: auth.credentials.refresh_token, | |
expires_at: auth.credentials.expires_at) | |
end | |
def expired? | |
expires_at.present? && expires_at < Time.now.to_i | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StaticPagesController < ApplicationController | |
before_action :check_google_token | |
# ... | |
private | |
def check_google_token | |
if current_user.identity.expired? | |
GoogleAuthService.new(current_user.identity).refresh_token! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment