| 1 |
a47eb4c |
Dawsh |
2y ago |
from fastapi.security import OAuth2PasswordBearer |
| 2 |
a47eb4c |
Dawsh |
2y ago |
from passlib.context import CryptContext |
| 3 |
a47eb4c |
Dawsh |
2y ago |
from pydantic import ConfigDict, BaseModel |
| 4 |
a47eb4c |
Dawsh |
2y ago |
|
| 5 |
a47eb4c |
Dawsh |
2y ago |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") |
| 6 |
f1ab16c |
Dawsh |
2y ago |
oauth2_scheme = OAuth2PasswordBearer( |
| 7 |
f1ab16c |
Dawsh |
2y ago |
tokenUrl="/api/admins/token" |
| 8 |
f1ab16c |
Dawsh |
2y ago |
) # 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 |
9ddfbc8 |
Mardin |
2y ago |
is_sudo: bool |
| 14 |
a47eb4c |
Dawsh |
2y ago |
token_type: str = "bearer" |
| 15 |
a47eb4c |
Dawsh |
2y ago |
|
| 16 |
a47eb4c |
Dawsh |
2y ago |
|
| 17 |
a47eb4c |
Dawsh |
2y ago |
class Admin(BaseModel): |
| 18 |
9ddfbc8 |
Mardin |
2y ago |
id: int | None = None |
| 19 |
a47eb4c |
Dawsh |
2y ago |
username: str |
| 20 |
a47eb4c |
Dawsh |
2y ago |
is_sudo: bool |
| 21 |
4708fcb |
Dawsh |
2y ago |
enabled: bool = True |
| 22 |
4708fcb |
Dawsh |
2y ago |
all_services_access: bool = False |
| 23 |
4708fcb |
Dawsh |
2y ago |
modify_users_access: bool = True |
| 24 |
4708fcb |
Dawsh |
2y ago |
service_ids: list = [] |
| 25 |
4708fcb |
Dawsh |
2y ago |
subscription_url_prefix: str = "" |
| 26 |
a47eb4c |
Dawsh |
2y ago |
model_config = ConfigDict(from_attributes=True) |
| 27 |
a47eb4c |
Dawsh |
2y ago |
|
| 28 |
a47eb4c |
Dawsh |
2y ago |
|
| 29 |
a47eb4c |
Dawsh |
2y ago |
class AdminCreate(Admin): |
| 30 |
4708fcb |
Dawsh |
2y ago |
username: str |
| 31 |
a47eb4c |
Dawsh |
2y ago |
password: str |
| 32 |
a47eb4c |
Dawsh |
2y ago |
|
| 33 |
a47eb4c |
Dawsh |
2y ago |
@property |
| 34 |
a47eb4c |
Dawsh |
2y ago |
def hashed_password(self): |
| 35 |
a47eb4c |
Dawsh |
2y ago |
return pwd_context.hash(self.password) |
| 36 |
a47eb4c |
Dawsh |
2y ago |
|
| 37 |
a47eb4c |
Dawsh |
2y ago |
|
| 38 |
4708fcb |
Dawsh |
2y ago |
class AdminResponse(Admin): |
| 39 |
4708fcb |
Dawsh |
2y ago |
id: int |
| 40 |
4708fcb |
Dawsh |
2y ago |
users_data_usage: int |
| 41 |
4708fcb |
Dawsh |
2y ago |
|
| 42 |
4708fcb |
Dawsh |
2y ago |
|
| 43 |
4708fcb |
Dawsh |
2y ago |
class AdminModify(Admin): |
| 44 |
a47eb4c |
Dawsh |
2y ago |
password: str |
| 45 |
a47eb4c |
Dawsh |
2y ago |
is_sudo: bool |
| 46 |
a47eb4c |
Dawsh |
2y ago |
|
| 47 |
a47eb4c |
Dawsh |
2y ago |
@property |
| 48 |
a47eb4c |
Dawsh |
2y ago |
def hashed_password(self): |
| 49 |
4708fcb |
Dawsh |
2y ago |
return ( |
| 50 |
4708fcb |
Dawsh |
2y ago |
pwd_context.hash(self.password) |
| 51 |
4708fcb |
Dawsh |
2y ago |
if self.password is not None |
| 52 |
4708fcb |
Dawsh |
2y ago |
else None |
| 53 |
4708fcb |
Dawsh |
2y ago |
) |
| 54 |
a47eb4c |
Dawsh |
2y ago |
|
| 55 |
a47eb4c |
Dawsh |
2y ago |
|
| 56 |
a47eb4c |
Dawsh |
2y ago |
class AdminPartialModify(AdminModify): |
| 57 |
4708fcb |
Dawsh |
2y ago |
"""__annotations__ = { |
| 58 |
4708fcb |
Dawsh |
2y ago |
k: v | None for k, v in AdminModify.__annotations__.items() |
| 59 |
4708fcb |
Dawsh |
2y ago |
}""" |
| 60 |
4708fcb |
Dawsh |
2y ago |
|
| 61 |
4708fcb |
Dawsh |
2y ago |
password: str | None = None |
| 62 |
4708fcb |
Dawsh |
2y ago |
username: str | None = None |
| 63 |
4708fcb |
Dawsh |
2y ago |
is_sudo: bool | None = None |
| 64 |
4708fcb |
Dawsh |
2y ago |
enabled: bool | None = None |
| 65 |
4708fcb |
Dawsh |
2y ago |
all_services_access: bool | None = None |
| 66 |
4708fcb |
Dawsh |
2y ago |
modify_users_access: bool | None = None |
| 67 |
4708fcb |
Dawsh |
2y ago |
service_ids: list | None = None |
| 68 |
4708fcb |
Dawsh |
2y ago |
subscription_url_prefix: str | None = None |
| 69 |
a47eb4c |
Dawsh |
2y ago |
|
| 70 |
a47eb4c |
Dawsh |
2y ago |
|
| 71 |
a47eb4c |
Dawsh |
2y ago |
class AdminInDB(Admin): |
| 72 |
a47eb4c |
Dawsh |
2y ago |
username: str |
| 73 |
a47eb4c |
Dawsh |
2y ago |
hashed_password: str |
| 74 |
a47eb4c |
Dawsh |
2y ago |
|
| 75 |
a47eb4c |
Dawsh |
2y ago |
def verify_password(self, plain_password): |
| 76 |
a47eb4c |
Dawsh |
2y ago |
return pwd_context.verify(plain_password, self.hashed_password) |