Repository: wandera/1password-client

Issue:
Login fails: "Data must be aligned to block boundary in ECB mode"
Hi, logging into my 1password account fails with the ValueError message above:

`from onepassword import OnePassword
op = OnePassword()
1Password master password: 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/lib/python3.9/site-packages/onepassword/client.py", line 60, in __init__
    self.encrypted_master_password, self.session_key = self.signin_wrapper(master_password=password)
  File "/opt/homebrew/lib/python3.9/site-packages/onepassword/client.py", line 179, in signin_wrapper
    encrypted_str = encrypt.encode(password)
  File "/opt/homebrew/lib/python3.9/site-packages/onepassword/utils.py", line 173, in encode
    return base64.b64encode(self.cipher.encrypt(input_str.rjust(32)))
  File "/opt/homebrew/lib/python3.9/site-packages/Crypto/Cipher/_mode_ecb.py", line 141, in encrypt
    raise ValueError("Data must be aligned to block boundary in ECB mode")
ValueError: Data must be aligned to block boundary in ECB mode`

Python 3.9.2, op 1.8.0, 1password-client 0.3.0

Best regards, Jan

Discussion:

Patch:
diff --git a/onepassword/utils.py b/onepassword/utils.py
index f37313d..733612b 100644
--- a/onepassword/utils.py
+++ b/onepassword/utils.py
@@ -1,6 +1,10 @@
 import os
 import base64
 from Crypto.Cipher import AES
+from Crypto.Util.Padding import pad
+
+
+BLOCK_SIZE = 32 # Bytes
 
 
 def read_bash_return(cmd, single=True):
@@ -128,14 +132,14 @@ class BashProfile:
 
 class Encryption:
     def __init__(self, secret_key):
-        self.secret_key = secret_key[0:32]
+        self.secret_key = secret_key[0:BLOCK_SIZE]
         self.cipher = AES.new(self.secret_key, AES.MODE_ECB)
 
     def decode(self, encoded):
-        return self.cipher.decrypt(base64.b64decode(encoded)).decode('UTF-8').replace(" ", "")
+        return self.cipher.decrypt(base64.b64decode(encoded)).decode('UTF-8').replace("\x1f", "")
 
     def encode(self, input_str):
-        return base64.b64encode(self.cipher.encrypt(input_str.rjust(32)))
+        return base64.b64encode(self.cipher.encrypt(pad(input_str, BLOCK_SIZE)))
 
 
 def bump_version():


Test patch:
diff --git a/test/test_client.py b/test/test_client.py
index f27454d..281d14f 100644
--- a/test/test_client.py
+++ b/test/test_client.py
@@ -12,7 +12,7 @@ def set_up_one_password():
     domain = "test"
     email = "user@test.com"
     secret = "test_secret"
-    password = "test_password"
+    password = "a234567890b234567890c234567890d234567890e23"
     account = "test"
     with open('.bash_profile', 'w') as f:
         f.write("OP_SESSION_test=fakelettersforsessionkey")