From 78eb7b5da876e730c7ee21a4413f599af584d300 Mon Sep 17 00:00:00 2001 From: cryzed Date: Fri, 3 Nov 2017 02:43:33 +0100 Subject: [PATCH] Select pass candidates for the fully-qualified domain name first, then for the registered domain and finally the IPv4 address if that is what the URL was --- misc/userscripts/qute-pass | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/misc/userscripts/qute-pass b/misc/userscripts/qute-pass index 19dae9d4a..6e6997ea5 100755 --- a/misc/userscripts/qute-pass +++ b/misc/userscripts/qute-pass @@ -50,11 +50,10 @@ stderr = functools.partial(print, file=sys.stderr) class ExitCodes(enum.IntEnum): SUCCESS = 0 - COULD_NOT_DETERMINE_URL = 1 - COULD_NOT_DETERMINE_DOMAIN = 2 - NO_PASS_CANDIDATES = 3 - COULD_NOT_MATCH_USERNAME = 4 - COULD_NOT_MATCH_PASSWORD = 5 + # 1 is reserved for general script errors + NO_PASS_CANDIDATES = 2 + COULD_NOT_MATCH_USERNAME = 3 + COULD_NOT_MATCH_PASSWORD = 4 def qute_command(command): @@ -88,20 +87,19 @@ def dmenu(items, invocation, encoding): def main(arguments): - # Domain wasn't overriden using CLI argument or found in qutebrowser environment variable - if not arguments.url: - stderr('Could not determine URL!') - return ExitCodes.COULD_NOT_DETERMINE_URL - - domain = tldextract.extract(arguments.url).registered_domain - if not domain: - stderr('Could not determine domain from URL: {!r}!'.format(arguments.url)) - return ExitCodes.COULD_NOT_DETERMINE_DOMAIN + domain = tldextract.extract(arguments.url) # Expand potential ~ in paths, since this script won't be called from a shell that does it for us - candidates = find_pass_candidates(domain, os.path.expanduser(arguments.password_store)) - if not candidates: - stderr('No candidates for domain {!r} found!'.format(domain)) + password_store_path = os.path.expanduser(arguments.password_store) + + # Try to find candidates using targets in the following order: fully qualified domain (including subdomains), + # registered domain and finally the IPv4 address if that's what the URL was + for target in filter(None, [domain.fqdn, domain.registered_domain, domain.ipv4]): + candidates = find_pass_candidates(target, password_store_path) + if candidates: + break + else: + stderr('No pass candidates for URL {!r} found!'.format(arguments.url)) return ExitCodes.NO_PASS_CANDIDATES selection = candidates[0] if len(candidates) == 1 else dmenu(candidates, arguments.dmenu_invocation,