1- #!/usr/bin/env python3
1+ """
2+ chrome_pass native application module
3+ """
24
3- # Requires python-gpg library
45import os
56import re
67import sys
910import shutil
1011import difflib
1112import pathlib
13+ import posixpath
1214from urllib .parse import parse_qs
1315from urllib .parse import urlparse
1416from collections import OrderedDict
17+ from importlib .metadata import entry_points
1518import pyotp
1619import gnupg
1720
1821if sys .platform == "win32" :
1922 # Interacts with windows registry to register this app
20- import winreg
23+ import winreg # pylint: disable=import-error
2124 # On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY
2225 # to avoid unwanted modifications of the input/output streams.
23- import msvcrt
26+ import msvcrt # pylint: disable=import-error
2427 msvcrt .setmode (sys .stdin .fileno (), os .O_BINARY )
2528 msvcrt .setmode (sys .stdout .fileno (), os .O_BINARY )
2629
@@ -97,13 +100,16 @@ def read_data(path):
97100 raise RuntimeError (f'Failed to decrypt { txt } ' )
98101
99102
100- # Returns a dictionary with id and value pairs read from pass files that match
101- # pattern:
102- #
103- # key=value
104- #
105- # key only matches alphanumeric characters and cannot have spaces.
106103def get_creds (path ):
104+ """
105+ Returns a dictionary with id and value pairs read from pass files
106+ that match pattern:
107+
108+ key=value
109+
110+ key only matches alphanumeric characters and cannot have spaces.
111+ """
112+
107113 # Read decripted pass file data.
108114 data = read_data (path ).decode ('utf-8' ).split ("\n " )
109115
@@ -131,17 +137,22 @@ def get_creds(path):
131137 return creds
132138
133139
134- # Sends the response message with the format that chrome HostNativeApplications
135- # expect.
136140def send_message (message ):
141+ """
142+ Sends response messages in format compatible with chrome
143+ HostNativeApplications.
144+ """
137145 response = json .dumps (message ).encode ('utf-8' )
138146 sys .stdout .buffer .write (struct .pack ('I' , len (response )))
139147 sys .stdout .buffer .write (response )
140148 sys .stdout .buffer .flush ()
141149
142150
143- # Method that implements Chrome Native App protocol for messaging.
144151def process_native ():
152+ """
153+ Method that implements Chrome Native App protocol to enable
154+ communication between chrome-pass chrome extension and pass.
155+ """
145156 size = sys .stdin .buffer .read (4 )
146157
147158 if not size :
@@ -173,21 +184,28 @@ def process_native():
173184 send_message ({"action" : "error" , "msg" : sys .exc_info ()[0 ]})
174185
175186
176- # Method prints to stdout the list of passwords ordered by a similarty pattern
177187def print_list (pattern ):
188+ """
189+ Method prints to stdout the list of passwords ordered by a similarty
190+ pattern
191+ """
178192 for credential in get_list (pattern )[:20 ]:
179193 print (credential )
180194
181195
182- # Method prints to stdout the first match creds data.
183196def print_creds (pattern ):
197+ """
198+ Method prints to stdout the first match creds data.
199+ """
184200 for credential in get_list (pattern )[:20 ]:
185201 account = credential [0 ] + "/" + credential [2 ]
186202 print (f'{ get_creds (account )} ' )
187203
188204
189- # Determines the path were the native app manifest should be installed.
190205def native_path_chrome ():
206+ """
207+ Determines the path were the native app manifest should be installed.
208+ """
191209 if sys .platform == "darwin" :
192210 return os .path .expanduser (
193211 '~'
@@ -228,8 +246,23 @@ def native_path_brave():
228246 sys .exit (1 )
229247
230248
231- # Installs the Native Host Application manifest for this script into Chrome.
249+ def find_chrome_pass_path ():
250+ """
251+ Convoluted function to figure out the absolute path of the chrome_pass
252+ console script.
253+ """
254+ entry_point = entry_points ().select (
255+ name = 'chrome_pass' , group = 'console_scripts' )[0 ]
256+ package_path = list (filter (
257+ lambda file : file .name == "chrome_pass" ,
258+ entry_point .dist .files ))[0 ]
259+ return posixpath .abspath (package_path .locate ())
260+
261+
232262def install (native_path , extension_id ):
263+ """
264+ Installs the Native Host Application manifest for this script into Chrome.
265+ """
233266 if sys .platform == "win32" :
234267 # Appends APPDATA to native_path and set this path as a registry value
235268 reg_key = os .path .join ("Software" , native_path )
@@ -242,14 +275,14 @@ def install(native_path, extension_id):
242275 os .makedirs (native_path )
243276
244277 if sys .platform == "win32" :
245- batch = "python \" {} \" %*" . format ( os .path .realpath (__file__ ))
278+ batch = f "python \" { os .path .realpath (__file__ )} \" %*"
246279 native_app = EXTENSION_NAME + '.bat'
247280 outfile = os .path .join (native_path , native_app )
248281 with open (outfile , 'w' , encoding = "utf-8" ) as file :
249282 file .write ("@echo off\n \n " )
250283 file .write (batch )
251284 else :
252- native_app = os . path . realpath ( __file__ )
285+ native_app = find_chrome_pass_path ( )
253286
254287 manifest = OrderedDict ()
255288 manifest ['name' ] = EXTENSION_NAME
@@ -264,22 +297,23 @@ def install(native_path, extension_id):
264297 json .dump (manifest , file , indent = '\t ' )
265298
266299
267- if len (sys .argv ) > 1 :
268- if sys .argv [1 ].startswith ('chrome-extension://' ):
269- process_native ()
270- elif sys .argv [1 ] == "install" :
271- if len (sys .argv ) > 2 :
272- install (native_path_chrome (), sys .argv [2 ])
273- install (native_path_chromium (), sys .argv [2 ])
274- install (native_path_brave (), sys .argv [2 ])
300+ def run ():
301+ if len (sys .argv ) > 1 :
302+ if sys .argv [1 ].startswith ('chrome-extension://' ):
303+ process_native ()
304+ elif sys .argv [1 ] == "install" :
305+ if len (sys .argv ) > 2 :
306+ install (native_path_chrome (), sys .argv [2 ])
307+ install (native_path_chromium (), sys .argv [2 ])
308+ install (native_path_brave (), sys .argv [2 ])
309+ else :
310+ install (native_path_chrome (), EXTENSION_ID )
311+ install (native_path_chromium (), EXTENSION_ID )
312+ install (native_path_brave (), EXTENSION_ID )
313+ elif sys .argv [1 ] == "pass" :
314+ if len (sys .argv ) > 2 :
315+ print_creds (sys .argv [2 ])
316+ elif sys .argv [1 ] == "gpgbin" :
317+ print (f"GPG Binary path: { get_gpg_bin ()} " )
275318 else :
276- install (native_path_chrome (), EXTENSION_ID )
277- install (native_path_chromium (), EXTENSION_ID )
278- install (native_path_brave (), EXTENSION_ID )
279- elif sys .argv [1 ] == "pass" :
280- if len (sys .argv ) > 2 :
281- print_creds (sys .argv [2 ])
282- elif sys .argv [1 ] == "gpgbin" :
283- print (f"GPG Binary path: { get_gpg_bin ()} " )
284- else :
285- print_list (sys .argv [1 ])
319+ print_list (sys .argv [1 ])
0 commit comments