Skip to content

Commit

Permalink
create a google service to refact code and create refresh token funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
eversonclear committed Mar 16, 2023
1 parent 6a16f19 commit de4bbe1
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
SECRET_KEY_BASE = "a98b01b9e0ee81b268ed8b33c846874c44a7307e7a9be5df2f659ba4dab528866010460ce559334aa8245525ba95c6a4c790d2be193e634a27777e62f09ec94a"
PAGARME_API_KEY = ''
GOOGLE_CLIENT_ID="336741883605-h9iivj9fgl5aig14ki3bversvhh4q6tu.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET_ID="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"
MICROSOFT_CLIENT_ID="7a59a59c-a48a-4ddc-9542-c2ce3c15cb2b"
18 changes: 13 additions & 5 deletions app/controllers/google_authentication_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
require 'json'
require 'net/http'
class GoogleAuthenticationController < ApplicationController
before_action :set_google_service
def authenticate
response = Net::HTTP.get(URI("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=#{params[:id_token]}"))
response = JSON.parse(response)
render json: { error: 'Invalid token' } and return unless response['email'] && response['email_verified']
user_data = @google_service.get_user_data(params[:id_token])
render json: { error: 'Invalid token' } and return unless user_data['email'] && user_data['email_verified']

password = Devise.friendly_token[0,20]
@current_user = User.where(email: response['email']).first_or_create(password: password, first_name: response['given_name'], last_name: response['family_name'])
@current_user.update(google_token: params[:id_token])
@current_user = User.where(email: user_data['email']).first_or_create(password: password, first_name: user_data['given_name'], last_name: response['family_name'])

data_token = @google_service.generate_refresh_token(params[:code])
@current_user.update(google_token: data_token["access_token"], google_refresh_token: data_token["refresh_token"], google_expire_token: Time.now + data_token['expires_in'])

render 'users/show'
end

def set_google_service
@google_service ||= GoogleService.new
end
end
48 changes: 48 additions & 0 deletions app/services/google_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'json'
require 'net/http'

class GoogleService
def initialize
@client_id = ENV["GOOGLE_CLIENT_ID"]
@client_secret = ENV["GOOGLE_CLIENT_SECRET_ID"]
end

def get_user_data(id_token)
user_data = Net::HTTP.get(URI("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=#{id_token}"))
JSON.parse(user_data)
end

def generate_refresh_token(authorization_code)
url = URI('https://accounts.google.com/o/oauth2/token')

params = {
code: authorization_code,
client_id: @client_id,
client_secret: @client_secret,
grant_type: "authorization_code",
access_type: "offline",
redirect_uri: 'http:https://localhost:3001'
}

res = Net::HTTP.post_form(url, params)
JSON.parse(res.body)
end

def access_token_is_valid?(date_expire)
(date_expire - 5.minutes) > Time.now
end

def refresh_token(refresh_token)
url = URI('https://oauth2.googleapis.com/token')

params = {
refresh_token: refresh_token,
client_id: @client_id,
client_secret: @client_secret,
grant_type: 'refresh_token',
}

res = Net::HTTP.post_form(url, params)
JSON.parse(res.body)
end
end
2 changes: 1 addition & 1 deletion app/services/microsoft_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def refresh_params(azure_refresh_token)
{
grant_type: 'refresh_token',
refresh_token: azure_refresh_token,
client_id: '7a59a59c-a48a-4ddc-9542-c2ce3c15cb2b',
client_id: ENV['MICROSOFT_CLIENT_ID'],
scope: [
'openid',
'offline_access',
Expand Down
24 changes: 16 additions & 8 deletions app/sidekiq/update_calendars_events_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ class UpdateCalendarsEventsJob

def perform(current_user_id)
@current_user = User.find(current_user_id)
refresh_token_if_invalid
set_google_calendar_service
update_user_calendars
end

def update_user_calendars
@google_service = google_service
def refresh_token_if_invalid
@google_service = GoogleService.new

calendars = @google_service.list_calendar_lists
if !@google_service.access_token_is_valid?(@current_user.google_expire_token)
data_token = @google_service.refresh_token(@current_user.google_refresh_token)
@current_user.update(google_token: data_token["access_token"], google_expire_token: Time.now + data_token['expires_in'])
end
end

def update_user_calendars
calendars = @google_calendar_service.list_calendar_lists

calendars.items.each do |calendar_item|
@calendar = Calendar.where(remote_id: calendar_item.id).first
Expand All @@ -20,7 +29,7 @@ def update_user_calendars
@calendar = Calendar.create!(calendar_params(calendar_item))
end

events = @google_service.list_events(calendar_item.id,
events = @google_calendar_service.list_events(calendar_item.id,
time_min: Time.now.iso8601)

events.items.each do |event_item|
Expand Down Expand Up @@ -48,12 +57,11 @@ def update_user_calendars

private

def google_service
def set_google_calendar_service
token = AccessTokenService.new @current_user.google_token

google_service = Google::Apis::CalendarV3::CalendarService.new
google_service.authorization = token
google_service
@google_calendar_service = Google::Apis::CalendarV3::CalendarService.new
@google_calendar_service.authorization = token
end

def calendar_params(calendar)
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20230310114047_add_google_token_to_users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class AddGoogleTokenToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :google_token, :string
add_column :users, :google_refresh_token, :string
add_column :users, :google_expire_token, :datetime
end
end
2 changes: 2 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions spec/sidekiq/update_calendars_events_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe UpdateCalendarsEventsJob, type: :job do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit de4bbe1

Please sign in to comment.