ToolJet/app/services/google_oauth_service.rb
Deepti Kakade b10d777f63
Fixes Rubocop issues in tests (#359)
* Updated project set up guide for Mac, added node version and Webpack install steps.

* Worked on PR comment i.e Can we change this line to Install Node.js ( version: v14.9.0 )

* Fixed "Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping."

* Resolved rubocop comments i.e "Style/FrozenStringLiteralComment: Missing frozen string literal comment." in test folder

Co-authored-by: Deepti Kakade <deepti@saeloun.com>
Co-authored-by: Deepti Kakade <“deepti@saeloun.com”>
2021-07-01 12:54:35 +05:30

62 lines
2.4 KiB
Ruby

# frozen_string_literal: true
class GoogleOauthService
def self.generate_base_auth_url
client_id = ENV.fetch("GOOGLE_CLIENT_ID", "")
"https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=#{client_id}&redirect_uri=#{ENV.fetch('TOOLJET_HOST')}/oauth2/authorize"
end
def self.fetch_access_token(code)
access_token_url = "https://oauth2.googleapis.com/token"
client_id = ENV.fetch("GOOGLE_CLIENT_ID", "")
client_secret = ENV.fetch("GOOGLE_CLIENT_SECRET", "")
grant_type = "authorization_code"
custom_params = [
%w[prompt consent],
%w[access_type offline]
].to_h
response = HTTParty.post(access_token_url,
body: { code: code,
client_id: client_id,
client_secret: client_secret,
grant_type: grant_type,
redirect_uri: "#{ENV.fetch('TOOLJET_HOST')}/oauth2/authorize",
**custom_params }.to_json,
headers: { "Content-Type" => "application/json" })
result = JSON.parse(response.body)
access_token = result["access_token"]
refresh_token = result["refresh_token"]
[["access_token", access_token], ["refresh_token", refresh_token]]
end
def self.refresh_access_token(refresh_token, data_source)
access_token_url = "https://oauth2.googleapis.com/token"
client_id = ENV.fetch("GOOGLE_CLIENT_ID")
client_secret = ENV.fetch("GOOGLE_CLIENT_SECRET")
grant_type = "refresh_token"
response = HTTParty.post(access_token_url,
body: { refresh_token: refresh_token,
client_id: client_id,
client_secret: client_secret,
grant_type: grant_type,
redirect_uri: "#{ENV.fetch('TOOLJET_HOST')}/oauth2/authorize"
}.to_json,
headers: { "Content-Type" => "application/json" })
result = JSON.parse(response.body)
access_token = result["access_token"]
credential_id = data_source.options["access_token"]["credential_id"]
credential = Credential.find(credential_id)
credential.update(value: access_token)
access_token
end
end