Commit 8ee4e8a2 authored by Jose Ernesto Suarez's avatar Jose Ernesto Suarez

Implementation of no file for loading credentials

parent 26cbeeca
...@@ -12,4 +12,5 @@ gem 'http','~> 4.4.0' ...@@ -12,4 +12,5 @@ gem 'http','~> 4.4.0'
gem 'logger','~> 1.4.2' gem 'logger','~> 1.4.2'
gem 'google' gem 'google'
gem 'googleauth' gem 'googleauth'
gem 'google-api-client' gem 'google-api-client'
\ No newline at end of file gem 'tempfile'
\ No newline at end of file
...@@ -35,6 +35,7 @@ Gem::Specification.new do |spec| ...@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'google' spec.add_dependency 'google'
spec.add_dependency 'googleauth' spec.add_dependency 'googleauth'
spec.add_dependency 'google-api-client' spec.add_dependency 'google-api-client'
spec.add_dependency 'tempfile'
spec.add_development_dependency 'bundler', '~> 2.1.4' spec.add_development_dependency 'bundler', '~> 2.1.4'
......
require 'googleauth' require 'googleauth'
require 'googleauth/stores/file_token_store' require 'googleauth/stores/file_token_store'
require 'tempfile'
module Wedoops module Wedoops
module Google module Google
...@@ -10,8 +11,13 @@ module Wedoops ...@@ -10,8 +11,13 @@ module Wedoops
def initialize(credentials,scopes='https://www.googleapis.com/auth/drive') def initialize(credentials,scopes='https://www.googleapis.com/auth/drive')
puts "Incilizalizando authenticator" puts "Incilizalizando authenticator"
raise "Unable to initialize with credentials file #{credentials}" unless File.exists?(credentials) if File.exists?(credentials)
@credentials_file_path = credentials puts "Loading credentials from file"
@credentials_file_path = credentials
else
@credentials_json = credentials
end
@scopes = scopes @scopes = scopes
@authenticate_by=nil @authenticate_by=nil
@credentials=nil @credentials=nil
...@@ -21,9 +27,16 @@ module Wedoops ...@@ -21,9 +27,16 @@ module Wedoops
def auth_by_console(user_id) def auth_by_console(user_id)
puts "Authenticating from console" puts "Authenticating from console"
scope = @scopes scope = @scopes
client_id = ::Google::Auth::ClientId.from_file(@credentials_file_path) client_id = nil
unless @credentials_json.nil?
client_id = ::Google::Auth::ClientId.from_hash(@credentials_json)
else
client_id = ::Google::Auth::ClientId.from_file(@credentials_file_path)
end
token_file = Tempfile.new(user_id)
token_file.close
token_store = ::Google::Auth::Stores::FileTokenStore.new( token_store = ::Google::Auth::Stores::FileTokenStore.new(
:file => './tokens/tokens.yaml') :file => token_file.path)
authorizer = ::Google::Auth::UserAuthorizer.new(client_id, scope, token_store) authorizer = ::Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
credentials = authorizer.get_credentials(user_id) credentials = authorizer.get_credentials(user_id)
...@@ -40,9 +53,17 @@ module Wedoops ...@@ -40,9 +53,17 @@ module Wedoops
end end
def auth_by_sa(user_id) def auth_by_sa(user_id)
@credentials = ::Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(@credentials_file_path), unless @credentials_file_path.nil?
scope: @scopes) @credentials = ::Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(@credentials_file_path),
scope: @scopes)
else
@credentials = ::Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: @credentials_json,
scope: @scopes)
end
@credentials.sub = user_id @credentials.sub = user_id
::Google::Apis::RequestOptions.default.authorization = @credentials ::Google::Apis::RequestOptions.default.authorization = @credentials
@authenticated_by="service_account" @authenticated_by="service_account"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment