From 169fcc013abdffb9825129ecc3d648100cc86751 Mon Sep 17 00:00:00 2001 From: samdixon Date: Thu, 17 Oct 2019 19:00:08 -0400 Subject: [PATCH 1/3] Adds ability to pass session variable to get_password, set_password, del_password --- bitwarden_keyring/__init__.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bitwarden_keyring/__init__.py b/bitwarden_keyring/__init__.py index 5ccb2c3..ffe9385 100644 --- a/bitwarden_keyring/__init__.py +++ b/bitwarden_keyring/__init__.py @@ -174,8 +174,9 @@ def get_session(environ): return ask_for_session(bool(user)) -def get_password(service, username): - session = get_session(os.environ) +def get_password(service, username, session=None): + if session == None: + session = get_session(os.environ) # Making sure we're up to date bw("sync", session=session) @@ -191,8 +192,9 @@ def get_password(service, username): return select_match(matches) -def set_password(service, username, password): - session = get_session(os.environ) +def set_password(service, username, password, session=None): + if session == None: + session = get_session(os.environ) template_str = bw("get", "template", "item", session=session) @@ -226,8 +228,9 @@ def confirm_delete(session, credential): print("Cancelled.") -def delete_password(service, username): - session = get_session(os.environ) +def delete_password(service, username, session=None): + if session == None: + session = get_session(os.environ) bw("sync", session=session) From ac62d21a240e0bb203f530a3885d9ca151739c86 Mon Sep 17 00:00:00 2001 From: samdixon Date: Thu, 17 Oct 2019 19:00:57 -0400 Subject: [PATCH 2/3] Adds tests for new session passing functionality --- tests/test_bitwarden_keyring.py | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/test_bitwarden_keyring.py b/tests/test_bitwarden_keyring.py index 559cf73..15e7020 100644 --- a/tests/test_bitwarden_keyring.py +++ b/tests/test_bitwarden_keyring.py @@ -292,6 +292,16 @@ def test_get_password(bw, db): assert bwkr.get_password("c", "a") == "b" +def test_get_password_with_session(bw, db): + bw.side_effect = [ + "mysession", + None, + '[{"login": {"username": "a", "password": "b"}}]', + ] + session = bwkr.get_session({"BW_SESSION": "bla"}) + + assert bwkr.get_password("c","a", session=session) == "b" + def test_encode(): assert bwkr.encode({"yay": "ho"}) == b"eyJ5YXkiOiAiaG8ifQ==" assert json.loads(base64.b64decode(bwkr.encode({"yay": "ho"}))) == {"yay": "ho"} @@ -323,6 +333,34 @@ def test_set_password(bw, db): } +def test_set_password_with_session(bw, db): + bw.side_effect = ["mysession", '{"a": "b"}', None] + + session = bwkr.get_session({"BW_SESSION": "bla"}) + + bwkr.set_password("c", "d", "e", session=session) + + payload = ( + b"eyJhIjogImIiLCAibmFtZSI6ICJjIiwgIm5vdGVzIjogbnVsbCw" + b"gImxvZ2luIjogeyJ1cmlzIjogW3sibWF0Y2giOiBudWxsLCAidXJ" + b"pIjogImMifV0sICJ1c2VybmFtZSI6ICJkIiwgInBhc3N3b3JkIjog" + b"ImUifX0=" + ) + + bw.assert_called_with("create", "item", payload) + + assert json.loads(base64.b64decode(payload).decode("utf-8")) == { + "a": "b", + "login": { + "password": "e", + "uris": [{"match": None, "uri": "c"}], + "username": "d", + }, + "name": "c", + "notes": None, + } + + def test_delete_password(bw, db, mocker): bw.side_effect = [ "mysession", @@ -337,6 +375,21 @@ def test_delete_password(bw, db, mocker): bw.assert_called_with("delete", "item", "a", session="mysession") +def test_delete_password_with_session(bw, db, mocker): + bw.side_effect = [ + "mysession", + None, + '{"id": "a", "login": {"username": "b"}}', + None, + ] + mocker.patch("bitwarden_keyring.input", return_value="yes") + + session = bwkr.get_session({"BW_SESSION": "bla"}) + + bwkr.delete_password("c", "d", session=session) + + bw.assert_called_with("delete", "item", "a", session=session) + def test_bitwarden_backend_prio_not_installed(mocker): mocker.patch("bitwarden_keyring.bitwarden_cli_installed", return_value=False) with pytest.raises(RuntimeError): From 790600ec16dc4b1a887a131c118faaf5ad8693f8 Mon Sep 17 00:00:00 2001 From: samdixon Date: Thu, 17 Oct 2019 19:07:05 -0400 Subject: [PATCH 3/3] Bumps minor version --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 68ac98f..e80af21 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,7 @@ [metadata] name = bitwarden-keyring description = Keyring backend reading password data from Bitwarden -version = 0.2.1 +version = 0.2.2 author = Joachim Jablon author_email = ewjoachim@gmail.com url = https://github.com/ewjoachim/bitwarden-keyring