Class: Conjur::API::TokenFileAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/conjur/base.rb

Overview

Obtains fresh tokens by reading them from a file. Some other process is assumed to be acquiring tokens and storing them to the file on a regular basis.

This authenticator assumes that the token was created immediately before it was written to the file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_file) ⇒ TokenFileAuthenticator

Returns a new instance of TokenFileAuthenticator.



343
344
345
# File 'lib/conjur/base.rb', line 343

def initialize token_file
  @token_file = token_file
end

Instance Attribute Details

#last_mtimeObject (readonly)

Returns the value of attribute last_mtime.



347
348
349
# File 'lib/conjur/base.rb', line 347

def last_mtime
  @last_mtime
end

#token_fileObject (readonly)

Returns the value of attribute token_file.



341
342
343
# File 'lib/conjur/base.rb', line 341

def token_file
  @token_file
end

Instance Method Details

#mtimeObject



349
350
351
# File 'lib/conjur/base.rb', line 349

def mtime
  File.mtime token_file
end

#needs_token_refresh?Boolean

Returns:

  • (Boolean)


363
364
365
# File 'lib/conjur/base.rb', line 363

def needs_token_refresh?
  mtime != last_mtime
end

#refresh_tokenObject



353
354
355
356
357
358
359
360
361
# File 'lib/conjur/base.rb', line 353

def refresh_token
  # There's a race condition here in which the file could be updated
  # after we read the mtime but before we read the file contents. So to be
  # conservative, use the oldest possible mtime.
  mtime = self.mtime
  File.open token_file, 'r' do |f|
    JSON.load(f.read).tap { @last_mtime = mtime }
  end
end