...
This commit is contained in:
0
herolib/crypt/__init__.py
Normal file
0
herolib/crypt/__init__.py
Normal file
0
herolib/crypt/box/__init__.py
Normal file
0
herolib/crypt/box/__init__.py
Normal file
41
herolib/crypt/box/box.py
Normal file
41
herolib/crypt/box/box.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from fastapi import HTTPException
|
||||
from cryptography.fernet import Fernet
|
||||
import redis
|
||||
import base64
|
||||
import hashlib
|
||||
|
||||
#TODO: KRISTOF FIX
|
||||
|
||||
def box_get():
|
||||
r = redis.Redis(host='localhost', port=6379, db=0)
|
||||
|
||||
key = r.get('my.secret')
|
||||
|
||||
if key is None:
|
||||
raise HTTPException(status_code=404, detail="can't find my.secret in redis, needs to be set: "+name+" use secret-set to register your secret.")
|
||||
|
||||
|
||||
hash_digest = hashlib.sha256(key).digest()
|
||||
|
||||
# Encode the hash digest to make it url-safe base64-encoded
|
||||
key2 = base64.urlsafe_b64encode(hash_digest)
|
||||
|
||||
try:
|
||||
f = Fernet(key2)
|
||||
except Exception as e:
|
||||
# if str(e).find("Resource Missing")>0:
|
||||
# raise HTTPException(status_code=400, detail="Could not find account with pubkey: "+account_keypair.public_key)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
return f
|
||||
|
||||
|
||||
def box_secret_set(secret:str):
|
||||
r = redis.Redis(host='localhost', port=6379, db=0)
|
||||
|
||||
# key = r.set('my.secret',secret)
|
||||
r.setex('my.secret', 43200,secret) # Set the key with an expiration time of 12 hours
|
||||
|
||||
box_get()
|
||||
|
||||
return "OK"
|
26
herolib/crypt/box/box_api.py
Normal file
26
herolib/crypt/box/box_api.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from fastapi import APIRouter, HTTPException,Response
|
||||
from pydantic import BaseModel, constr, Field
|
||||
from secret.box import box_secret_set,box_get
|
||||
|
||||
#TODO: KRISTOF FIX
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
##############POSITION
|
||||
|
||||
class BoxSecretSetRequest(BaseModel):
|
||||
secret: str = Field(..., description="a well chosen secret key, do never forget this key, you will loose your assets")
|
||||
|
||||
|
||||
|
||||
@router.post("/secret",description="Set your secret for your hero, will be kept for 12 hours")
|
||||
async def set_secret(request: BoxSecretSetRequest):
|
||||
box_secret_set(secret=request.secret)
|
||||
return Response(content="OK", media_type="text/plain")
|
||||
|
||||
|
||||
@router.get("/secret",description="Check if it exists.")
|
||||
async def secret_check():
|
||||
b=box_get()
|
||||
return Response(content="OK", media_type="text/plain")
|
||||
|
Reference in New Issue
Block a user