-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create a google service to refact code and create refresh token funct…
…ions
- Loading branch information
1 parent
6a16f19
commit de4bbe1
Showing
8 changed files
with
89 additions
and
14 deletions.
There are no files selected for viewing
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
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" |
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
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 |
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
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 |
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
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
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
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |