Skip to content

Commit 6d75a9e

Browse files
committed
Add LPASS_CONFIG_FILE and LPASS_BLOB_FILE variables.
Add LPASS_USER and LPASS_PASSWORD variables. Fix pycodestyle issues.
1 parent fffe7f8 commit 6d75a9e

5 files changed

Lines changed: 30 additions & 11 deletions

File tree

lastpass/account.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def fields(self):
3232
def __str__(self):
3333
return "name: {}\n\tusername: {}\n\tpassword: {}\n\turl: {}\n\tgroup: {}\n\tnotes: {}".format(self.name, self.username, self.password, self.url, self.group, self.notes_string())
3434

35+
3536
class SecureNote(Account):
3637
"""
3738
Lastpass Secure Note
@@ -43,4 +44,4 @@ def __str__(self):
4344
try:
4445
return getattr(self, 'unparsed_notes_0').decode()
4546
except AttributeError:
46-
return '\n'.join( [ '\t\t{}: {}'.format(field, getattr(self, field).decode() ) for field in self.fields() ] )
47+
return '\n'.join(['\t\t{}: {}'.format(field, getattr(self, field).decode()) for field in self.fields()])

lastpass/fetcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def request_iteration_count(username, web_client=http):
6060

6161
try:
6262
count = int(response.content)
63-
except:
63+
except Exception:
6464
raise InvalidResponseError('Key iteration count is invalid')
6565

6666
if count > 0:

lastpass/parser.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
compress = zlib.compress
4444
decompress = zlib.decompress
4545

46+
4647
def extract_chunks(blob):
4748
"""Splits the blob into chucks grouped by kind."""
4849
chunks = []
@@ -150,7 +151,7 @@ def parse_secure_note_server(notes):
150151
info[last_field] = b''
151152
if last_field:
152153
old_bytes = info[last_field]
153-
info[last_field] = (old_bytes.decode()+'\n'+line.decode()).encode()
154+
info[last_field] = (old_bytes.decode() + '\n' + line.decode()).encode()
154155
continue
155156

156157
# Split only once so that strings like "Hostname:host.example.com:80"
@@ -242,6 +243,7 @@ def encode_base64(data):
242243
b64data = b64encode(data)
243244
return b64data
244245

246+
245247
def decode_aes256_plain_auto(data, encryption_key):
246248
"""Guesses AES cipher (EBC or CBD) from the length of the plain data."""
247249
assert isinstance(data, bytes)
@@ -322,10 +324,20 @@ def pad(data):
322324
# see http://passingcuriosity.com/2009/aes-encryption-in-python-with-m2crypto/
323325
padded = (BS - len(data) % BS) * chr(BS - len(data) % BS)
324326
if isinstance(data, str):
325-
data = str.encode(data, 'latin1')
327+
try:
328+
data = str.encode(data, 'latin1')
329+
except Exception:
330+
data = bytes(data)
326331
if isinstance(padded, str):
327-
padded = str.encode(padded, 'latin1')
328-
return data + padded
332+
try:
333+
padded = str.encode(padded, 'latin1')
334+
except Exception:
335+
padded = bytes(data)
336+
try:
337+
result = bytes(data + padded)
338+
except Exception:
339+
result = data + padded
340+
return result
329341

330342

331343
def unpad(data):
@@ -334,9 +346,13 @@ def unpad(data):
334346
"""
335347
# see http://passingcuriosity.com/2009/aes-encryption-in-python-with-m2crypto/
336348
if isinstance(data, str):
337-
data = str.encode(data, 'latin1')
349+
try:
350+
data = str.encode(data, 'latin1')
351+
except Exception:
352+
data = bytes(data)
338353
return data[0:-ord(data[-1:])]
339354

355+
340356
def decode_aes256(cipher, iv, data, encryption_key):
341357
"""
342358
Decrypt AES-256 bytes.

lastpass/vault.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from . import parser
44
from .exceptions import InvalidResponseError
55

6+
67
class Vault(object):
78
"""
89
Lastpass Vault

scripts/lpass

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ LastPass CLI in Python. Very Minimal.
3636
---------------------------------------------
3737
"""
3838

39+
from __future__ import print_function
3940
import argparse
4041
import getpass
4142
import json
@@ -68,8 +69,8 @@ S_WHITE = '\033[37m'
6869
S_RESET = '\033[0m'
6970

7071
LPASS_ENCRYPTION_KEY = '1234567890ABCDEF'
71-
LPASS_CONFIG_FILE = os.path.expanduser("~/.lpass-config")
72-
LPASS_BLOB_FILE = os.path.expanduser("~/.lpass-blob")
72+
LPASS_CONFIG_FILE = os.getenv('LPASS_CONFIG_FILE', os.path.expanduser("~/.lpass-config"))
73+
LPASS_BLOB_FILE = os.getenv('LPASS_BLOB_FILE', os.path.expanduser("~/.lpass-blob"))
7374
CLIENT_ID = None # Usually the IMEI.
7475

7576

@@ -138,7 +139,7 @@ def command_login(args):
138139
if os.path.exists(LPASS_CONFIG_FILE):
139140
if not args.force:
140141
raise Exception('Unable to Overwrite Configuration.')
141-
(_, password) = __get_login(args.username, None, args.mfa)
142+
(_, password) = __get_login(args.username, os.getenv('LPASS_PASSWORD', None), args.mfa)
142143
__print_message('Logged into {}'.format(__colored(args.username, S_YELLOW)))
143144
__write_config(args.username, password, args.mfa)
144145

@@ -273,7 +274,7 @@ def main():
273274
parser_login.add_argument('--plaintext-key', default=True, action='store_true')
274275
parser_login.add_argument('--mfa', default=False, action='store_true')
275276
parser_login.add_argument('--force', '-f', default=False, action='store_true')
276-
parser_login.add_argument('username', type=str, default=None)
277+
parser_login.add_argument('username', type=str, default=os.getenv('LPASS_USER', None))
277278
parser_login.set_defaults(func=command_login)
278279

279280
parser_logout = subparsers.add_parser('logout', help='Logout from Lasspass.')

0 commit comments

Comments
 (0)