Something went wrong. Try again.
Personal configuration files
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889#!/usr/bin/env python3import argparseimport subprocessimport sysfrom enum import Enumfrom typing import TYPE_CHECKING, castclass Operation(Enum): ERASE = "erase" GET = "get" STORE = "store"if TYPE_CHECKING: class Args(argparse.Namespace): account: "str" item: "str" operation: "Operation"parser = argparse.ArgumentParser(description="1Password credential helper for Git.")parser.add_argument( "-a", "--account", type=str, help="account to use", default="my.1password.com",)parser.add_argument( "-v", "--vault", type=str, help="vault to interact with", default="Personal",)parser.add_argument( "item", type=str, help="item to retrieve",)parser.add_argument( "operation", type=Operation, help="argument provided by Git (get|store|erase)")args = cast("Args", parser.parse_args())if args.operation != Operation.GET: # We don't care about store or erase at the moment. print(f"Ignoring {args.operation} directive. Use 1Password directly.") sys.exit(0)params: "dict[str, str]" = {}for line in sys.stdin.readlines(): parts = line.split("=", maxsplit=1) # If we are handed a line like "password=", skip it. if not parts[0]: continue params[parts[0]] = parts[1].rstrip()proc = subprocess.run( [ "op", "--account", args.account, "read", args.item, ], check=True, encoding="utf-8", stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,)proc.check_returncode()token = proc.stdout.strip()if params["protocol"] in ("smtp", "smtps"): print(f"password={token}")elif params["protocol"] == "https": print(f"username={params.get('username', 'git')}") print(f"password={token}")else: sys.exit(1)