Skip to content

Commit

Permalink
Merge pull request #62 from bondhome/marcio/auto-unlock-with-pin
Browse files Browse the repository at this point in the history
Auto unlock bond with Pin and get the token
  • Loading branch information
marciogranzotto authored May 4, 2023
2 parents ef9cc3e + 864258f commit fd7b808
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
19 changes: 9 additions & 10 deletions bond/commands/select.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -14,18 +14,15 @@ 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"},
}

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"
Expand All @@ -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:
Expand All @@ -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")
Expand Down
19 changes: 13 additions & 6 deletions bond/commands/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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 <token>', or unlock the token and run 'bond token'"
)
print("You can set it manually with 'bond token <token>', or unlock the token and run 'bond token'")
print("(tip: the token is unlocked for a short period after a reboot)")
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit fd7b808

Please sign in to comment.