Class: LittleGhost::Providers::VertexAI::CredentialResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/providers/vertex_ai/credential_resolver.rb

Overview

Resolves Vertex access tokens from explicit values, service-account ADC, or the Google metadata server.

Constant Summary collapse

TOKEN_URI =

:nodoc:

URI("https://oauth2.googleapis.com/token")
SCOPE =

:nodoc:

"https://www.googleapis.com/auth/cloud-platform"

Instance Method Summary collapse

Constructor Details

#initialize(environment: ENV, access_token: nil, clock: -> { Time.now.to_i }) ⇒ CredentialResolver

Uses an explicit token or Google application credentials in environment.



18
19
20
21
22
23
# File 'lib/little_ghost/providers/vertex_ai/credential_resolver.rb', line 18

def initialize(environment: ENV, access_token: nil, clock: -> { Time.now.to_i })
  @environment = environment
  @access_token = access_token
  @clock = clock
  @mutex = Mutex.new
end

Instance Method Details

#call(cancellation_token: nil, deadline: nil) ⇒ Object

Returns a current access token, refreshing cached credentials as needed.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/little_ghost/providers/vertex_ai/credential_resolver.rb', line 26

def call(cancellation_token: nil, deadline: nil)
  return @access_token unless @access_token.to_s.empty?

  @mutex.synchronize do
    return @cached_token if @cached_token && @expires_at && @expires_at > @clock.call + 60

    file = @environment["GOOGLE_APPLICATION_CREDENTIALS"]
    token, expires_in = if file
      (file, cancellation_token:, deadline:)
    else
      (cancellation_token:, deadline:)
    end
    @cached_token = token
    @expires_at = @clock.call + Integer(expires_in || 3600)
    token
  end
end