Class: ShedCloud::PartnerApi::AuthProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/shedcloud/partner_api/auth_provider.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, auth:, transport: HttpTransport.new, token_skew_seconds: 60) ⇒ AuthProvider

Returns a new instance of AuthProvider.



9
10
11
12
13
14
15
16
# File 'lib/shedcloud/partner_api/auth_provider.rb', line 9

def initialize(base_url:, auth:, transport: HttpTransport.new, token_skew_seconds: 60)
  @base_url = Hosts.trim_trailing_slashes(base_url)
  @auth = auth
  @transport = transport
  @token_skew_seconds = token_skew_seconds
  @cached_access_token = nil
  @cached_expires_at = nil
end

Instance Method Details

#access_tokenObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/shedcloud/partner_api/auth_provider.rb', line 18

def access_token
  return @auth.api_key.to_s.strip if @auth.api_key?

  now = Time.now.to_i
  if @cached_access_token && @cached_expires_at && now < (@cached_expires_at - @token_skew_seconds)
    return @cached_access_token
  end

  refresh_oauth_token
end

#refreshObject



29
30
31
32
33
34
35
# File 'lib/shedcloud/partner_api/auth_provider.rb', line 29

def refresh
  return @auth.api_key.to_s.strip if @auth.api_key?

  @cached_access_token = nil
  @cached_expires_at = nil
  refresh_oauth_token
end