Commit adb1f3cd by Karsa Zoltán István

bugfixes

parent 9a6f23b7
from typing import Union from typing import Union
from pydantic import BaseModel, Field, EmailStr from pydantic import BaseModel, EmailStr
class User(BaseModel): class User(BaseModel):
username: str username: str
...@@ -14,13 +14,13 @@ class Token(BaseModel): ...@@ -14,13 +14,13 @@ class Token(BaseModel):
token: str token: str
class UserLoginSchema(BaseModel): class UserLoginSchema(BaseModel):
username: EmailStr = Field(...) username: str
password: str = Field(...) password: str
class Config: class Config:
schema_extra = { schema_extra = {
"example": { "example": {
"email": "abdulazeez@x.com", "username": "user",
"password": "weakpassword" "password": "weakpassword"
} }
} }
\ No newline at end of file
...@@ -8,8 +8,7 @@ from sredis.sredis import * ...@@ -8,8 +8,7 @@ from sredis.sredis import *
from sredis.models import * from sredis.models import *
import logging import logging
from core.models import User, DataCenter, Token from core.models import User, DataCenter, Token
from uuid import uuid4 from core.auth import signJWT
from core.auth import signJWT, hash_pass
from core.bearer import get_current_user from core.bearer import get_current_user
logging.config.fileConfig('logging.conf', disable_existing_loggers=False) logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
...@@ -26,12 +25,7 @@ add_datacenter("https://kappa3.fured.cloud.bme.hu") ...@@ -26,12 +25,7 @@ add_datacenter("https://kappa3.fured.cloud.bme.hu")
@app.post("/user/signup", tags=["user"]) @app.post("/user/signup", tags=["user"])
async def create_user(user: User = Body(...)): async def create_user(user: User = Body(...)):
user = PUser( create_puser(user)
username=user.username,
email=user.email,
password=hash_pass(user.password)
)
user.save()
return signJWT(user.username) return signJWT(user.username)
@app.post("/user/login", tags=["user"]) @app.post("/user/login", tags=["user"])
......
import datetime from redis_om import HashModel, Field
from typing import Optional
from redis_om import HashModel
from pydantic import EmailStr from pydantic import EmailStr
class PUser(HashModel): class PUser(HashModel):
username: str username: str = Field(index=True)
email: EmailStr email: EmailStr
password: str password: str
import redis import redis
from core.auth import hash_pass
from core.models import UserLoginSchema from core.models import UserLoginSchema
from .models import PUser from .models import PUser
from passlib.hash import pbkdf2_sha256 from passlib.hash import pbkdf2_sha256
from core.models import User
from core.auth import hash_pass
from fastapi import HTTPException
from redis_om import Migrator
r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True) r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
r.set("datacenters_cnt", 1) r.set("datacenters_cnt", 1)
...@@ -33,7 +36,18 @@ def get_datacenter_token(username: str, datacenter: str): ...@@ -33,7 +36,18 @@ def get_datacenter_token(username: str, datacenter: str):
return str(r.hget(f"tokens:{username}", datacenter)) return str(r.hget(f"tokens:{username}", datacenter))
def check_user(data: UserLoginSchema): def check_user(data: UserLoginSchema):
user = PUser.find((PUser.username == data.username)).all() user = PUser.find(PUser.username == data.username).all()
if pbkdf2_sha256.verify(data.password, user[0].password): if pbkdf2_sha256.verify(data.password, user[0].password):
return user[0] return user[0]
return False return False
\ No newline at end of file
def create_puser(user: User):
s = PUser.find(PUser.username == 'karsa').all()
if s:
raise HTTPException(status_code=403, detail="User already exists")
user = PUser(
username=user.username,
email=user.email,
password=hash_pass(user.password)
)
user.save()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment