Class: Bcome::Driver::Gcp::Authentication::Oauth

Inherits:
Base
  • Object
show all
Includes:
Utilities
Defined in:
lib/objects/driver/gcp/authentication/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#oauth_redirect_body, #oauth_redirect_html

Methods inherited from Base

#credential_directory, #ensure_credential_directory, #full_path_to_credential_file, #loader_title

Methods included from LoadingBar::Handler

#cursor, #do_signal, #fork_process, #signal_failure, #signal_stop, #signal_success, #start_indicator, #stop_indicator, #wrap_indicator

Constructor Details

#initialize(driver, service, client_config, node) ⇒ Oauth

Returns a new instance of Oauth.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#client_configObject (readonly)

Returns the value of attribute client_config.



13
14
15
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 13

def client_config
  @client_config
end

#scopesObject (readonly)

Returns the value of attribute scopes.



13
14
15
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 13

def scopes
  @scopes
end

#secrets_filenameObject (readonly)

Returns the value of attribute secrets_filename.



13
14
15
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 13

def secrets_filename
  @secrets_filename
end

#serviceObject (readonly)

Returns the value of attribute service.



13
14
15
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 13

def service
  @service
end

Instance Method Details

#authorize!Object



38
39
40
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 38

def authorize!
  @service.authorization = storage.authorize
end

#authorized?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 30

def authorized?
  storage && !@storage.authorization.nil?
end

#client_secretsObject



42
43
44
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 42

def client_secrets
  @client_secrets ||= load_client_secrets
end

#credential_fileObject



56
57
58
59
60
# 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_suffixObject



34
35
36
# File 'lib/objects/driver/gcp/authentication/oauth.rb', line 34

def credential_file_suffix
  'oauth2.json'
end

#do!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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_secretsObject



46
47
48
49
50
# 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_successObject



92
93
94
# 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

#storageObject



52
53
54
# 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