From aeaed5bf4516b77f3d9d0e96c57c65a5f82b9150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sivert=20V=2E=20S=C3=A6ther?= Date: Tue, 18 Feb 2025 15:26:30 +0100 Subject: [PATCH] Add some python tools and .gitignore --- .gitignore | 1 + README.md | 7 +++++++ aes.py | 22 ++++++++++++++++++++++ apikeygen.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 13 +++++++++++++ requirements.txt | 3 +++ 6 files changed, 95 insertions(+) create mode 100644 .gitignore create mode 100755 aes.py create mode 100755 apikeygen.py create mode 100644 pyproject.toml create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/README.md b/README.md index 660e18d..defa4dd 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,13 @@ To avoud cluttering it too bad there is also the `docker` folder, as my workflow This too is in my `PATH`. +### Python and pip + +Everyone should know that pip kinda sucks. +So to make use of all the Python scripts in here I **highly** recommend to use `poetry` to make a virtual env. +Then all you'll have to do is run `poetry shell` in the root of this repo and you'll be ready to run all those nice, juicy Python scripts of mine. + + ### deploy.py To use this one you'll need to `pip install docker` and maybe also `pip install paramiko` depending on what you put in the `DOCKER_HOST` env var. diff --git a/aes.py b/aes.py new file mode 100755 index 0000000..174c123 --- /dev/null +++ b/aes.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +from Cryptodome.Cipher import AES +from base64 import b64decode + +KEY = b'931069B52546C2DF278CD52EE695D4F2' + + +def decode(enc): + enc = b64decode(enc) + return enc[16:], enc[0:16] + + +def decipher(enc, iv): + cipher = AES.new(KEY, AES.MODE_CBC, iv=iv) + return cipher.decrypt(enc) + + +if __name__ == '__main__': + from sys import argv + enc, iv = decode(argv[1]) + print(decipher(enc, iv)) + diff --git a/apikeygen.py b/apikeygen.py new file mode 100755 index 0000000..b2c0dfa --- /dev/null +++ b/apikeygen.py @@ -0,0 +1,49 @@ +#!/usr/bin/env -S python3 +from datetime import datetime, timedelta +from jwt import decode, encode +from sys import argv + +unix_time = lambda x: (x - datetime(1970, 1, 1)).total_seconds() +EXP = int('{:.0f}'.format( + unix_time(datetime.now() + timedelta(weeks=52 * 9)) +)) + +SECRET = r'Sd-{01i*_6Ns22xk^|?/*%g`LP327$4yrLYF#H5zo' +ALGO = 'HS256' +SKEL = { + 'role': 'SystemAdministrator', + # 'role': 'User', + 'iss': 'portal.byndle.no', + 'aud': 'portal.byndle.no', + # 'iss': 'localhost:4200', + # 'aud': 'localhost:4200', + 'generator': argv[0], + 'exp': EXP +} + + +# PROD +UID=2322 +CID=14 + +# DEV +UID=1367 #133 # 10 +CID=236 + +if __name__ == '__main__': + if len(argv) > 1: + payload = decode(argv[1], algorithms=[ALGO], options={'verify_signature': False}) + payload.update({ +# 'role': 'AccountOwner', + 'modificator': argv[0], + 'exp': EXP + }) + print(payload) + print(encode(payload, SECRET, algorithm=ALGO)) + else: + payload = SKEL.copy() + payload.update({ + 'customerId': CID, + 'userId': UID, + }) + print(encode(payload, SECRET, algorithm=ALGO)) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..499c1f0 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,13 @@ +[tool.poetry] +name = "byndle-tools" +version = "0.1.0" +description = "Tools to make the Byndle DX go brrr 😜" +authors = ["Sivert V. Sæther "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.10" +pycryptodomex = "^3.21.0" +docker = "^7.1.0" +pyjwt = "^2.10.1" + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8adeab3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pycryptodomex==3.21.0 +docker==7.1.0 +pyjwt==2.10.1