Class: Spotted::Internal::OAuth2ClientCredentials

Inherits:
Object
  • Object
show all
Defined in:
lib/spotted/internal/oauth2.rb

Instance Method Summary collapse

Constructor Details

#initialize(token_url:, client_id:, client_secret:, timeout:, client:) ⇒ OAuth2ClientCredentials

Returns a new instance of OAuth2ClientCredentials.

Parameters:

  • token_url (String)
  • client_id (String)
  • client_secret (String)
  • timeout (Integer)
  • client (Object)


11
12
13
14
15
16
17
18
19
20
# File 'lib/spotted/internal/oauth2.rb', line 11

def initialize(token_url:, client_id:, client_secret:, timeout:, client:)
  @token_url = token_url
  @client_id = client_id
  @client_secret = client_secret
  @client = client
  @timeout = timeout
  @token = nil
  @token_expires_at = nil
  @mutex = Thread::Mutex.new
end

Instance Method Details

#auth_headersHash{String=>String}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash{String=>String})


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spotted/internal/oauth2.rb', line 25

def auth_headers
  @mutex.synchronize do
    if @token && !token_expired?
      return {"Authorization" => "Bearer #{@token}"}
    end

    @token = nil
    @token_expires_at = nil

    token_response = fetch_token
    if token_response
      @token = token_response[:access_token]
      @token_expires_at = Time.now + token_response[:expires_in]

      return {"Authorization" => "Bearer #{@token}"}
    end

    {}
  end
end