Class: MsRest::BasicAuthenticationCredentials

Inherits:
ServiceClientCredentials show all
Defined in:
lib/ms_rest/credentials/basic_authentication_credentials.rb

Constant Summary

Constants inherited from ServiceClientCredentials

ServiceClientCredentials::AUTHORIZATION

Instance Method Summary collapse

Constructor Details

#initialize(user_name, password, scheme = DEFAULT_SCHEME) ⇒ BasicAuthenticationCredentials

Creates and initialize new instance of the BasicAuthenticationCredentials class.

Parameters:

  • user_name (String)

    the username for authentication.

  • password (String)

    the password for authentication.

  • scheme (defaults to: DEFAULT_SCHEME)

    DEFAULT_SCHEME [String] the scheme for composing credentials.



32
33
34
35
36
37
38
39
40
# File 'lib/ms_rest/credentials/basic_authentication_credentials.rb', line 32

def initialize(user_name, password, scheme = DEFAULT_SCHEME)
  fail ArgumentError, 'user_name cannot be nil' if user_name.nil?
  fail ArgumentError, 'password cannot be nil' if password.nil?
  fail ArgumentError, 'scheme cannot be nil' if scheme.nil?

  @user_name = user_name
  @password = password
  @scheme = scheme
end

Instance Method Details

#sign_request(request) ⇒ Net::HTTPRequest

Attaches basic authentication header to the given HTTP request.

Parameters:

  • request (Net::HTTPRequest)

    the request authentication header needs to be attached to.

Returns:

  • (Net::HTTPRequest)

    request with attached authentication header.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ms_rest/credentials/basic_authentication_credentials.rb', line 47

def sign_request(request)
  super(request)
  encodeCredentials = Base64.strict_encode64("#{user_name}:#{password}")
  credentials = "#{scheme} #{encodeCredentials}"

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