Slices, an extension script for CostaPy

This commit is contained in:
Dita Aji Pratama 2023-08-22 21:00:32 +07:00
parent 8fa748fd62
commit a2df8314dc
5 changed files with 36 additions and 0 deletions

0
slices/__init__.py Normal file
View File

13
slices/bodyfetcher.py Normal file
View File

@ -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

23
slices/tokenguard.py Normal file
View File

@ -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