Class: Lastpass::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/lastpass-api/cli.rb

Overview

Low-level interaction with LastPass CLI

Constant Summary collapse

UPLOAD_QUEUE_PATH =

TODO: Make this configurable? Or at least smarter?

'~/.lpass/upload-queue'

Class Method Summary collapse

Class Method Details

.add(name, username: nil, password: nil, url: nil, notes: nil, group: nil) ⇒ Object

Note:

lpass add [–sync=auto|now|no] [–non-interactive] [–color=auto|never|always] –username|–password|–url|–notes|–field=FIELD|–note-type=NOTETYPE NAME



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lastpass-api/cli.rb', line 71

def self.add( name, username: nil, password: nil, url: nil, notes: nil, group: nil )
  data = {}
  data[:Username] = escape( username,   double: true ) if username
  data[:Password] = escape( password,   double: true ) if password
  data[:URL]      = escape( url,        double: true ) if url
  data[:Notes] = '\n' << escape( notes, double: true ) if notes

  command = 'printf "'
  command << data.map { |d| d.join( ': ' ) }.join( '\n' )
  command << '" | lpass add --non-interactive --sync=no \''
  command << "#{escape( group )}/" if group
  command << "#{escape( name )}'"
  response = Utils.cmd command
  sync
  response
end

.add_group(name) ⇒ Object

Note:

lpass add [–sync=auto|now|no] [–non-interactive] [–color=auto|never|always] –username|–password|–url|–notes|–field=FIELD|–note-type=NOTETYPE NAME



89
90
91
92
93
# File 'lib/lastpass-api/cli.rb', line 89

def self.add_group( name )
  response = Utils.cmd "printf 'URL: http://group' | lpass add --non-interactive --sync=no '#{escape( name )}/'"
  sync
  response
end

.duplicateObject

Note:

lpass duplicate [–sync=auto|now|no] [–color=auto|never|always] UNIQUENAME|UNIQUEID



134
135
136
# File 'lib/lastpass-api/cli.rb', line 134

def self.duplicate
  not_implemented!
end

.edit(id, name: nil, username: nil, password: nil, url: nil, notes: nil, group: nil) ⇒ Object

Note:

lpass edit [–sync=auto|now|no] [–non-interactive] [–color=auto|never|always] –name|–username|–password|–url|–notes|–field=FIELD NAME|UNIQUEID



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lastpass-api/cli.rb', line 96

def self.edit( id, name: nil, username: nil, password: nil, url: nil, notes: nil, group: nil )
  data = {}
  name_with_group = ''
  name_with_group << "#{group}/" if group
  name_with_group << name if name

  data[:Name]     = escape( name_with_group, double: true ) unless name_with_group == ''
  data[:Username] = escape( username,        double: true ) if username
  data[:Password] = escape( password,        double: true ) if password
  data[:URL]      = escape( url,             double: true ) if url
  data[:Notes] = '\n' << escape( notes,      double: true ) if notes

  command = 'printf "'
  command << data.map { |d| d.join( ': ' ) }.join( '\n' )
  command << '" | lpass edit --non-interactive --sync=no '
  command << id
  response = Utils.cmd command
  sync
  response
end

.edit_group(id, name:) ⇒ Object

Note:

lpass edit [–sync=auto|now|no] [–non-interactive] [–color=auto|never|always] –name|–username|–password|–url|–notes|–field=FIELD NAME|UNIQUEID



118
119
120
121
122
123
124
125
126
# File 'lib/lastpass-api/cli.rb', line 118

def self.edit_group( id, name: )
  command = 'printf "'
  command << "Name: #{escape( name, double: true )}/"
  command << '" | lpass edit --non-interactive --sync=no '
  command << id
  response = Utils.cmd command
  sync
  response
end

.exportObject

Note:

lpass export [–sync=auto|now|no] [–color=auto|never|always]



161
162
163
# File 'lib/lastpass-api/cli.rb', line 161

def self.export
  Utils.cmd 'lpass export'
end

.generateObject

Note:

lpass generate [–sync=auto|now|no] [–clip, -c] [–username=USERNAME] [–url=URL] [–no-symbols] NAME|UNIQUEID LENGTH



129
130
131
# File 'lib/lastpass-api/cli.rb', line 129

def self.generate
  not_implemented! # TODO
end

.import(csv_filename) ⇒ Object

Note:

lpass import [CSV_FILENAME]



166
167
168
# File 'lib/lastpass-api/cli.rb', line 166

def self.import( csv_filename )
  Utils.cmd "lpass import '#{escape( csv_filename )}'"
end

.login(username, password:, trust: false, plaintext_key: false, force: false) ⇒ Object

Note:

lpass login [–trust] [–plaintext-key [–force, -f]] [–color=auto|never|always] USERNAME

Login to LastPass, opening up a global session



12
13
14
15
16
17
18
19
# File 'lib/lastpass-api/cli.rb', line 12

def self.( username, password:, trust: false, plaintext_key: false, force: false )
  command = "echo '#{password}' | LPASS_DISABLE_PINENTRY=1 lpass login"
  command << ' --trust' if trust
  command << ' --plaintext-key' if plaintext_key
  command << ' --force' if force
  command << " '#{escape( username )}'"
  Utils.cmd command
end

.logout(force: false) ⇒ Object

Note:

lpass logout [–force, -f] [–color=auto|never|always]



22
23
24
25
26
# File 'lib/lastpass-api/cli.rb', line 22

def self.logout( force: false )
  command = 'echo "Y" | lpass logout'
  command << ' --force' if force
  Utils.cmd command
end

.ls(group = nil, long: false, m: false, u: false) ⇒ Object

Note:

lpass ls [–sync=auto|now|no] [–long, -l] [-m] [-u] [–color=auto|never|always] [GROUP]



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lastpass-api/cli.rb', line 51

def self.ls( group = nil, long: false, m: false, u: false )
  sync # Ensure everything is synced up before running!
  command = 'lpass ls'
  command << ' --long' if long
  command << ' -m' if m
  command << ' -u' if u
  command << " '#{escape( group )}'" if group
  response = Utils.cmd command
  # Don't let LastPass know the accounts were accessed as it clogs up the sync!
  # So clear out sync files if a lot of accounts were accessed at the same time.
  remove_sync_files if response.length > 400
  response
end

.mvObject

Note:

lpass mv [–color=auto|never|always] UNIQUENAME|UNIQUEID GROUP



66
67
68
# File 'lib/lastpass-api/cli.rb', line 66

def self.mv
  not_implemented!
end

.passwdObject

Note:

lpass passwd



29
30
31
# File 'lib/lastpass-api/cli.rb', line 29

def self.passwd
  not_implemented!
end

.rm(id) ⇒ Object

Note:

lpass rm [–sync=auto|now|no] [–color=auto|never|always] UNIQUENAME|UNIQUEID



139
140
141
142
143
# File 'lib/lastpass-api/cli.rb', line 139

def self.rm( id )
  response = Utils.cmd "lpass rm --sync=no #{id}"
  sync
  response
end

.shareObject

Note:

lpass share subcommand sharename …



171
172
173
# File 'lib/lastpass-api/cli.rb', line 171

def self.share
  not_implemented!
end

.show(account, clip: false, expand_multi: false, all: false, basic_regexp: false, id: false) ⇒ Object

Note:

lpass show [–sync=auto|now|no] [–clip, -c] [–expand-multi, -x] [–all|–username|–password|–url|–notes|–field=FIELD|–id|–name|–attach=ATTACHID] [–basic-regexp, -G|–fixed-strings, -F] [–color=auto|never|always] UNIQUENAME|UNIQUEID



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lastpass-api/cli.rb', line 34

def self.show( , clip: false, expand_multi: false, all: false, basic_regexp: false, id: false )
  sync # Ensure everything is synced up before running!
  command = 'lpass show'
  command << ' --clip' if clip
  command << ' --expand-multi' if expand_multi
  command << ' --all' if all
  command << ' --basic-regexp' if basic_regexp
  command << ' --id' if id
  command << " '#{escape(  )}'"
  response = Utils.cmd command
  # Don't let LastPass know the accounts were accessed as it clogs up the sync!
  # So clear out sync files if a lot of accounts were accessed at the same time.
  remove_sync_files if response.length > 400
  response
end

.status(quiet: false) ⇒ Object

Note:

lpass status [–quiet, -q] [–color=auto|never|always]



146
147
148
149
150
# File 'lib/lastpass-api/cli.rb', line 146

def self.status( quiet: false )
  command = 'lpass status'
  command << ' --quiet' if quiet
  Utils.cmd command
end

.syncObject

Note:

This is a buggy function of the lpass executable. May not be super reliable.

Note:

lpass sync [–background, -b] [–color=auto|never|always]



154
155
156
157
158
# File 'lib/lastpass-api/cli.rb', line 154

def self.sync
  sleep 1 # Allow file IO before attempting sync
  Utils.cmd 'lpass sync'
  sleep 1 # Allow sync to finish
end

.versionObject

Note:

lpass –version



176
177
178
# File 'lib/lastpass-api/cli.rb', line 176

def self.version
  Utils.cmd 'lpass --version'
end