From a2df8314dc4dbce845425057ff86bd4fa81d0eee Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Tue, 22 Aug 2023 21:00:32 +0700 Subject: [PATCH] Slices, an extension script for CostaPy --- slices/__init__.py | 0 slices/bodyfetcher.py | 13 +++++++++++++ {core => slices}/loggorilla.py | 0 core/mailme.py => slices/sendwave.py | 0 slices/tokenguard.py | 23 +++++++++++++++++++++++ 5 files changed, 36 insertions(+) create mode 100644 slices/__init__.py create mode 100644 slices/bodyfetcher.py rename {core => slices}/loggorilla.py (100%) rename core/mailme.py => slices/sendwave.py (100%) create mode 100644 slices/tokenguard.py diff --git a/slices/__init__.py b/slices/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/slices/bodyfetcher.py b/slices/bodyfetcher.py new file mode 100644 index 0000000..aaa167c --- /dev/null +++ b/slices/bodyfetcher.py @@ -0,0 +1,13 @@ +import cherrypy +import cherrypy_cors +import json + +def body_json(): + result = None + if cherrypy.request.method == 'OPTIONS': + cherrypy_cors.preflight(allowed_methods=['GET', 'POST']) + if cherrypy.request.method == 'POST': + cherrypy.serving.response.headers['Content-Type'] = 'application/json' + body_request = cherrypy.request.body.read() + result = json.loads(body_request.decode()) + return result diff --git a/core/loggorilla.py b/slices/loggorilla.py similarity index 100% rename from core/loggorilla.py rename to slices/loggorilla.py diff --git a/core/mailme.py b/slices/sendwave.py similarity index 100% rename from core/mailme.py rename to slices/sendwave.py diff --git a/slices/tokenguard.py b/slices/tokenguard.py new file mode 100644 index 0000000..b2d6ef6 --- /dev/null +++ b/slices/tokenguard.py @@ -0,0 +1,23 @@ +from cryptography.hazmat.primitives import serialization +import jwt + +def encode(payload, id_rsa, passphrase): + private_key = open(id_rsa, 'r').read() + key = serialization.load_ssh_private_key(private_key.encode(), password=passphrase) + token = jwt.encode( + payload = payload, + key = key, + algorithm = 'RS256' + ) + return token + +def decode(token, id_rsa): + public_key = open(id_rsa, 'r').read() + key = serialization.load_ssh_public_key(public_key.encode()) + header = jwt.get_unverified_header(token) + payload = jwt.decode( + jwt = token, + key = key, + algorithms = [header['alg'], ] + ) + return payload