Sha256: 5941dc6c3879131cc8c2b23ff2dfed62a80a3d1269c73effcfe7d14a15ae565b

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

module RailsCloudTasks
  class Credentials
    require 'googleauth'
    require 'google/apis/iamcredentials_v1'

    DEFAULT_SCOPES = ['https://www.googleapis.com/auth/cloud-platform'].freeze
    attr_reader :request_options, :iam_credential, :token_request, :auth

    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

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

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

      request_options.authorization
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails-cloud-tasks-0.0.10 lib/rails_cloud_tasks/credentials.rb
rails-cloud-tasks-0.0.9 lib/rails_cloud_tasks/credentials.rb
rails-cloud-tasks-0.0.9.pre.rc lib/rails_cloud_tasks/credentials.rb
rails-cloud-tasks-0.0.8 lib/rails_cloud_tasks/credentials.rb