class MsRest::TokenCredentials

Class which keeps functionality and date for performing OAuth (token based) authentication.

Constants

DEFAULT_SCHEME

Attributes

token_provider[RW]

@return [String] the scheme for arranging token in the HTTP header.

Public Class Methods

new(*args) click to toggle source

Creates and initialize new instance of the TokenCredentials class. @param #token_provider [TokenProvider] the token provider. @param token [String] the token.

# File lib/ms_rest/credentials/token_credentials.rb, line 23
def initialize(*args)
  if (args.size == 1)
    if args[0].respond_to? :get_authentication_header
      @token_provider = args[0]
    elsif args[0].is_a? String
      @token_provider = StringTokenProvider.new args[0], DEFAULT_SCHEME
    else
      fail ArgumentError, 'Invalid argument was passed, is can be either TokenProvider or token'
    end
  elsif (args.size == 2)
    token = args[0]
    token_type = args[1]
    @token_provider = StringTokenProvider.new token, token_type
  else
    fail ArgumentError, 'Invalid number of parameters was passed to TokenCredentials constructor, valid number is 1 or 2'
  end
end

Public Instance Methods

sign_request(request) click to toggle source

Attaches OAuth authentication header to the given HTTP request. @param request [Net::HTTPRequest] the request authentication header needs to be attached to.

@return [Net::HTTPRequest] request with attached authentication header

# File lib/ms_rest/credentials/token_credentials.rb, line 46
def sign_request(request)
  super(request)
  header = @token_provider.get_authentication_header

  if (request.respond_to?(:request_headers))
    request.request_headers[AUTHORIZATION] = header
  elsif request.respond_to?(:headers)
    request.headers[AUTHORIZATION] = header
  else
    fail ArgumentError, 'Incorrect request object was provided'
  end
end