Class: Lastpass::Cli Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Low-level interaction with LastPass CLI

Examples:

Lastpass::Cli.( username, password:, trust: false, plaintext_key: false, force: false )
Lastpass::Cli.logout( force: false )
Lastpass::Cli.show( , clip: false, expand_multi: false, all: false, basic_regexp: false, id: false )
Lastpass::Cli.ls( group = nil, long: false, m: false, u: false )
Lastpass::Cli.add( name, username: nil, password: nil, url: nil, notes: nil, group: nil )
Lastpass::Cli.add_group( name )
Lastpass::Cli.edit( id, name: nil, username: nil, password: nil, url: nil, notes: nil, group: nil )
Lastpass::Cli.edit_group( id, name: )
Lastpass::Cli.rm( id )
Lastpass::Cli.status( quiet: false )
Lastpass::Cli.sync
Lastpass::Cli.export
Lastpass::Cli.import( csv_filename )
Lastpass::Cli.version

Constant Summary collapse

UPLOAD_QUEUE_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lastpass-api/cli.rb', line 91

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



109
110
111
112
113
# File 'lib/lastpass-api/cli.rb', line 109

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

.duplicateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

Not yet implemented

Note:

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



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

def self.duplicate
  not_implemented!
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/lastpass-api/cli.rb', line 116

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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

Note:

This does actually rename the project even though it looks like it creates a new group in Lastpass. This is because the original creds that were under this project have the group name in the name of the creds (yay for Lastpass awesomeness). So, after a project rename, newly created creds will go under the new group name. Old creds will rename in the old pseudo name.



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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



189
190
191
# File 'lib/lastpass-api/cli.rb', line 189

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

.generateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

Not yet implemented

Note:

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



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

def self.generate
  not_implemented!
end

.import(csv_filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

lpass import [CSV_FILENAME]



194
195
196
# File 'lib/lastpass-api/cli.rb', line 194

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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

Login to LastPass, opening up a global session



30
31
32
33
34
35
36
37
# File 'lib/lastpass-api/cli.rb', line 30

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



40
41
42
43
44
# File 'lib/lastpass-api/cli.rb', line 40

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

Not yet implemented

Note:

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



86
87
88
# File 'lib/lastpass-api/cli.rb', line 86

def self.mv
  not_implemented!
end

.passwdObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

Not yet implemented

Note:

lpass passwd



48
49
50
# File 'lib/lastpass-api/cli.rb', line 48

def self.passwd
  not_implemented!
end

.rm(id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



167
168
169
170
171
# File 'lib/lastpass-api/cli.rb', line 167

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

.shareObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

Not yet implemented

Note:

lpass share subcommand sharename …



200
201
202
# File 'lib/lastpass-api/cli.rb', line 200

def self.share
  not_implemented!
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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}



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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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



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

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

.syncObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

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

Note:

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



182
183
184
185
186
# File 'lib/lastpass-api/cli.rb', line 182

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

.versionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

lpass –version



205
206
207
# File 'lib/lastpass-api/cli.rb', line 205

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