Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add session passing to functions #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions bitwarden_keyring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if session == None:
if session is None:

Copy link
Owner

@ewjoachim ewjoachim Sep 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about passing session to get_session() and doing the check over there ? This way, we won't have the check in multiple places in the code ?

session = get_session(os.environ)

# Making sure we're up to date
bw("sync", session=session)
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
url = https://github.com/ewjoachim/bitwarden-keyring
Expand Down
53 changes: 53 additions & 0 deletions tests/test_bitwarden_keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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",
Expand All @@ -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):
Expand Down