https://dev.kronoz.win/r/marzneshin.git
| 1 |
a47eb4c |
Dawsh |
2y ago |
from typing import Optional |
| 2 |
a47eb4c |
Dawsh |
2y ago |
|
| 3 |
a47eb4c |
Dawsh |
2y ago |
from fastapi.security import OAuth2PasswordBearer |
| 4 |
a47eb4c |
Dawsh |
2y ago |
from passlib.context import CryptContext |
| 5 |
a47eb4c |
Dawsh |
2y ago |
from pydantic import ConfigDict, BaseModel |
| 6 |
a47eb4c |
Dawsh |
2y ago |
|
| 7 |
a47eb4c |
Dawsh |
2y ago |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") |
| 8 |
d6e2aad |
Dawsh |
2y ago |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/admins/token") # Admin view url |
| 9 |
a47eb4c |
Dawsh |
2y ago |
|
| 10 |
a47eb4c |
Dawsh |
2y ago |
|
| 11 |
a47eb4c |
Dawsh |
2y ago |
class Token(BaseModel): |
| 12 |
a47eb4c |
Dawsh |
2y ago |
access_token: str |
| 13 |
a47eb4c |
Dawsh |
2y ago |
token_type: str = "bearer" |
| 14 |
a47eb4c |
Dawsh |
2y ago |
|
| 15 |
a47eb4c |
Dawsh |
2y ago |
|
| 16 |
a47eb4c |
Dawsh |
2y ago |
class Admin(BaseModel): |
| 17 |
a47eb4c |
Dawsh |
2y ago |
username: str |
| 18 |
a47eb4c |
Dawsh |
2y ago |
is_sudo: bool |
| 19 |
a47eb4c |
Dawsh |
2y ago |
model_config = ConfigDict(from_attributes=True) |
| 20 |
a47eb4c |
Dawsh |
2y ago |
|
| 21 |
a47eb4c |
Dawsh |
2y ago |
|
| 22 |
a47eb4c |
Dawsh |
2y ago |
class AdminCreate(Admin): |
| 23 |
a47eb4c |
Dawsh |
2y ago |
password: str |
| 24 |
a47eb4c |
Dawsh |
2y ago |
|
| 25 |
a47eb4c |
Dawsh |
2y ago |
@property |
| 26 |
a47eb4c |
Dawsh |
2y ago |
def hashed_password(self): |
| 27 |
a47eb4c |
Dawsh |
2y ago |
return pwd_context.hash(self.password) |
| 28 |
a47eb4c |
Dawsh |
2y ago |
|
| 29 |
a47eb4c |
Dawsh |
2y ago |
|
| 30 |
a47eb4c |
Dawsh |
2y ago |
class AdminModify(BaseModel): |
| 31 |
a47eb4c |
Dawsh |
2y ago |
password: str |
| 32 |
a47eb4c |
Dawsh |
2y ago |
is_sudo: bool |
| 33 |
a47eb4c |
Dawsh |
2y ago |
|
| 34 |
a47eb4c |
Dawsh |
2y ago |
@property |
| 35 |
a47eb4c |
Dawsh |
2y ago |
def hashed_password(self): |
| 36 |
a47eb4c |
Dawsh |
2y ago |
return pwd_context.hash(self.password) |
| 37 |
a47eb4c |
Dawsh |
2y ago |
|
| 38 |
a47eb4c |
Dawsh |
2y ago |
|
| 39 |
a47eb4c |
Dawsh |
2y ago |
class AdminPartialModify(AdminModify): |
| 40 |
a47eb4c |
Dawsh |
2y ago |
__annotations__ = {k: Optional[v] for k, v in AdminModify.__annotations__.items()} |
| 41 |
a47eb4c |
Dawsh |
2y ago |
|
| 42 |
a47eb4c |
Dawsh |
2y ago |
|
| 43 |
a47eb4c |
Dawsh |
2y ago |
class AdminInDB(Admin): |
| 44 |
a47eb4c |
Dawsh |
2y ago |
username: str |
| 45 |
a47eb4c |
Dawsh |
2y ago |
hashed_password: str |
| 46 |
a47eb4c |
Dawsh |
2y ago |
|
| 47 |
a47eb4c |
Dawsh |
2y ago |
def verify_password(self, plain_password): |
| 48 |
a47eb4c |
Dawsh |
2y ago |
return pwd_context.verify(plain_password, self.hashed_password) |