Class: DotNetServices::Authentication::UsernamePassword

Inherits:
Object
  • Object
show all
Defined in:
lib/dot_net_services/authentication.rb

Overview

Standard Username and Password authenticator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, token = nil) ⇒ UsernamePassword

Returns a new instance of UsernamePassword.



44
45
46
47
# File 'lib/dot_net_services/authentication.rb', line 44

def initialize(username, password, token = nil)
  @username, @password = username, password
  @token = token
end

Instance Attribute Details

#passwordObject (readonly)

:nodoc:



42
43
44
# File 'lib/dot_net_services/authentication.rb', line 42

def password
  @password
end

#tokenObject (readonly)

:nodoc:



42
43
44
# File 'lib/dot_net_services/authentication.rb', line 42

def token
  @token
end

#usernameObject (readonly)

:nodoc:



42
43
44
# File 'lib/dot_net_services/authentication.rb', line 42

def username
  @username
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



60
61
62
# File 'lib/dot_net_services/authentication.rb', line 60

def ==(other)
  other.is_a?(Authentication::UsernamePassword) && @username == other.username && @password == other.password
end

#acquire_tokenObject

Retrieve a token from the DotNetServices token issuing service.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dot_net_services/authentication.rb', line 70

def acquire_token
  http = Net::HTTP.new(DotNetServices.identity_host, 443)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  escaped_username = CGI.escape(@username)
  escaped_password = CGI.escape(@password)
  begin
    response = http.get("/issuetoken.aspx?u=#{escaped_username}&p=#{escaped_password}")
  rescue => e
    raise AuthenticationError, "Failed to obtain authentication token. Original error of type #{e.class} " +
          "was overridden to prevent logging security-sensitive data"
  end

  unless response.is_a?(Net::HTTPOK)
    raise AuthenticationError, "Failed to obtain a security token from the identity service. HTTP response was #{response.class.name}"
  end

  Token.new(response.body)
end

#authenticateObject



49
50
51
52
# File 'lib/dot_net_services/authentication.rb', line 49

def authenticate
  return if @token and not @token.expired?
  @token = acquire_token
end

#enhance(request) ⇒ Object

Enhance the request with the identity token provided by the identity service.



55
56
57
58
# File 'lib/dot_net_services/authentication.rb', line 55

def enhance(request)
  authenticate
  request['X-MS-Identity-Token'] = token.value
end

#hashObject



65
66
67
# File 'lib/dot_net_services/authentication.rb', line 65

def hash
  @hash ||= @username.hash & @password.hash
end