class Bosh::Monitor::UAAToken

Constants

EXPIRATION_DEADLINE_IN_SECONDS

Public Class Methods

new(client_id, client_secret, uaa_url, ca_cert_file_path, logger) click to toggle source
# File lib/bosh/monitor/auth_provider.rb, line 39
def initialize(client_id, client_secret, uaa_url, ca_cert_file_path, logger)
  options = {}

  if File.exist?(ca_cert_file_path) && !File.read(ca_cert_file_path).strip.empty?
    options[:ssl_ca_file] = ca_cert_file_path
  else
    cert_store = OpenSSL::X509::Store.new
    cert_store.set_default_paths
    options[:ssl_cert_store] = cert_store
  end

  @uaa_token_issuer = CF::UAA::TokenIssuer.new(
    uaa_url,
    client_id,
    client_secret,
    options,
  )
  @logger = logger
end

Public Instance Methods

auth_header() click to toggle source
# File lib/bosh/monitor/auth_provider.rb, line 59
def auth_header
  if @uaa_token && !expires_soon?
    return @uaa_token.auth_header
  end

  fetch

  @uaa_token ? @uaa_token.auth_header : nil
end

Private Instance Methods

decode() click to toggle source
# File lib/bosh/monitor/auth_provider.rb, line 83
def decode
  access_token = @uaa_token.info['access_token'] || @uaa_token.info[:access_token]
  CF::UAA::TokenCoder.decode(
    access_token,
    {verify: false},
    nil, nil)
end
expires_soon?() click to toggle source
# File lib/bosh/monitor/auth_provider.rb, line 71
def expires_soon?
  expiration = @token_data[:exp] || @token_data['exp']
  (Time.at(expiration).to_i - Time.now.to_i) < EXPIRATION_DEADLINE_IN_SECONDS
end
fetch() click to toggle source
# File lib/bosh/monitor/auth_provider.rb, line 76
def fetch
  @uaa_token = @uaa_token_issuer.client_credentials_grant
  @token_data = decode
rescue => e
  @logger.error("Failed to obtain token from UAA: #{e.inspect}")
end