Class: RailsCloudTasks::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_cloud_tasks/credentials.rb

Constant Summary collapse

DEFAULT_SCOPES =
['https://www.googleapis.com/auth/cloud-platform'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_options: Google::Apis::RequestOptions.new, iam_credential: Google::Apis::IamcredentialsV1::IAMCredentialsService.new, token_request: Google::Apis::IamcredentialsV1::GenerateAccessTokenRequest, auth: Google::Auth) ⇒ Credentials

Returns a new instance of Credentials.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rails_cloud_tasks/credentials.rb', line 9

def initialize(
  request_options: Google::Apis::RequestOptions.new,
  iam_credential: Google::Apis::IamcredentialsV1::IAMCredentialsService.new,
  token_request: Google::Apis::IamcredentialsV1::GenerateAccessTokenRequest,
  auth: Google::Auth
)
  @auth = auth
  @request_options = request_options
  @iam_credential = iam_credential
  @token_request = token_request
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



7
8
9
# File 'lib/rails_cloud_tasks/credentials.rb', line 7

def auth
  @auth
end

#iam_credentialObject (readonly)

Returns the value of attribute iam_credential.



7
8
9
# File 'lib/rails_cloud_tasks/credentials.rb', line 7

def iam_credential
  @iam_credential
end

#request_optionsObject (readonly)

Returns the value of attribute request_options.



7
8
9
# File 'lib/rails_cloud_tasks/credentials.rb', line 7

def request_options
  @request_options
end

#token_requestObject (readonly)

Returns the value of attribute token_request.



7
8
9
# File 'lib/rails_cloud_tasks/credentials.rb', line 7

def token_request
  @token_request
end

Instance Method Details

#generate(impersonate_account = nil, scopes = []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_cloud_tasks/credentials.rb', line 21

def generate( = nil, scopes = [])
  current_scopes = DEFAULT_SCOPES + scopes
  authorization = auth.get_application_default(current_scopes).dup
  request_options.authorization = authorization

  if 
    iam_credential.(
      "projects/-/serviceAccounts/#{}",
      token_request.new(scope: current_scopes, lifetime: '3600s'),
      options: request_options
    )
  end

  request_options.authorization
end