Class: Pluggy::CredentialStore

Inherits:
Object
  • Object
show all
Defined in:
lib/pluggy/credential_store.rb

Overview

Holds the current apiKey for one client and renews it as needed.

One store per Client, so a key is fetched at most once per lifetime regardless of how many services or threads use it.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CredentialStore

Returns a new instance of CredentialStore.



9
10
11
12
13
14
15
# File 'lib/pluggy/credential_store.rb', line 9

def initialize(config)
  @config = config
  @mutex = Mutex.new
  @key = config.api_key ? ApiKey.new(config.api_key) : nil
  # A caller-supplied key with no credentials behind it cannot be renewed.
  @static = !config.api_key.nil? && !config.credentials?
end

Instance Method Details

#fetch(requestor) ⇒ Object



19
20
21
22
23
24
# File 'lib/pluggy/credential_store.rb', line 19

def fetch(requestor)
  @mutex.synchronize do
    @key = authenticate(requestor) if @key.nil? || (!@static && @key.expired?)
    @key
  end
end

#refresh!(requestor, stale:) ⇒ Object

Called after a 403 whose body carried no codeDescription -- see APIRequestor#expired_key?.

stale is the key that just failed. If another thread has already rotated it, reuse theirs rather than authenticating again: N threads hitting expiry together produce exactly one POST /auth.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pluggy/credential_store.rb', line 32

def refresh!(requestor, stale:)
  if @static
    raise AuthenticationError,
      "the supplied apiKey was rejected and there are no credentials to renew it with; " \
      "construct Pluggy::Client with client_id: and client_secret: for automatic renewal"
  end

  @mutex.synchronize do
    return @key if @key && !@key.equal?(stale)

    @key = authenticate(requestor)
  end
end

#static?Boolean

Returns:

  • (Boolean)


17
# File 'lib/pluggy/credential_store.rb', line 17

def static? = @static