class Azure::Auth::TokenProvider::MsiTokenSource

Provdes OAuth 2.0 access token by calling Azure MV IDMS

Constants

API_VERSION
AZURE_VM_IDMS_ENDPOINT

Public Instance Methods

token(resource = DEFAULT_RESOURCE) click to toggle source
# File lib/azure/auth/token_provider/msi_token_source.rb, line 41
def token(resource = DEFAULT_RESOURCE)
  query_params = "#{API_VERSION}&resource=#{resource}"
  uri_src = "#{AZURE_VM_IDMS_ENDPOINT}?#{query_params}"
  uri = URI.parse(uri_src)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  request['Metadata'] = 'true'
  response = http.request(request)
  return nil if response.code != '200'

  parse_json_token(response.body)
end

Private Instance Methods

parse_json_token(token_src) click to toggle source
# File lib/azure/auth/token_provider/msi_token_source.rb, line 56
def parse_json_token(token_src)
  token_hash = JSON.parse(token_src)
  Token.new(
    token_hash['access_token'],
    Time.at(token_hash['expires_on'].to_i),
    token_hash['token_type'],
    read_ext(token_hash)
  )
end
read_ext(token_hash) click to toggle source
# File lib/azure/auth/token_provider/msi_token_source.rb, line 66
def read_ext(token_hash)
  {
    client_id: token_hash['client_id'],
    expires_in: token_hash['expires_in'].to_i,
    ext_expires_in: token_hash['ext_expires_in'].to_i,
    not_before: Time.at(token_hash['not_before'].to_i),
    resource: token_hash['resource']
  }
end