aboutsummaryrefslogtreecommitdiff
path: root/scripts/p7s/bitwarden.py
diff options
context:
space:
mode:
authoralex <alex@pdp7.net>2025-04-26 18:12:45 +0200
committeralexpdp7 <alex@corcoles.net>2025-04-26 18:27:22 +0200
commit63a77d4fcabe403ee6688384b4a71bc9dc81e737 (patch)
treed21daf57ae96d7b1c8d564b81b02177c508becaa /scripts/p7s/bitwarden.py
parent91e9af4495a9317b1186375f247324a72f0ee011 (diff)
Update scripts
- Remove Bitwarden download, ubpkg can do it - Remove newsboat, zfs scripts - Remove appdirs - Migrate to new Bitwarden server - Some fixes
Diffstat (limited to 'scripts/p7s/bitwarden.py')
-rw-r--r--scripts/p7s/bitwarden.py37
1 files changed, 7 insertions, 30 deletions
diff --git a/scripts/p7s/bitwarden.py b/scripts/p7s/bitwarden.py
index f3f26dd1..ab6cc4f2 100644
--- a/scripts/p7s/bitwarden.py
+++ b/scripts/p7s/bitwarden.py
@@ -1,35 +1,13 @@
import contextlib
-import io
import json
import os
import subprocess
-import zipfile
-
-import httpx
-
-from p7s import appdirs
class Bitwarden():
- def download(self, check_version=False):
- self.bw_command = appdirs.user_cache_dir() / "bw"
- if self.bw_command.exists() and not check_version:
- return self.bw_command
- r = httpx.get("https://vault.bitwarden.com/download/?app=cli&platform=linux")
- location = r.headers["location"]
- version = location.split("/")[7]
- bw_versioned_command = appdirs.user_cache_dir() / f"bw-{version}"
- if not bw_versioned_command.exists():
- with zipfile.ZipFile(io.BytesIO(httpx.get(location, follow_redirects=True).content)) as zip:
- with zip.open("bw") as zip_bw:
- bw_versioned_command.write_bytes(zip_bw.read())
- bw_versioned_command.chmod(0o755)
- self.bw_command.unlink(missing_ok=True)
- self.bw_command.symlink_to(bw_versioned_command)
-
@contextlib.contextmanager
def login(self, server, email):
- subprocess.run([self.bw_command, "config", "server", server], check=True)
+ subprocess.run(["bw", "config", "server", server], check=True)
status = self.status()["status"]
if status == "unauthenticated":
command = ["login", email]
@@ -37,29 +15,28 @@ class Bitwarden():
command = ["unlock"]
else:
assert False, f"unexpected status {status}"
- command = subprocess.run([self.bw_command] + command, check=True, stdout=subprocess.PIPE, encoding="UTF8")
+ command = subprocess.run(["bw"] + command, check=True, stdout=subprocess.PIPE, encoding="UTF8")
export_line = command.stdout.splitlines()[3]
session = export_line.split('"')[1]
os.environ["BW_SESSION"] = session
try:
- yield self.bw_command
- subprocess.run([self.bw_command, "lock"], check=True)
+ yield
+ subprocess.run(["bw", "logout"], check=True)
finally:
del os.environ["BW_SESSION"]
def sync(self):
- subprocess.run([self.bw_command, "sync"], check=True)
+ subprocess.run(["bw", "sync"], check=True)
def status(self):
- return json.loads(subprocess.run([self.bw_command, "status"], check=True, stdout=subprocess.PIPE).stdout)
+ return json.loads(subprocess.run(["bw", "status"], check=True, stdout=subprocess.PIPE).stdout)
def get_item(self, uuid):
- return json.loads(subprocess.run([self.bw_command, "get", "item", uuid], check=True, stdout=subprocess.PIPE).stdout)
+ return json.loads(subprocess.run(["bw", "get", "item", uuid], check=True, stdout=subprocess.PIPE).stdout)
def get_item(server, email, uuid):
b = Bitwarden()
- b.download()
with b.login(server, email):
b.sync()
return b.get_item(uuid)