class Opr21

Constants

CREDENTIALS

set initial credentials if you want to …

Attributes

debug[W]
expiration_date[R]
password[R]
tempfile[R]

Public Class Methods

new(password, hash={}) click to toggle source
# File lib/opr21.rb, line 18
def initialize(password, hash={})
  @password = password
  @debug = false
  @tempfile = Tempfile.new('op')
  check_creds(hash)
end

Public Instance Methods

getItem(name, fields = "website,username,title,password", vault = "Private") click to toggle source

gibt die Werte des ersten Elementes mit entsprechenden Namen (exakter Match case ignore)

# File lib/opr21.rb, line 35
def getItem(name, fields = "website,username,title,password", vault = "Private")
  all = self.op "list", "items", "--vault", vault
  arr = all.select{|i| i["overview"]["title"].casecmp(name) == 0 }
  if arr.class == Array
      self.op "get", "item", arr[0]["uuid"], "--fields", fields
  else
      {}
  end
end
getItemList(name, vault = "Private") click to toggle source

gibt eine Liste von item.titles aus die matchen

# File lib/opr21.rb, line 46
def getItemList(name, vault = "Private")
  regex = %r{#{name}}i
  all = self.op "list", "items", "--vault", vault
  arr = all.select{|i| regex.match?(i["overview"]["title"])}
  return_arr = []
  if arr.class == Array
      arr.each do |item|
        return_arr << item["overview"]["title"]
      end
  end
  return_arr
end
op(*args) click to toggle source
# File lib/opr21.rb, line 25
def op(*args)
  check_creds()
  cmd = %(. #{tempfile.path} ; op "#{args.join('" "')}")
  puts cmd if @debug
  output = `#{cmd}`
  puts output if @debug
  JSON.parse(output)
end

Private Instance Methods

check_creds(hash={}) click to toggle source
# File lib/opr21.rb, line 65
def check_creds(hash={})
  hash = CREDENTIALS.update hash
  if system(". #{tempfile.path} ;op get account > /dev/null 2>&1")
      # noch ok
      @expiration_date = Time.at(Time.now.to_i + 30*60)
  else
      # neu einloggen
      system("echo '#{password}' | op signin #{CREDENTIALS[:domain]} #{CREDENTIALS[:email]} #{CREDENTIALS[:secret]} > #{@tempfile.path}")
      puts "session saved in #{tempfile.path}" if @debug
  end
end