lib/fog/proxmox.rb in fog-proxmox-0.8.1 vs lib/fog/proxmox.rb in fog-proxmox-0.8.2

- old
+ new

@@ -45,18 +45,19 @@ class << self attr_reader :credentials attr_reader :version attr_accessor :now # tests only + attr_accessor :ticket_lifetime # tests only end def self.clear_credentials @credentials = {} end def self.authenticate(options, connection_options = {}) - get_tokens(options, connection_options) + get_credentials(options, connection_options) self end def self.authenticated? !@credentials.empty? @@ -69,27 +70,27 @@ def self.extract_password(options) ticket = options[:pve_ticket] options[:pve_password] = ticket unless ticket end - def self.get_tokens(options, connection_options = {}) + def self.get_credentials(options, connection_options = {}) username = options[:pve_username].to_s password = options[:pve_password].to_s url = options[:pve_url] extract_password(options) uri = URI.parse(url) @api_path = uri.path connection_options = connection_options.merge(path_prefix: @api_path) - password = @credentials[:csrftoken] if credentials_has_expired? - retrieve_tokens(uri, connection_options, username, password) unless authenticated? && !credentials_has_expired? + password = @credentials[:ticket] if credentials_has_expired? + request_credentials(uri, connection_options, username, password) unless authenticated? && !credentials_has_expired? end - def self.retrieve_tokens(uri, connection_options, username, password) + def self.request_credentials(uri, connection_options, username, password) request = { expects: [200, 204], headers: { 'Accept' => 'application/json' }, - body: "username=#{username}&password=#{password}", + body: URI.encode_www_form(username: username, password: password), method: 'POST', path: 'access/ticket' } connection = Fog::Core::Connection.new( uri.to_s, @@ -100,13 +101,13 @@ data = Json.get_data(response) ticket = data['ticket'] username = data['username'] csrftoken = data['CSRFPreventionToken'] deadline = Time.at(@now.to_i + @ticket_lifetime) - save_token(username, ticket, csrftoken, deadline) + save_credentials(username, ticket, csrftoken, deadline) end - def self.save_token(username, ticket, csrftoken, deadline) + def self.save_credentials(username, ticket, csrftoken, deadline) @credentials = { username: username, ticket: ticket, csrftoken: csrftoken, deadline: deadline