diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 0e998f0..16c3b60 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,6 +1,6 @@ # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.166.1/containers/python-3/.devcontainer/base.Dockerfile -# [Choice] Python version: 3, 3.9, 3.8, 3.7, 3.6 +# [Choice] Python version: 3, 3.9, 3.8, 3.7 ARG VARIANT="3" FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT} diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index bde21f2..d2ca647 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -20,7 +20,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.6, 3.7, 3.8] + python-version: [3.7, 3.8, 3.9] steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} @@ -35,7 +35,7 @@ jobs: - name: Lint with flake8 run: | # stop the build if there are any errors. The GitHub editor is 127 chars wide - flake8 . --count --max-complexity=10 --max-line-length=127 --statistics + flake8 . --count --max-line-length=127 --statistics - name: Test with pytest run: | pytest diff --git a/bond/commands/select.py b/bond/commands/select.py index cd99d7c..b268682 100644 --- a/bond/commands/select.py +++ b/bond/commands/select.py @@ -1,4 +1,4 @@ -from bond.commands.token import check_unlocked_token +from bond.commands.token import check_unlocked_token, unlock_token from bond.database import BondDatabase @@ -14,6 +14,7 @@ class SelectCommand(object): (you can also use a prefix, if it uniquely identifies an already-discovered Bond""", }, + "--pin": {"help": "specify Bond PIN to automatically unlock token"}, "--clear": {"action": "store_true", "help": "clear selection"}, "--ip": {"help": "specify Bond IP address"}, "--port": {"help": "specify Bond HTTP port"}, @@ -21,11 +22,7 @@ class SelectCommand(object): def run(self, args): if args.bond_id: - matches = [ - bond - for bond in BondDatabase.get_bonds() - if bond.lower().startswith(args.bond_id.lower()) - ] + matches = [bond for bond in BondDatabase.get_bonds() if bond.lower().startswith(args.bond_id.lower())] if len(matches) == 0: proceed = input( f"{args.bond_id} hasn't been discovered by bond-cli ('bond discover').\n" @@ -34,9 +31,7 @@ def run(self, args): if proceed.lower() == "y": bond_id = args.bond_id else: - raise SystemExit( - "Aborting. Try 'bond discover' on the same network as your Bond" - ) + raise SystemExit("Aborting. Try 'bond discover' on the same network as your Bond") if len(matches) == 1: bond_id = matches[0] if len(matches) > 1: @@ -52,7 +47,11 @@ def run(self, args): BondDatabase.set_bond(bond_id, "port", args.port) print(f"Set {bond_id} port {args.ip}") print(f"Selected Bond: {BondDatabase().get('selected_bondid')}") - token = check_unlocked_token() # noqa: F841 + if args.pin: + print("Unlocking token...") + token = unlock_token(bond_id, args.pin) + else: + token = check_unlocked_token() # noqa: F841 elif args.clear: BondDatabase().pop("selected_bondid", None) print("Cleared selected Bond") diff --git a/bond/commands/token.py b/bond/commands/token.py index 7dd8571..9d49034 100644 --- a/bond/commands/token.py +++ b/bond/commands/token.py @@ -21,6 +21,17 @@ def check_unlocked_token(bond_id=None): return token is not None +def unlock_token(bond_id=None, pin=None): + bond_id = bond_id or BondDatabase.get_assert_selected_bondid() + if not pin: + pin = input("Enter Bond PIN: ") + rsp = bond.proto.patch(bond_id, topic="token", body={"pin": pin, "locked": 0}) + token = rsp.get("b", {}).get("token") + if token: + update_token(token, bond_id) + return token is not None + + class TokenCommand(object): subcmd = "token" help = "Manage token-based authentication." @@ -37,11 +48,7 @@ def run(self, args): print(f"{bond_id}'s token is not unlocked.") stored_token = BondDatabase.get_bond(bond_id).get("token") if stored_token: - print( - f"There's already a token for {bond_id} in your local database: {stored_token}." - ) + print(f"There's already a token for {bond_id} in your local database: {stored_token}.") print("If this token is obsolete, you will need to set the new token.") - print( - "You can set it manually with 'bond token ', or unlock the token and run 'bond token'" - ) + print("You can set it manually with 'bond token ', or unlock the token and run 'bond token'") print("(tip: the token is unlocked for a short period after a reboot)") diff --git a/setup.py b/setup.py index a966617..ee78645 100644 --- a/setup.py +++ b/setup.py @@ -1,20 +1,20 @@ from setuptools import find_packages, setup -DEPS_ALL = open("requirements.txt").readlines() +DEPS_ALL = open("requirements.txt", encoding="utf-8").readlines() -DEPS_TEST = DEPS_ALL + open("requirements-test.txt").readlines() +DEPS_TEST = DEPS_ALL + open("requirements-test.txt", encoding="utf-8").readlines() setup( name="bond-cli", - version="0.2.1", + version="0.3.0", author="Olibra", packages=find_packages(), scripts=["bond/bond"], include_package_data=True, description="Bond CLI", - long_description=open("README.md").read(), + long_description=open("README.md", encoding="utf-8").read(), long_description_content_type="text/markdown", - python_requires=">=3.6", + python_requires=">=3.7", setup_requires=["pytest-runner"], install_requires=DEPS_ALL, tests_require=DEPS_TEST,