Exemple #1
0
def main():
    parser = argparse.ArgumentParser(
        description='NatWest password management tool.'
    )
    parser.add_argument(
        '--pin',
        nargs=3,
        type=arg_validator,
        help='A space separated list of entries, such as 1 3 2.'
    )
    parser.add_argument(
        '--password',
        nargs=3,
        type=arg_validator,
        help='A space separate list of entries, such as 2 4 19.'
    )
    args = parser.parse_args()
    if not (args.pin or args.password):
        parser.print_help()
        sys.exit(1)

    print_greeting()

    keychain_path = find_keychain()
    if not keychain_path:
        print 'Unable to find keychain. Exiting'
        sys.exit(1)

    keychain = Keychain(keychain_path)
    master_password = getpass('Please enter your master password:'******'Unable to unlock keychain.'
        sys.exit(1)

    if args.pin:
        try:
            nw_pin = keychain.item(settings.PIN_ITEM).password
        except ValueError:
            print 'Unable to find item {} in the keychain.'.format(settings.PIN_ITEM)
            sys.exit(1)

        print_pin_splash()

        for i in args.pin:
            print_generator(nw_pin, i)

    if args.password:
        try:
            nw_password = keychain.item(settings.PASSWORD_ITEM).password
        except ValueError:
            print 'Unable to find item {} in the keychain.'.format(settings.PASSWORD_ITEM)
            sys.exit(1)

        print_password_splash()

        for i in args.password:
            print_generator(nw_password, i)
Exemple #2
0
    def test_unlock_and_read_web_form_password(self):
        keychain = Keychain(path=self.keychain_path)

        unlock_result = keychain.unlock("wrong-password".encode("utf-8"))
        self.assertFalse(unlock_result)

        unlock_result = keychain.unlock("badger".encode("utf-8"))
        self.assertTrue(unlock_result)

        self.assertIsNone(keychain.item("does-not-exist"))
        self.assertEquals("123456", keychain.item("onetosix").password)
        self.assertEquals("abcdef", keychain.item("atof").password)
Exemple #3
0
    def test_unlock_and_read_web_form_password(self):
        keychain = Keychain(path=self.keychain_path)

        unlock_result = keychain.unlock("wrong-password")
        self.assertFalse(unlock_result)

        unlock_result = keychain.unlock("badger")
        self.assertTrue(unlock_result)

        self.assertIsNone(keychain.item("does-not-exist"))
        self.assertEquals("123456", keychain.item("onetosix").password)
        self.assertEquals("abcdef", keychain.item("atof").password)
Exemple #4
0
    def test_unlock_and_read_web_form_password(self):
        path = os.path.join(os.path.dirname(__file__), "data", "1Password.agilekeychain")
        keychain = Keychain(path=path)

        unlock_result = keychain.unlock("wrong-password")
        self.assertFalse(unlock_result)

        unlock_result = keychain.unlock("badger")
        self.assertTrue(unlock_result)

        self.assertIsNone(keychain.item("does-not-exist"))
        self.assertEquals("123456", keychain.item("onetosix").password)
        self.assertEquals("abcdef", keychain.item("atof").password)
Exemple #5
0
    def test_unlock_and_read_with_fuzzy_matching(self):
        keychain = Keychain(path=self.keychain_path)

        keychain.unlock("badger".encode("utf-8"))
        item = keychain.item("foobr", fuzzy_threshold=70)
        self.assertEquals("foobar", item.password)
Exemple #6
0
    def test_unlock_and_read_generic_account_password(self):
        keychain = Keychain(path=self.keychain_path)

        keychain.unlock("badger".encode("utf-8"))
        self.assertEquals("flibble", keychain.item("Generic Account").password)
Exemple #7
0
    def test_unlock_and_read_generated_password(self):
        keychain = Keychain(path=self.keychain_path)

        keychain.unlock("badger".encode("utf-8"))
        self.assertEquals("foobar", keychain.item("foobar").password)
Exemple #8
0
class CLI(object):
    """
    The 1pass command line interface.
    """

    def __init__(self, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
                 getpass=getpass.getpass, arguments=sys.argv[1:]):
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.getpass = getpass
        self.arguments = self.argument_parser().parse_args(arguments)
        self.keychain = Keychain(self.arguments.path)

    def run(self):
        """
        The main entry point, performs the appropriate action for the given
        arguments.
        """
	if self.arguments.l:
		self.keychain.print_list()
		sys.exit()

	if self.arguments.m:
		self.keychain.print_list(self.arguments.m)
		sys.exit()

	if not self.arguments.l and not self.arguments.m and self.arguments.item is None:
		self.argument_parser().print_usage()
		self.stderr.write("1pass: error: too few arguments\n")
		sys.exit(0)

        self._unlock_keychain()

        item = self.keychain.item(
            self.arguments.item,
            fuzzy_threshold=self._fuzzy_threshold(),
        )

        if item is not None:
            self.stdout.write("username: %s\n" % item.username)
            self.stdout.write("password: %s\n" % item.password)
        else:
            self.stderr.write("1pass: Could not find an item named '%s'\n" % (
                self.arguments.item,
            ))
            sys.exit(os.EX_DATAERR)

    def argument_parser(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("item", nargs='?', help="The name of the password to decrypt")
        parser.add_argument(
            "--path",
            default=os.environ.get('ONEPASSWORD_KEYCHAIN', DEFAULT_KEYCHAIN_PATH),
            help="Path to your 1Password.agilekeychain file",
        )
        parser.add_argument(
            "--fuzzy",
            action="store_true",
            help="Perform fuzzy matching on the item",
        )
        parser.add_argument(
            "--no-prompt",
            action="store_true",
            help="Don't prompt for a password, read from STDIN instead",
        )
	parser.add_argument(
	    "-l",
	    action="store_true",
            help="Print all stored item names",
	)
	parser.add_argument(
	    "-m",
	    action="store",
	    help="Print all stored item names matching -m string",
	)
        return parser

    def _unlock_keychain(self):
        if self.arguments.no_prompt:
            self._unlock_keychain_stdin()
        else:
            self._unlock_keychain_prompt()

    def _unlock_keychain_stdin(self):
        password = self.stdin.read().strip()
        self.keychain.unlock(password)
        if self.keychain.locked:
            self.stderr.write("1pass: Incorrect master password\n")
            sys.exit(os.EX_DATAERR)

    def _unlock_keychain_prompt(self):
        while self.keychain.locked:
            try:
                self.keychain.unlock(self.getpass("Master password: "******"\n")
                sys.exit(0)

    def _fuzzy_threshold(self):
        if self.arguments.fuzzy:
            return 70
        else:
            return 100
Exemple #9
0
class CLI(object):
    """
    The 1pass command line interface.
    """

    def __init__(self, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
                 getpass=getpass.getpass, arguments=sys.argv[1:]):
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.getpass = getpass
        self.arguments = self.argument_parser().parse_args(arguments)
        self.keychain = Keychain(self.arguments.path)
        #self.keychain = Keychain(DEFAULT_KEYCHAIN_PATH)

    def run(self):
        """
        Blabla
        """
        self._unlock_keychain()

        items = self.keychain.items(
            self.arguments.item
        )

        if items:
            if items.count == 1:
                self.get_item(items[0][0])
            else:
                i = 1
                for n in items:
                    self.stdout.write("%i. %s\n" % (i, n[0]))
                    i = i + 1
                self.stdout.write("q. Quit\n")
                inp = input("Which one? ")
                if "q" == "%s".format(inp):
                    sys.exit(0)
                else:
                    index = int(inp) - 1
                    self.get_item(items[index][0])
        else:
            self.stderr.write("1pass: Could not find an item named '%s'\n" % (
                self.arguments.item,
            ))
            sys.exit(os.EX_DATAERR)

    def get_item(self, item):
        item = self.keychain.item(
            item,
            fuzzy_threshold=self._fuzzy_threshold(),
        )
        pyperclip.copy(item.password)
        self.stdout.write("Copied to clipboard!")

    def argument_parser(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("item", help="The name of the password to decrypt")
        parser.add_argument(
            "--path",
            default=os.environ.get('ONEPASSWORD_KEYCHAIN', DEFAULT_KEYCHAIN_PATH),
            help="Path to your 1Password.agilekeychain file",
        )
        parser.add_argument(
            "--fuzzy",
            action="store_true",
            help="Perform fuzzy matching on the item",
        )
        parser.add_argument(
            "--no-prompt",
            action="store_true",
            help="Don't prompt for a password, read from STDIN instead",
        )
        return parser

    def _unlock_keychain(self):
        if self.arguments.no_prompt:
            self._unlock_keychain_stdin()
        else:
            self._unlock_keychain_prompt()

    def _unlock_keychain_stdin(self):
        password = self.stdin.read().strip()
        self.keychain.unlock(password)
        if self.keychain.locked:
            self.stderr.write("1pass: Incorrect master password\n")
            sys.exit(os.EX_DATAERR)

    def _unlock_keychain_prompt(self):
        while self.keychain.locked:
            try:
                self.keychain.unlock(self.getpass("Master password: "******"\n")
                sys.exit(0)

    def _fuzzy_threshold(self):
        if self.arguments.fuzzy:
            return 70
        else:
            return 100
Exemple #10
0
    def test_unlock_and_read_with_fuzzy_matching(self):
        keychain = Keychain(path=self.keychain_path)

        keychain.unlock("badger")
        item = keychain.item("foobr", fuzzy_threshold=70)
        self.assertEquals("foobar", item.password)
Exemple #11
0
    def test_unlock_and_read_generic_account_password(self):
        keychain = Keychain(path=self.keychain_path)

        keychain.unlock("badger")
        self.assertEquals("flibble", keychain.item("Generic Account").password)
Exemple #12
0
    def test_unlock_and_read_generated_password(self):
        keychain = Keychain(path=self.keychain_path)

        keychain.unlock("badger")
        self.assertEquals("foobar", keychain.item("foobar").password)
Exemple #13
0
class CLI(object):
    """
    The 1pass command line interface.
    """
    def __init__(self,
                 stdin=sys.stdin,
                 stdout=sys.stdout,
                 stderr=sys.stderr,
                 getpass=getpass.getpass,
                 arguments=sys.argv[1:]):
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.getpass = getpass
        self.arguments = self.argument_parser().parse_args(arguments)
        self.keychain = Keychain(self.arguments.path)

    def run(self):
        """
        The main entry point, performs the appropriate action for the given
        arguments.
        """
        self._unlock_keychain()

        item = self.keychain.item(
            self.arguments.item,
            fuzzy_threshold=self._fuzzy_threshold(),
        )

        if item is not None:
            self.stdout.write("%s\n" % item.password)
        else:
            self.stderr.write("1pass: Could not find an item named '%s'\n" %
                              (self.arguments.item, ))
            sys.exit(os.EX_DATAERR)

    def argument_parser(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("item", help="The name of the password to decrypt")
        parser.add_argument(
            "--path",
            default=os.environ.get('ONEPASSWORD_KEYCHAIN',
                                   DEFAULT_KEYCHAIN_PATH),
            help="Path to your 1Password.agilekeychain file",
        )
        parser.add_argument(
            "--fuzzy",
            action="store_true",
            help="Perform fuzzy matching on the item",
        )
        parser.add_argument(
            "--no-prompt",
            action="store_true",
            help="Don't prompt for a password, read from STDIN instead",
        )
        return parser

    def _unlock_keychain(self):
        if self.arguments.no_prompt:
            self._unlock_keychain_stdin()
        else:
            self._unlock_keychain_prompt()

    def _unlock_keychain_stdin(self):
        password = self.stdin.read().strip()
        self.keychain.unlock(password)
        if self.keychain.locked:
            self.stderr.write("1pass: Incorrect master password\n")
            sys.exit(os.EX_DATAERR)

    def _unlock_keychain_prompt(self):
        while self.keychain.locked:
            try:
                self.keychain.unlock(self.getpass("Master password: "******"\n")
                sys.exit(0)

    def _fuzzy_threshold(self):
        if self.arguments.fuzzy:
            return 70
        else:
            return 100
Exemple #14
0
    def test_unlock_and_read_generic_account_password(self):
        path = os.path.join(os.path.dirname(__file__), "data", "1Password.agilekeychain")
        keychain = Keychain(path=path)

        keychain.unlock("badger")
        self.assertEquals("flibble", keychain.item("Generic Account").password)
Exemple #15
0
    def test_unlock_and_read_generated_password(self):
        path = os.path.join(os.path.dirname(__file__), "data", "1Password.agilekeychain")
        keychain = Keychain(path=path)

        keychain.unlock("badger")
        self.assertEquals("foobar", keychain.item("foobar").password)
Exemple #16
0
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from onepassword import Keychain

DEFAULT_KEYCHAIN_PATH = "~/Dropbox/1Password.agilekeychain"

parser = argparse.ArgumentParser()
parser.add_argument("item", help="The name of the password to decrypt")
parser.add_argument("--path",
                    default=os.environ.get('ONEPASSWORD_KEYCHAIN',
                                           DEFAULT_KEYCHAIN_PATH),
                    help="Path to your 1Password.agilekeychain file")
args = parser.parse_args()

keychain = Keychain(args.path)
while keychain.locked:
    try:
        keychain.unlock(getpass("Master password: "******"")
        sys.exit(0)

item = keychain.item(args.item)
if item is not None:
    print(repr(item._data['notesPlain']))
else:
    sys.stderr.write("Could not find a item named '%s'\n" % args.item)
    sys.exit(1)