class Bcome::Driver::Gcp::Authentication::Oauth

Attributes

client_config[R]
scopes[R]
secrets_filename[R]
service[R]

Public Class Methods

new(driver, service, client_config, node) click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 15
def initialize(driver, service, client_config, node)
  @service = service
  @scopes = client_config.scopes
  @node = node
  @driver = driver
  @client_config = client_config
  @secrets_filename = client_config.secrets_filename
  @path_to_secrets = "#{credential_directory}/#{@secrets_filename}"

  raise ::Bcome::Exception::Generic, "Missing OAuth 2.0 client secrets file from GCP network configuration. Cannot find '#{@path_to_secrets}'" unless File.exist?(@path_to_secrets) && File.file?(@path_to_secrets)

  # All credentials are held in .gauth
  ensure_credential_directory
end

Public Instance Methods

authorize!() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 38
def authorize!
  @service.authorization = storage.authorize
end
authorized?() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 30
def authorized?
  storage && !@storage.authorization.nil?
end
client_secrets() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 42
def client_secrets
  @client_secrets ||= load_client_secrets
end
credential_file() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 56
def credential_file
  # If an authorization has the same scopes & secrets file, it is the same authorization. Hence we store the resulting oauth2 access credentials as the same file. This allows
  # re-use of authorizations and prevents multiple oauth loops.
  "#{@client_config.checksum}:#{credential_file_suffix}"
end
credential_file_suffix() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 34
def credential_file_suffix
  'oauth2.json'
end
do!() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 62
def do!
  authorize!
  if @storage.authorization.nil?
    # Total bloat from google here. Thanks google... requiring at last possible moment.
    require 'google/api_client/auth/installed_app'

      wrap_indicator type: :basic, title: loader_title, completed_title: '' do
      flow = Google::APIClient::InstalledAppFlow.new(
        client_id: client_secrets.client_id,
        client_secret: client_secrets.client_secret,
        scope: @scopes
      )
 
      ## Override the redirected-to screen so that clearer instruction can be given
      flow.class.send(:remove_const,'RESPONSE_BODY') if flow.class.const_defined?('RESPONSE_BODY')
      flow.class.send(:const_set,'RESPONSE_BODY', oauth_redirect_html)

      begin
         @service.authorization = flow.authorize(storage)
         signal_success
      rescue ArgumentError => e
        signal_failure
        raise ::Bcome::Exception::MissingOrInvalidClientSecrets, "#{@path_to_secrets}. Gcp exception: #{e.class} #{e.message}"
       end
    end
  end

  @service
end
load_client_secrets() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 46
def load_client_secrets
  ::Google::APIClient::ClientSecrets.load(@path_to_secrets)
rescue Exception => e
  raise ::Bcome::Exception::MissingOrInvalidClientSecrets, "#{@path_to_secrets}. Gcp exception: #{e.class} #{e.message}"
end
notify_success() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 92
def notify_success
  print "[\s" + "Credentials file written to\s" + full_path_to_credential_file + "\s]" + "\n"
end
storage() click to toggle source
# File lib/objects/driver/gcp/authentication/oauth.rb, line 52
def storage
  @storage ||= ::Google::APIClient::Storage.new(Google::APIClient::FileStore.new(full_path_to_credential_file))
end