Class: Passwordstate::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/passwordstate/client.rb

Constant Summary collapse

USER_AGENT =
"RubyPasswordstate/#{Passwordstate::VERSION}".freeze
DEFAULT_HEADERS =
{
  'accept'       => 'application/json',
  'user-agent'   => USER_AGENT
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
# File 'lib/passwordstate/client.rb', line 14

def initialize(url, options = {})
  @server_url = URI(url)
  @validate_certificate = true
  @headers = DEFAULT_HEADERS
  @auth_data = options.select { |k, _v| %i[apikey username password].include? k }
  @api_type = options.fetch(:api_type) if options.key? :api_type
end

Instance Attribute Details

#api_typeObject



26
27
28
# File 'lib/passwordstate/client.rb', line 26

def api_type
  @api_type || (auth_data.key?(:apikey) ? :api : :winapi)
end

#auth_dataObject

Returns the value of attribute auth_data.



11
12
13
# File 'lib/passwordstate/client.rb', line 11

def auth_data
  @auth_data
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/passwordstate/client.rb', line 11

def headers
  @headers
end

#server_urlObject

Returns the value of attribute server_url.



11
12
13
# File 'lib/passwordstate/client.rb', line 11

def server_url
  @server_url
end

#validate_certificateObject

Returns the value of attribute validate_certificate.



11
12
13
# File 'lib/passwordstate/client.rb', line 11

def validate_certificate
  @validate_certificate
end

Instance Method Details

#foldersObject



30
31
32
33
# File 'lib/passwordstate/client.rb', line 30

def folders
  ResourceList.new self, Passwordstate::Resources::Folder,
                   only: %i[all search post]
end

#hostsObject



35
36
37
38
# File 'lib/passwordstate/client.rb', line 35

def hosts
  ResourceList.new self, Passwordstate::Resources::Host,
                   only: %i[search post delete]
end

#inspectObject



98
99
100
# File 'lib/passwordstate/client.rb', line 98

def inspect
  "#{to_s[0..-2]} #{instance_variables.reject { |k| %i[@auth_data @http @logger].include? k }.map { |k| "#{k}=#{instance_variable_get(k).inspect}" }.join ', '}>"
end

#loggerObject



22
23
24
# File 'lib/passwordstate/client.rb', line 22

def logger
  @logger ||= Logging.logger[self]
end

#password_listsObject



44
45
46
47
# File 'lib/passwordstate/client.rb', line 44

def password_lists
  ResourceList.new self, Passwordstate::Resources::PasswordList,
                   except: %i[put delete]
end

#passwordsObject



40
41
42
# File 'lib/passwordstate/client.rb', line 40

def passwords
  ResourceList.new self, Passwordstate::Resources::Password
end

#request(method, api_path, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/passwordstate/client.rb', line 65

def request(method, api_path, options = {})
  uri = URI(server_url + "/#{api_type}/" + api_path)
  uri.query = URI.encode_www_form(options.fetch(:query)) if options.key? :query
  uri.query = nil if uri.query&.empty?

  req_obj = Net::HTTP.const_get(method.to_s.capitalize.to_sym).new uri
  if options.key? :body
    req_obj.body = options.fetch(:body)
    req_obj.body = req_obj.body.to_json unless req_obj.body.is_a?(String)
    req_obj['content-type'] = 'application/json'
  end

  req_obj.ntlm_auth(auth_data[:username], auth_data[:password]) if api_type == :winapi
  headers.each { |h, v| req_obj[h] = v }
  req_obj['APIKey'] = auth_data[:apikey] if api_type == :api

  print_http req_obj
  res_obj = http.request req_obj
  print_http res_obj

  return true if res_obj.is_a? Net::HTTPNoContent

  data = JSON.parse(res_obj.body) rescue nil
  if data
    return data if res_obj.is_a? Net::HTTPSuccess
    data = data&.first
    raise Passwordstate::HTTPError.new(res_obj.code, data&.fetch('errors', []) || [])
  else
    return res_obj.body if options.fetch(:allow_html, false)
    raise Passwordstate::PasswordstateError, 'Response was not parseable as JSON'
  end
end

#valid?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/passwordstate/client.rb', line 49

def valid?
  version
  true
rescue StandardError
  false
end

#versionObject



56
57
58
59
60
61
62
63
# File 'lib/passwordstate/client.rb', line 56

def version
  @version ||= begin
    html = request(:get, '', allow_html: true)
    version = html.find_line { |line| line.include? '<span>V</span>' }
    version = />(\d\.\d) \(Build (.+)\)</.match(version)
    "#{version[1]}.#{version[2]}"
  end
end