new crypto system for passwords, removed bcrypt
This commit is contained in:
@@ -7,7 +7,6 @@ import logging
|
|||||||
from fastapi import FastAPI, HTTPException, Depends, APIRouter
|
from fastapi import FastAPI, HTTPException, Depends, APIRouter
|
||||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||||
from jose import jwt
|
from jose import jwt
|
||||||
from passlib.context import CryptContext
|
|
||||||
from utils.sqlite import SQLite
|
from utils.sqlite import SQLite
|
||||||
from utils import API_VERSION, FIREGEX_PORT, FIREGEX_HOST, JWT_ALGORITHM, get_interfaces, socketio_emit, DEBUG, SysctlManager, NORELOAD
|
from utils import API_VERSION, FIREGEX_PORT, FIREGEX_HOST, JWT_ALGORITHM, get_interfaces, socketio_emit, DEBUG, SysctlManager, NORELOAD
|
||||||
from utils.loader import frontend_deploy, load_routers
|
from utils.loader import frontend_deploy, load_routers
|
||||||
@@ -16,6 +15,7 @@ from contextlib import asynccontextmanager
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
import socketio
|
import socketio
|
||||||
from socketio.exceptions import ConnectionRefusedError
|
from socketio.exceptions import ConnectionRefusedError
|
||||||
|
import hashlib
|
||||||
|
|
||||||
# DB init
|
# DB init
|
||||||
db = SQLite('db/firegex.db')
|
db = SQLite('db/firegex.db')
|
||||||
@@ -27,7 +27,6 @@ sysctl = SysctlManager({
|
|||||||
})
|
})
|
||||||
|
|
||||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/login", auto_error=False)
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/login", auto_error=False)
|
||||||
crypto = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app):
|
async def lifespan(app):
|
||||||
@@ -65,9 +64,15 @@ app.mount("/sock", sio_app)
|
|||||||
def APP_STATUS(): return "init" if db.get("password") is None else "run"
|
def APP_STATUS(): return "init" if db.get("password") is None else "run"
|
||||||
def JWT_SECRET(): return db.get("secret")
|
def JWT_SECRET(): return db.get("secret")
|
||||||
|
|
||||||
|
def hash_psw(psw: str):
|
||||||
|
salt = db.get("salt")
|
||||||
|
if not salt:
|
||||||
|
salt = secrets.token_hex(32)
|
||||||
|
db.put("salt", salt)
|
||||||
|
return hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()
|
||||||
|
|
||||||
def set_psw(psw: str):
|
def set_psw(psw: str):
|
||||||
hash_psw = crypto.hash(psw)
|
db.put("password", hash_psw(psw))
|
||||||
db.put("password",hash_psw)
|
|
||||||
|
|
||||||
def create_access_token(data: dict):
|
def create_access_token(data: dict):
|
||||||
to_encode = data.copy()
|
to_encode = data.copy()
|
||||||
@@ -137,7 +142,7 @@ async def login_api(form: OAuth2PasswordRequestForm = Depends()):
|
|||||||
if form.password == "":
|
if form.password == "":
|
||||||
return {"status":"Cannot insert an empty password!"}
|
return {"status":"Cannot insert an empty password!"}
|
||||||
await asyncio.sleep(0.3) # No bruteforce :)
|
await asyncio.sleep(0.3) # No bruteforce :)
|
||||||
if crypto.verify(form.password, db.get("password")):
|
if db.get("password") == hash_psw(form.password):
|
||||||
return {"access_token": create_access_token({"logged_in": True}), "token_type": "bearer"}
|
return {"access_token": create_access_token({"logged_in": True}), "token_type": "bearer"}
|
||||||
raise HTTPException(406,"Wrong password!")
|
raise HTTPException(406,"Wrong password!")
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
fastapi[all]
|
fastapi[all]
|
||||||
httpx
|
httpx
|
||||||
uvicorn[standard]
|
uvicorn[standard]
|
||||||
passlib[bcrypt]
|
|
||||||
bcrypt <5
|
|
||||||
psutil
|
psutil
|
||||||
python-jose[cryptography]
|
python-jose[cryptography]
|
||||||
python-socketio
|
python-socketio
|
||||||
|
|||||||
Reference in New Issue
Block a user