summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorafreakk <laderud@hotmail.com>2021-04-16 17:12:09 +0200
committerFlorian Bruhin <me@the-compiler.org>2021-04-19 11:28:05 +0200
commitfcb483d36f5efbfb0fca331acbc8b392e512cc82 (patch)
treeb2eb769d7892a223a3ccbe5d498bc44130e27204
parentd64d59502ab219077abed8f8b9ac12fc3e3bc35c (diff)
downloadqutebrowser-fcb483d36f5efbfb0fca331acbc8b392e512cc82.tar.gz
qutebrowser-fcb483d36f5efbfb0fca331acbc8b392e512cc82.zip
qute-pass: extract username/pw only when needed
(cherry picked from commit efcb3798729b6dedbf63baa443837720df63ce29)
-rwxr-xr-xmisc/userscripts/qute-pass76
1 files changed, 48 insertions, 28 deletions
diff --git a/misc/userscripts/qute-pass b/misc/userscripts/qute-pass
index cf65fe045..05297c94b 100755
--- a/misc/userscripts/qute-pass
+++ b/misc/userscripts/qute-pass
@@ -109,6 +109,14 @@ class ExitCodes(enum.IntEnum):
COULD_NOT_MATCH_PASSWORD = 4
+class CouldNotMatchUsername(Exception):
+ pass
+
+
+class CouldNotMatchPassword(Exception):
+ pass
+
+
def qute_command(command):
with open(os.environ['QUTE_FIFO'], 'w') as fifo:
fifo.write(command + '\n')
@@ -174,6 +182,26 @@ def fake_key_raw(text):
qute_command('fake-key {}'.format(sequence))
+def extract_password(secret, pattern):
+ match = re.match(pattern, secret)
+ if not match:
+ raise CouldNotMatchPassword("Pattern did not match target")
+ try:
+ return match.group(1)
+ except IndexError:
+ raise CouldNotMatchPassword("Pattern did not contain capture group, please use capture group. Example: (.*)")
+
+
+def extract_username(target, pattern):
+ match = re.search(pattern, target, re.MULTILINE)
+ if not match:
+ raise CouldNotMatchUsername("Pattern did not match target")
+ try:
+ return match.group(1)
+ except IndexError:
+ raise CouldNotMatchUsername("Pattern did not contain capture group, please use capture group. Example: (.*)")
+
+
def main(arguments):
if not arguments.url:
argument_parser.print_help()
@@ -216,35 +244,27 @@ def main(arguments):
secret = None
if not (arguments.username_target == 'path' and arguments.username_only):
secret = pass_(selection)
-
- # Match password
- match = re.match(arguments.password_pattern, secret)
- if not match:
- stderr('Failed to match password pattern on secret!')
- return ExitCodes.COULD_NOT_MATCH_PASSWORD
- password = match.group(1)
-
- # Match username
- target = selection if arguments.username_target == 'path' else secret
- match = re.search(arguments.username_pattern, target, re.MULTILINE)
- if not match:
- stderr('Failed to match username pattern on {}!'.format(arguments.username_target))
+ username_target = selection if arguments.username_target == 'path' else secret
+ try:
+ if arguments.username_only:
+ fake_key_raw(extract_username(username_target, arguments.username_pattern))
+ elif arguments.password_only:
+ fake_key_raw(extract_password(secret, arguments.password_pattern))
+ elif arguments.otp_only:
+ otp = pass_otp(selection)
+ fake_key_raw(otp)
+ else:
+ # Enter username and password using fake-key and <Tab> (which seems to work almost universally), then switch
+ # back into insert-mode, so the form can be directly submitted by hitting enter afterwards
+ fake_key_raw(extract_username(username_target, arguments.username_pattern))
+ qute_command('fake-key <Tab>')
+ fake_key_raw(extract_password(secret, arguments.password_pattern))
+ except CouldNotMatchPassword as e:
+ stderr('Failed to match password, target: secret, error: {}'.format(e))
+ return ExitCodes.COULD_NOT_MATCH_PASSWORD
+ except CouldNotMatchUsername as e:
+ stderr('Failed to match username, target: {}, error: {}'.format(arguments.username_target, e))
return ExitCodes.COULD_NOT_MATCH_USERNAME
- username = match.group(1)
-
- if arguments.username_only:
- fake_key_raw(username)
- elif arguments.password_only:
- fake_key_raw(password)
- elif arguments.otp_only:
- otp = pass_otp(selection)
- fake_key_raw(otp)
- else:
- # Enter username and password using fake-key and <Tab> (which seems to work almost universally), then switch
- # back into insert-mode, so the form can be directly submitted by hitting enter afterwards
- fake_key_raw(username)
- qute_command('fake-key <Tab>')
- fake_key_raw(password)
if arguments.insert_mode:
qute_command('mode-enter insert')