class MsRestAzure::AzureCliTokenProvider

Class that provides access to access tokens generated by the azure cli.

Attributes

cli_path[R]

@return [String] the type of token.

expiration_threshold[R]

@return [Integer] the amount of time we refresh token before it expires.

settings[RW]

@return [ActiveDirectoryServiceSettings] settings.

token[RW]

@return [String] auth token.

token_expires_on[RW]

@return [Time] the date when the current token expires.

token_type[R]

@return [String] the type of token.

Public Class Methods

new(settings = ActiveDirectoryServiceSettings.get_azure_settings) click to toggle source

Creates and initialize new instance of the AzureCliTokenProvider class. @param settings [ActiveDirectoryServiceSettings] active directory setting.

# File lib/ms_rest_azure/credentials/azure_cli_token_provider.rb, line 37
def initialize(settings = ActiveDirectoryServiceSettings.get_azure_settings)
  fail ArgumentError, 'Azure AD settings cannot be nil' if settings.nil?
  raise AzureCliError, 'Azure CLI is not installed' unless locate_azure_cli

  @settings = settings

  @expiration_threshold = 5 * 60
end

Public Instance Methods

get_authentication_header() click to toggle source

Gets an authentication header string using an access token from the Azure cli @param settings [ActiveDirectoryServiceSettings] active directory settings.

@return [String] The authenticaiton header string

# File lib/ms_rest_azure/credentials/azure_cli_token_provider.rb, line 51
def get_authentication_header
  acquire_token if token_expired?
  "#{token_type} #{token}"
end

Private Instance Methods

acquire_token() click to toggle source

Acquires a new access token from teh azure CLI

@return [String] The access token to the desired resource

# File lib/ms_rest_azure/credentials/azure_cli_token_provider.rb, line 86
def acquire_token()
  response_body = JSON.load(`#{cli_path} account get-access-token -o json --resource #{@settings.token_audience}`)
  
  @token_expires_on = Time.parse(response_body['expiresOn'])
  @token_type = response_body['tokenType']
  @token = response_body['accessToken']
rescue
  raise AzureCliError, 'Error acquiring token from the Azure CLI'
end
locate_azure_cli() click to toggle source

Checks if the Azure CLI is installed

@return [Boolean] Does the az command exist on the path

# File lib/ms_rest_azure/credentials/azure_cli_token_provider.rb, line 70
def locate_azure_cli
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      executable = File.join(path, "az#{ext}")
      return @cli_path = executable if File.executable?(executable) && !File.directory?(executable)
    end
  end

  return nil
end
token_expired?() click to toggle source

Checks whether token is about to expire.

@return [Bool] True if token is about to expire, false otherwise.

# File lib/ms_rest_azure/credentials/azure_cli_token_provider.rb, line 62
def token_expired?
  @token.nil? || Time.now >= @token_expires_on + expiration_threshold
end