Merge pull request 'VORKOUT-12' (#7) from VORKOUT-12 into master
Reviewed-on: #7 Reviewed-by: ivan.dev <ivan.dev@heado.ru> Reviewed-by: cyrussmeat <dr.cyrill@gmail.com>
This commit is contained in:
commit
f97d419467
9
Makefile
9
Makefile
@ -49,3 +49,12 @@ install:
|
|||||||
|
|
||||||
%::
|
%::
|
||||||
echo $(MESSAGE)
|
echo $(MESSAGE)
|
||||||
|
|
||||||
|
format-api:
|
||||||
|
cd api && \
|
||||||
|
poetry run ruff format .
|
||||||
|
|
||||||
|
|
||||||
|
check-api:
|
||||||
|
cd api && \
|
||||||
|
poetry run ruff format . --check
|
||||||
|
@ -21,7 +21,6 @@ def bind_routes(application: FastAPI, setting: DefaultSettings) -> None:
|
|||||||
application.include_router(route, prefix=setting.PATH_PREFIX)
|
application.include_router(route, prefix=setting.PATH_PREFIX)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_app() -> FastAPI:
|
def get_app() -> FastAPI:
|
||||||
"""Creates application and all dependable objects."""
|
"""Creates application and all dependable objects."""
|
||||||
loguru.logger.remove()
|
loguru.logger.remove()
|
||||||
@ -80,7 +79,6 @@ app.add_middleware(
|
|||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["GET", "POST", "OPTIONS", "DELETE", "PUT"],
|
allow_methods=["GET", "POST", "OPTIONS", "DELETE", "PUT"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_middleware(MiddlewareAccessTokenValidadtion)
|
app.add_middleware(MiddlewareAccessTokenValidadtion)
|
||||||
|
@ -44,16 +44,11 @@ class DefaultSettings(BaseSettings):
|
|||||||
REDIS_DB: int = int(environ.get("REDIS_DB", "0"))
|
REDIS_DB: int = int(environ.get("REDIS_DB", "0"))
|
||||||
REDIS_PASSWORD: str = environ.get("REDIS_PASSWORD", "hackme")
|
REDIS_PASSWORD: str = environ.get("REDIS_PASSWORD", "hackme")
|
||||||
|
|
||||||
|
|
||||||
SECRET_KEY: str = environ.get("SECRET_KEY", "secret")
|
SECRET_KEY: str = environ.get("SECRET_KEY", "secret")
|
||||||
ALGORITHM: str = environ.get("ALGORITHM", "HS256")
|
ALGORITHM: str = environ.get("ALGORITHM", "HS256")
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = int(
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = int(environ.get("ACCESS_TOKEN_EXPIRE_MINUTES", 600))
|
||||||
environ.get("ACCESS_TOKEN_EXPIRE_MINUTES", 600)
|
|
||||||
)
|
|
||||||
|
|
||||||
REFRESH_TOKEN_EXPIRE_DAYS: int = int(
|
REFRESH_TOKEN_EXPIRE_DAYS: int = int(environ.get("REFRESH_TOKEN_EXPIRE_DAYS_LONG", 365))
|
||||||
environ.get("REFRESH_TOKEN_EXPIRE_DAYS_LONG", 365)
|
|
||||||
)
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def database_settings(self) -> dict:
|
def database_settings(self) -> dict:
|
||||||
|
@ -8,18 +8,16 @@ import asyncio
|
|||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine
|
||||||
from sqlalchemy import URL,create_engine, text
|
from sqlalchemy import URL, create_engine, text
|
||||||
|
|
||||||
|
|
||||||
from api.config import get_settings
|
from api.config import get_settings
|
||||||
from api.config.default import DbCredentialsSchema
|
from api.config.default import DbCredentialsSchema
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SessionManager:
|
class SessionManager:
|
||||||
engines: Any
|
engines: Any
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, database_uri=get_settings().database_uri) -> None:
|
def __init__(self, database_uri=get_settings().database_uri) -> None:
|
||||||
self.database_uri = database_uri
|
self.database_uri = database_uri
|
||||||
self.refresh(database_uri)
|
self.refresh(database_uri)
|
||||||
@ -44,9 +42,11 @@ class SessionManager:
|
|||||||
pool_size=get_settings().CONNECTION_POOL_SIZE,
|
pool_size=get_settings().CONNECTION_POOL_SIZE,
|
||||||
max_overflow=get_settings().CONNECTION_OVERFLOW,
|
max_overflow=get_settings().CONNECTION_OVERFLOW,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_engine_by_db_uri(self, database_uri) -> AsyncEngine:
|
def get_engine_by_db_uri(self, database_uri) -> AsyncEngine:
|
||||||
return self.engines[database_uri]
|
return self.engines[database_uri]
|
||||||
|
|
||||||
|
|
||||||
@contextlib.asynccontextmanager
|
@contextlib.asynccontextmanager
|
||||||
async def get_connection(
|
async def get_connection(
|
||||||
database_uri=None,
|
database_uri=None,
|
||||||
@ -58,6 +58,7 @@ async def get_connection(
|
|||||||
async with engine.connect() as conn:
|
async with engine.connect() as conn:
|
||||||
yield conn
|
yield conn
|
||||||
|
|
||||||
|
|
||||||
async def get_connection_dep() -> AsyncConnection:
|
async def get_connection_dep() -> AsyncConnection:
|
||||||
async with get_connection() as conn:
|
async with get_connection() as conn:
|
||||||
yield conn
|
yield conn
|
||||||
|
@ -16,10 +16,7 @@ async def get_user_id(connection: AsyncConnection, id: int) -> Optional[User]:
|
|||||||
"""
|
"""
|
||||||
Получает юзера по id.
|
Получает юзера по id.
|
||||||
"""
|
"""
|
||||||
query = (
|
query = select(account_table).where(account_table.c.id == id)
|
||||||
select(account_table)
|
|
||||||
.where(account_table.c.id == id)
|
|
||||||
)
|
|
||||||
|
|
||||||
user_db_cursor = await connection.execute(query)
|
user_db_cursor = await connection.execute(query)
|
||||||
user_db = user_db_cursor.one_or_none()
|
user_db = user_db_cursor.one_or_none()
|
||||||
@ -28,8 +25,11 @@ async def get_user_id(connection: AsyncConnection, id: int) -> Optional[User]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
user_data = {
|
user_data = {
|
||||||
column.name: (getattr(user_db, column.name).name if isinstance(
|
column.name: (
|
||||||
getattr(user_db, column.name), Enum) else getattr(user_db, column.name))
|
getattr(user_db, column.name).name
|
||||||
|
if isinstance(getattr(user_db, column.name), Enum)
|
||||||
|
else getattr(user_db, column.name)
|
||||||
|
)
|
||||||
for column in account_table.columns
|
for column in account_table.columns
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,10 +40,7 @@ async def get_user_login(connection: AsyncConnection, login: str) -> Optional[Us
|
|||||||
"""
|
"""
|
||||||
Получает юзера по login.
|
Получает юзера по login.
|
||||||
"""
|
"""
|
||||||
query = (
|
query = select(account_table).where(account_table.c.login == login)
|
||||||
select(account_table)
|
|
||||||
.where(account_table.c.login == login)
|
|
||||||
)
|
|
||||||
|
|
||||||
user_db_cursor = await connection.execute(query)
|
user_db_cursor = await connection.execute(query)
|
||||||
user_db = user_db_cursor.one_or_none()
|
user_db = user_db_cursor.one_or_none()
|
||||||
@ -52,8 +49,11 @@ async def get_user_login(connection: AsyncConnection, login: str) -> Optional[Us
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
user_data = {
|
user_data = {
|
||||||
column.name: (getattr(user_db, column.name).name if isinstance(
|
column.name: (
|
||||||
getattr(user_db, column.name), Enum) else getattr(user_db, column.name))
|
getattr(user_db, column.name).name
|
||||||
|
if isinstance(getattr(user_db, column.name), Enum)
|
||||||
|
else getattr(user_db, column.name)
|
||||||
|
)
|
||||||
for column in account_table.columns
|
for column in account_table.columns
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,11 +64,7 @@ async def update_user_id(connection: AsyncConnection, update_values, user) -> Op
|
|||||||
"""
|
"""
|
||||||
Вносит изменеия в нужное поле таблицы account_table.
|
Вносит изменеия в нужное поле таблицы account_table.
|
||||||
"""
|
"""
|
||||||
await connection.execute(
|
await connection.execute(account_table.update().where(account_table.c.id == user.id).values(**update_values))
|
||||||
account_table.update()
|
|
||||||
.where(account_table.c.id == user.id)
|
|
||||||
.values(**update_values)
|
|
||||||
)
|
|
||||||
|
|
||||||
await connection.commit()
|
await connection.commit()
|
||||||
|
|
||||||
@ -86,11 +82,9 @@ async def create_user(connection: AsyncConnection, user: User, creator_id: int)
|
|||||||
meta=user.meta,
|
meta=user.meta,
|
||||||
creator_id=creator_id,
|
creator_id=creator_id,
|
||||||
created_at=datetime.now(timezone.utc),
|
created_at=datetime.now(timezone.utc),
|
||||||
status=user.status.value
|
status=user.status.value,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await connection.execute(query)
|
await connection.execute(query)
|
||||||
|
|
||||||
await connection.commit()
|
await connection.commit()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from sqlalchemy import select, update
|
from sqlalchemy import select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
@ -11,16 +11,14 @@ from api.schemas.account.account_keyring import AccountKeyring
|
|||||||
|
|
||||||
from api.utils.key_id_gen import KeyIdGenerator
|
from api.utils.key_id_gen import KeyIdGenerator
|
||||||
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
|
||||||
async def get_user(connection: AsyncConnection, login: str) -> Optional[User]:
|
async def get_user(connection: AsyncConnection, login: str) -> Optional[User]:
|
||||||
|
|
||||||
query = (
|
query = (
|
||||||
select(account_table, account_keyring_table)
|
select(account_table, account_keyring_table)
|
||||||
.join(account_keyring_table, account_table.c.id == account_keyring_table.c.owner_id)
|
.join(account_keyring_table, account_table.c.id == account_keyring_table.c.owner_id)
|
||||||
.where(account_table.c.login == login,
|
.where(account_table.c.login == login, account_keyring_table.c.key_type == KeyType.PASSWORD)
|
||||||
account_keyring_table.c.key_type == KeyType.PASSWORD)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
user_db_cursor = await connection.execute(query)
|
user_db_cursor = await connection.execute(query)
|
||||||
@ -29,16 +27,21 @@ async def get_user(connection: AsyncConnection, login: str) -> Optional[User]:
|
|||||||
if not user_db:
|
if not user_db:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
|
||||||
user_data = {
|
user_data = {
|
||||||
column.name: (getattr(user_db, column.name).name if isinstance(
|
column.name: (
|
||||||
getattr(user_db, column.name), Enum) else getattr(user_db, column.name))
|
getattr(user_db, column.name).name
|
||||||
|
if isinstance(getattr(user_db, column.name), Enum)
|
||||||
|
else getattr(user_db, column.name)
|
||||||
|
)
|
||||||
for column in account_table.columns
|
for column in account_table.columns
|
||||||
}
|
}
|
||||||
|
|
||||||
password_data = {
|
password_data = {
|
||||||
column.name: (getattr(user_db, column.name).name if isinstance(
|
column.name: (
|
||||||
getattr(user_db, column.name), Enum) else getattr(user_db, column.name))
|
getattr(user_db, column.name).name
|
||||||
|
if isinstance(getattr(user_db, column.name), Enum)
|
||||||
|
else getattr(user_db, column.name)
|
||||||
|
)
|
||||||
for column in account_keyring_table.columns
|
for column in account_keyring_table.columns
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,8 +50,7 @@ async def get_user(connection: AsyncConnection, login: str) -> Optional[User]:
|
|||||||
return user, password
|
return user, password
|
||||||
|
|
||||||
|
|
||||||
async def upgrade_old_refresh_token(connection: AsyncConnection, user,refresh_token) -> Optional[User]:
|
async def upgrade_old_refresh_token(connection: AsyncConnection, user, refresh_token) -> Optional[User]:
|
||||||
|
|
||||||
new_status = KeyStatus.EXPIRED
|
new_status = KeyStatus.EXPIRED
|
||||||
|
|
||||||
update_query = (
|
update_query = (
|
||||||
@ -57,7 +59,7 @@ async def upgrade_old_refresh_token(connection: AsyncConnection, user,refresh_to
|
|||||||
account_table.c.id == user.id,
|
account_table.c.id == user.id,
|
||||||
account_keyring_table.c.status == KeyStatus.ACTIVE,
|
account_keyring_table.c.status == KeyStatus.ACTIVE,
|
||||||
account_keyring_table.c.key_type == KeyType.REFRESH_TOKEN,
|
account_keyring_table.c.key_type == KeyType.REFRESH_TOKEN,
|
||||||
account_keyring_table.c.key_value == refresh_token
|
account_keyring_table.c.key_value == refresh_token,
|
||||||
)
|
)
|
||||||
.values(status=new_status)
|
.values(status=new_status)
|
||||||
)
|
)
|
||||||
@ -67,8 +69,9 @@ async def upgrade_old_refresh_token(connection: AsyncConnection, user,refresh_to
|
|||||||
await connection.commit()
|
await connection.commit()
|
||||||
|
|
||||||
|
|
||||||
async def add_new_refresh_token(connection: AsyncConnection, new_refresh_token, new_refresh_token_expires_time, user) -> Optional[User]:
|
async def add_new_refresh_token(
|
||||||
|
connection: AsyncConnection, new_refresh_token, new_refresh_token_expires_time, user
|
||||||
|
) -> Optional[User]:
|
||||||
new_refresh_token = account_keyring_table.insert().values(
|
new_refresh_token = account_keyring_table.insert().values(
|
||||||
owner_id=user.id,
|
owner_id=user.id,
|
||||||
key_type=KeyType.REFRESH_TOKEN,
|
key_type=KeyType.REFRESH_TOKEN,
|
||||||
|
@ -5,21 +5,17 @@ from enum import Enum
|
|||||||
from sqlalchemy import insert, select
|
from sqlalchemy import insert, select
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||||
|
|
||||||
from api.db.tables.account import account_keyring_table
|
from api.db.tables.account import account_keyring_table
|
||||||
|
|
||||||
from api.schemas.account.account_keyring import AccountKeyring
|
from api.schemas.account.account_keyring import AccountKeyring
|
||||||
from api.schemas.endpoints.account_keyring import AccountKeyringUpdate, StatusKey, TypeKey
|
from api.schemas.endpoints.account_keyring import AccountKeyringUpdate, StatusKey, TypeKey
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def get_key_id(connection: AsyncConnection, key_id: str) -> Optional[AccountKeyring]:
|
async def get_key_id(connection: AsyncConnection, key_id: str) -> Optional[AccountKeyring]:
|
||||||
"""
|
"""
|
||||||
Получает key по key_id.
|
Получает key по key_id.
|
||||||
"""
|
"""
|
||||||
query = (
|
query = select(account_keyring_table).where(account_keyring_table.c.key_id == key_id)
|
||||||
select(account_keyring_table)
|
|
||||||
.where(account_keyring_table.c.key_id == key_id)
|
|
||||||
)
|
|
||||||
|
|
||||||
user_db_cursor = await connection.execute(query)
|
user_db_cursor = await connection.execute(query)
|
||||||
user_db = user_db_cursor.one_or_none()
|
user_db = user_db_cursor.one_or_none()
|
||||||
@ -28,8 +24,11 @@ async def get_key_id(connection: AsyncConnection, key_id: str) -> Optional[Accou
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
user_data = {
|
user_data = {
|
||||||
column.name: (getattr(user_db, column.name).name if isinstance(
|
column.name: (
|
||||||
getattr(user_db, column.name), Enum) else getattr(user_db, column.name))
|
getattr(user_db, column.name).name
|
||||||
|
if isinstance(getattr(user_db, column.name), Enum)
|
||||||
|
else getattr(user_db, column.name)
|
||||||
|
)
|
||||||
for column in account_keyring_table.columns
|
for column in account_keyring_table.columns
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,30 +40,28 @@ async def update_key_id(connection: AsyncConnection, update_values, key) -> Opti
|
|||||||
Вносит изменеия в нужное поле таблицы account_keyring_table.
|
Вносит изменеия в нужное поле таблицы account_keyring_table.
|
||||||
"""
|
"""
|
||||||
await connection.execute(
|
await connection.execute(
|
||||||
account_keyring_table.update()
|
account_keyring_table.update().where(account_keyring_table.c.key_id == key.key_id).values(**update_values)
|
||||||
.where(account_keyring_table.c.key_id == key.key_id)
|
)
|
||||||
.values(**update_values)
|
|
||||||
)
|
|
||||||
|
|
||||||
await connection.commit()
|
await connection.commit()
|
||||||
|
|
||||||
|
|
||||||
async def create_key(connection: AsyncConnection, key: AccountKeyring, key_id:int) -> Optional[AccountKeyring]:
|
async def create_key(connection: AsyncConnection, key: AccountKeyring, key_id: int) -> Optional[AccountKeyring]:
|
||||||
"""
|
"""
|
||||||
Создает нове поле в таблице account_keyring_table).
|
Создает нове поле в таблице account_keyring_table).
|
||||||
"""
|
"""
|
||||||
query = insert(account_keyring_table).values(
|
query = insert(account_keyring_table).values(
|
||||||
owner_id = key.owner_id,
|
owner_id=key.owner_id,
|
||||||
key_type= key.key_type.value,
|
key_type=key.key_type.value,
|
||||||
key_id= key_id,
|
key_id=key_id,
|
||||||
key_value= key.key_value,
|
key_value=key.key_value,
|
||||||
created_at= datetime.now(timezone.utc),
|
created_at=datetime.now(timezone.utc),
|
||||||
expiry= key.expiry,
|
expiry=key.expiry,
|
||||||
status= key.status.value
|
status=key.status.value,
|
||||||
)
|
)
|
||||||
|
|
||||||
key.created_at= datetime.now(timezone.utc)
|
key.created_at = datetime.now(timezone.utc)
|
||||||
key.key_id= key_id
|
key.key_id = key_id
|
||||||
|
|
||||||
await connection.execute(query)
|
await connection.execute(query)
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
from . import account,events,process
|
from . import account, events, process
|
||||||
|
@ -7,13 +7,14 @@ from api.db.sql_types import UnsignedInt
|
|||||||
from api.db import metadata
|
from api.db import metadata
|
||||||
|
|
||||||
|
|
||||||
class AccountRole(str,Enum):
|
class AccountRole(str, Enum):
|
||||||
OWNER = auto()
|
OWNER = auto()
|
||||||
ADMIN = auto()
|
ADMIN = auto()
|
||||||
EDITOR = auto()
|
EDITOR = auto()
|
||||||
VIEWER = auto()
|
VIEWER = auto()
|
||||||
|
|
||||||
class AccountStatus(str,Enum):
|
|
||||||
|
class AccountStatus(str, Enum):
|
||||||
ACTIVE = auto()
|
ACTIVE = auto()
|
||||||
DISABLED = auto()
|
DISABLED = auto()
|
||||||
BLOCKED = auto()
|
BLOCKED = auto()
|
||||||
@ -21,39 +22,44 @@ class AccountStatus(str,Enum):
|
|||||||
|
|
||||||
|
|
||||||
account_table = Table(
|
account_table = Table(
|
||||||
'account', metadata,
|
"account",
|
||||||
Column('id', UnsignedInt, primary_key=True, autoincrement=True),
|
metadata,
|
||||||
Column('name', String(100), nullable=False),
|
Column("id", UnsignedInt, primary_key=True, autoincrement=True),
|
||||||
Column('login', String(100), nullable=False),
|
Column("name", String(100), nullable=False),
|
||||||
Column('email', String(100), nullable=True),
|
Column("login", String(100), nullable=False),
|
||||||
Column('bind_tenant_id', String(40), nullable=True),
|
Column("email", String(100), nullable=True),
|
||||||
Column('role', SQLAEnum(AccountRole), nullable=False),
|
Column("bind_tenant_id", String(40), nullable=True),
|
||||||
Column('meta', JSON, default={}),
|
Column("role", SQLAEnum(AccountRole), nullable=False),
|
||||||
Column('creator_id', UnsignedInt, ForeignKey('account.id'), nullable=True),
|
Column("meta", JSON, default={}),
|
||||||
Column('created_at', DateTime(timezone=True), server_default=func.now()),
|
Column("creator_id", UnsignedInt, ForeignKey("account.id"), nullable=True),
|
||||||
Column('status', SQLAEnum(AccountStatus), nullable=False),
|
Column("created_at", DateTime(timezone=True), server_default=func.now()),
|
||||||
|
Column("status", SQLAEnum(AccountStatus), nullable=False),
|
||||||
Index('idx_login', 'login'),
|
Index("idx_login", "login"),
|
||||||
Index('idx_name', 'name'),
|
Index("idx_name", "name"),
|
||||||
)
|
)
|
||||||
|
|
||||||
class KeyType(str,Enum):
|
|
||||||
|
class KeyType(str, Enum):
|
||||||
PASSWORD = auto()
|
PASSWORD = auto()
|
||||||
ACCESS_TOKEN = auto()
|
ACCESS_TOKEN = auto()
|
||||||
REFRESH_TOKEN = auto()
|
REFRESH_TOKEN = auto()
|
||||||
API_KEY = auto()
|
API_KEY = auto()
|
||||||
|
|
||||||
class KeyStatus(str,Enum):
|
|
||||||
|
class KeyStatus(str, Enum):
|
||||||
ACTIVE = auto()
|
ACTIVE = auto()
|
||||||
EXPIRED = auto()
|
EXPIRED = auto()
|
||||||
DELETED = auto()
|
DELETED = auto()
|
||||||
|
|
||||||
|
|
||||||
account_keyring_table = Table(
|
account_keyring_table = Table(
|
||||||
'account_keyring', metadata,
|
"account_keyring",
|
||||||
Column('owner_id', UnsignedInt, ForeignKey('account.id'), primary_key=True, nullable=False),
|
metadata,
|
||||||
Column('key_type', SQLAEnum(KeyType), primary_key=True, nullable=False),
|
Column("owner_id", UnsignedInt, ForeignKey("account.id"), primary_key=True, nullable=False),
|
||||||
Column('key_id', String(40),primary_key=True, default=None),
|
Column("key_type", SQLAEnum(KeyType), primary_key=True, nullable=False),
|
||||||
Column('key_value', String(255), nullable=False),
|
Column("key_id", String(40), primary_key=True, default=None),
|
||||||
Column('created_at', DateTime(timezone=True), server_default=func.now()),
|
Column("key_value", String(255), nullable=False),
|
||||||
Column('expiry', DateTime(timezone=True), nullable=True),
|
Column("created_at", DateTime(timezone=True), server_default=func.now()),
|
||||||
Column('status', SQLAEnum(KeyStatus), nullable=False), )
|
Column("expiry", DateTime(timezone=True), nullable=True),
|
||||||
|
Column("status", SQLAEnum(KeyStatus), nullable=False),
|
||||||
|
)
|
||||||
|
@ -6,10 +6,12 @@ from api.db.sql_types import UnsignedInt
|
|||||||
|
|
||||||
from api.db import metadata
|
from api.db import metadata
|
||||||
|
|
||||||
|
|
||||||
class EventState(str, Enum):
|
class EventState(str, Enum):
|
||||||
AUTO = auto()
|
AUTO = auto()
|
||||||
DESCRIPTED = auto()
|
DESCRIPTED = auto()
|
||||||
|
|
||||||
|
|
||||||
class EventStatus(str, Enum):
|
class EventStatus(str, Enum):
|
||||||
ACTIVE = auto()
|
ACTIVE = auto()
|
||||||
DISABLED = auto()
|
DISABLED = auto()
|
||||||
@ -17,13 +19,14 @@ class EventStatus(str, Enum):
|
|||||||
|
|
||||||
|
|
||||||
list_events_table = Table(
|
list_events_table = Table(
|
||||||
'list_events', metadata,
|
"list_events",
|
||||||
Column('id', UnsignedInt, primary_key=True, autoincrement=True),
|
metadata,
|
||||||
Column('name', String(40, collation='latin1_bin'), nullable=False,unique=True),
|
Column("id", UnsignedInt, primary_key=True, autoincrement=True),
|
||||||
Column('title', String(64), nullable=False),
|
Column("name", String(40, collation="latin1_bin"), nullable=False, unique=True),
|
||||||
Column('creator_id', UnsignedInt, ForeignKey('account.id'), nullable=False),
|
Column("title", String(64), nullable=False),
|
||||||
Column('created_at', DateTime(timezone=True), server_default=func.now()),
|
Column("creator_id", UnsignedInt, ForeignKey("account.id"), nullable=False),
|
||||||
Column('schema', JSON, default={}),
|
Column("created_at", DateTime(timezone=True), server_default=func.now()),
|
||||||
Column('state', SQLAEnum(EventState), nullable=False),
|
Column("schema", JSON, default={}),
|
||||||
Column('status', SQLAEnum(EventStatus), nullable=False),
|
Column("state", SQLAEnum(EventState), nullable=False),
|
||||||
|
Column("status", SQLAEnum(EventStatus), nullable=False),
|
||||||
)
|
)
|
||||||
|
@ -1,4 +1,16 @@
|
|||||||
from sqlalchemy import Table, Column, Integer, String, Text, Enum as SQLAEnum, JSON, ForeignKey, DateTime, Index, PrimaryKeyConstraint
|
from sqlalchemy import (
|
||||||
|
Table,
|
||||||
|
Column,
|
||||||
|
Integer,
|
||||||
|
String,
|
||||||
|
Text,
|
||||||
|
Enum as SQLAEnum,
|
||||||
|
JSON,
|
||||||
|
ForeignKey,
|
||||||
|
DateTime,
|
||||||
|
Index,
|
||||||
|
PrimaryKeyConstraint,
|
||||||
|
)
|
||||||
from sqlalchemy.sql import func
|
from sqlalchemy.sql import func
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
|
|
||||||
@ -7,7 +19,6 @@ from api.db.sql_types import UnsignedInt
|
|||||||
from api.db import metadata
|
from api.db import metadata
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ProcessStatus(str, Enum):
|
class ProcessStatus(str, Enum):
|
||||||
ACTIVE = auto()
|
ACTIVE = auto()
|
||||||
STOPPING = auto()
|
STOPPING = auto()
|
||||||
@ -16,69 +27,80 @@ class ProcessStatus(str, Enum):
|
|||||||
|
|
||||||
|
|
||||||
process_schema_table = Table(
|
process_schema_table = Table(
|
||||||
'process_schema', metadata,
|
"process_schema",
|
||||||
Column('id', UnsignedInt, primary_key=True, autoincrement=True),
|
metadata,
|
||||||
Column('title', String(100), nullable=False),
|
Column("id", UnsignedInt, primary_key=True, autoincrement=True),
|
||||||
Column('description', Text, nullable=False),
|
Column("title", String(100), nullable=False),
|
||||||
Column('owner_id', UnsignedInt, ForeignKey('account.id'), nullable=False),
|
Column("description", Text, nullable=False),
|
||||||
Column('creator_id', UnsignedInt, ForeignKey('account.id'), nullable=False),
|
Column("owner_id", UnsignedInt, ForeignKey("account.id"), nullable=False),
|
||||||
Column('created_at', DateTime(timezone=True), server_default=func.now()),
|
Column("creator_id", UnsignedInt, ForeignKey("account.id"), nullable=False),
|
||||||
Column('settings', JSON, default={}),
|
Column("created_at", DateTime(timezone=True), server_default=func.now()),
|
||||||
Column('status', SQLAEnum(ProcessStatus), nullable=False),
|
Column("settings", JSON, default={}),
|
||||||
|
Column("status", SQLAEnum(ProcessStatus), nullable=False),
|
||||||
Index('idx_owner_id', 'owner_id',)
|
Index(
|
||||||
|
"idx_owner_id",
|
||||||
|
"owner_id",
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
process_version_archive_table = Table(
|
process_version_archive_table = Table(
|
||||||
'process_version_archive', metadata,
|
"process_version_archive",
|
||||||
Column('id', UnsignedInt, autoincrement=True, nullable=False),
|
metadata,
|
||||||
Column('ps_id', UnsignedInt, ForeignKey('process_schema.id'), nullable=False),
|
Column("id", UnsignedInt, autoincrement=True, nullable=False),
|
||||||
Column('version', UnsignedInt, default=1, nullable=False),
|
Column("ps_id", UnsignedInt, ForeignKey("process_schema.id"), nullable=False),
|
||||||
Column('snapshot', JSON, default={}),
|
Column("version", UnsignedInt, default=1, nullable=False),
|
||||||
Column('owner_id', UnsignedInt, ForeignKey('account.id'), nullable=False),
|
Column("snapshot", JSON, default={}),
|
||||||
Column('created_at', DateTime(timezone=True), server_default=func.now()),
|
Column("owner_id", UnsignedInt, ForeignKey("account.id"), nullable=False),
|
||||||
Column('is_last', UnsignedInt, default=0),
|
Column("created_at", DateTime(timezone=True), server_default=func.now()),
|
||||||
PrimaryKeyConstraint('id', 'version') )
|
Column("is_last", UnsignedInt, default=0),
|
||||||
|
PrimaryKeyConstraint("id", "version"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NodeStatus(str, Enum):
|
class NodeStatus(str, Enum):
|
||||||
ACTIVE = auto()
|
ACTIVE = auto()
|
||||||
DISABLED = auto()
|
DISABLED = auto()
|
||||||
DELETED = auto()
|
DELETED = auto()
|
||||||
|
|
||||||
|
|
||||||
class NodeType(Enum):
|
class NodeType(Enum):
|
||||||
TYPE1 = 'Type1'
|
TYPE1 = "Type1"
|
||||||
TYPE2 = 'Type2'
|
TYPE2 = "Type2"
|
||||||
TYPE3 = 'Type3'
|
TYPE3 = "Type3"
|
||||||
|
|
||||||
|
|
||||||
ps_node_table = Table(
|
ps_node_table = Table(
|
||||||
'ps_node', metadata,
|
"ps_node",
|
||||||
Column('id', UnsignedInt, autoincrement=True, primary_key=True, nullable=False),
|
metadata,
|
||||||
Column('ps_id', UnsignedInt, ForeignKey('process_schema.id'), nullable=False),
|
Column("id", UnsignedInt, autoincrement=True, primary_key=True, nullable=False),
|
||||||
Column('node_type', SQLAEnum(NodeType), nullable=False),
|
Column("ps_id", UnsignedInt, ForeignKey("process_schema.id"), nullable=False),
|
||||||
Column('settings', JSON, default={}),
|
Column("node_type", SQLAEnum(NodeType), nullable=False),
|
||||||
Column('creator_id', UnsignedInt, ForeignKey('account.id'), nullable=False),
|
Column("settings", JSON, default={}),
|
||||||
Column('created_at', DateTime(timezone=True), server_default=func.now()),
|
Column("creator_id", UnsignedInt, ForeignKey("account.id"), nullable=False),
|
||||||
Column('status', SQLAEnum(NodeStatus), nullable=False),
|
Column("created_at", DateTime(timezone=True), server_default=func.now()),
|
||||||
|
Column("status", SQLAEnum(NodeStatus), nullable=False),
|
||||||
Index('idx_ps_id', 'ps_id')
|
Index("idx_ps_id", "ps_id"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class NodeLinkStatus(str, Enum):
|
class NodeLinkStatus(str, Enum):
|
||||||
ACTIVE = auto()
|
ACTIVE = auto()
|
||||||
STOPPING = auto()
|
STOPPING = auto()
|
||||||
STOPPED = auto()
|
STOPPED = auto()
|
||||||
DELETED = auto()
|
DELETED = auto()
|
||||||
|
|
||||||
node_link_table = Table(
|
|
||||||
'node_link', metadata,
|
|
||||||
Column('id', UnsignedInt, autoincrement=True, primary_key=True, nullable=False),
|
|
||||||
Column('link_name', String(20), nullable=False),
|
|
||||||
Column('node_id', UnsignedInt, ForeignKey('ps_node.id'), nullable=False),
|
|
||||||
Column('next_node_id', UnsignedInt, ForeignKey('ps_node.id'), nullable=False),
|
|
||||||
Column('settings', JSON, default={}),
|
|
||||||
Column('creator_id', UnsignedInt, ForeignKey('account.id'), nullable=False),
|
|
||||||
Column('created_at', DateTime(timezone=True), server_default=func.now()),
|
|
||||||
Column('status', SQLAEnum(NodeLinkStatus),nullable=False),
|
|
||||||
|
|
||||||
Index('idx_node_id', 'node_id'),
|
node_link_table = Table(
|
||||||
Index('idx_next_node_id', 'next_node_id'))
|
"node_link",
|
||||||
|
metadata,
|
||||||
|
Column("id", UnsignedInt, autoincrement=True, primary_key=True, nullable=False),
|
||||||
|
Column("link_name", String(20), nullable=False),
|
||||||
|
Column("node_id", UnsignedInt, ForeignKey("ps_node.id"), nullable=False),
|
||||||
|
Column("next_node_id", UnsignedInt, ForeignKey("ps_node.id"), nullable=False),
|
||||||
|
Column("settings", JSON, default={}),
|
||||||
|
Column("creator_id", UnsignedInt, ForeignKey("account.id"), nullable=False),
|
||||||
|
Column("created_at", DateTime(timezone=True), server_default=func.now()),
|
||||||
|
Column("status", SQLAEnum(NodeLinkStatus), nullable=False),
|
||||||
|
Index("idx_node_id", "node_id"),
|
||||||
|
Index("idx_next_node_id", "next_node_id"),
|
||||||
|
)
|
||||||
|
@ -2,11 +2,8 @@ from api.endpoints.auth import api_router as auth_router
|
|||||||
from api.endpoints.profile import api_router as profile_router
|
from api.endpoints.profile import api_router as profile_router
|
||||||
from api.endpoints.account import api_router as account_router
|
from api.endpoints.account import api_router as account_router
|
||||||
from api.endpoints.keyring import api_router as keyring_router
|
from api.endpoints.keyring import api_router as keyring_router
|
||||||
list_of_routes = [
|
|
||||||
auth_router,
|
list_of_routes = [auth_router, profile_router, account_router, keyring_router]
|
||||||
profile_router,
|
|
||||||
account_router,
|
|
||||||
keyring_router]
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"list_of_routes",
|
"list_of_routes",
|
||||||
|
@ -14,9 +14,9 @@ from sqlalchemy.ext.asyncio import AsyncConnection
|
|||||||
|
|
||||||
from api.db.connection.session import get_connection_dep
|
from api.db.connection.session import get_connection_dep
|
||||||
|
|
||||||
from api.db.logic.account import get_user_id, update_user_id, create_user,get_user_login
|
from api.db.logic.account import get_user_id, update_user_id, create_user, get_user_login
|
||||||
|
|
||||||
from api.schemas.account.account import User,Status
|
from api.schemas.account.account import User, Status
|
||||||
from api.schemas.endpoints.account import UserUpdate
|
from api.schemas.endpoints.account import UserUpdate
|
||||||
|
|
||||||
|
|
||||||
@ -30,34 +30,21 @@ api_router = APIRouter(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@api_router.get("/{user_id}")
|
@api_router.get("/{user_id}")
|
||||||
async def get_account(user_id: int,
|
async def get_account(user_id: int, request: Request, connection: AsyncConnection = Depends(get_connection_dep)):
|
||||||
request: Request,
|
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
|
||||||
):
|
|
||||||
|
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
|
|
||||||
user = await get_user_id(connection, user_id)
|
user = await get_user_id(connection, user_id)
|
||||||
|
|
||||||
if user is None:
|
if user is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="Account not found")
|
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
@api_router.post("")
|
@api_router.post("")
|
||||||
async def create_account(
|
async def create_account(user: UserUpdate, request: Request, connection: AsyncConnection = Depends(get_connection_dep)):
|
||||||
user: UserUpdate,
|
|
||||||
request: Request,
|
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
|
||||||
):
|
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
@ -65,40 +52,29 @@ async def create_account(
|
|||||||
user_validation = await get_user_login(connection, user.login)
|
user_validation = await get_user_login(connection, user.login)
|
||||||
|
|
||||||
if user_validation is None:
|
if user_validation is None:
|
||||||
|
await create_user(connection, user, authorize_user.id)
|
||||||
await create_user(connection,user,authorize_user.id)
|
|
||||||
user_new = await get_user_login(connection, user.login)
|
user_new = await get_user_login(connection, user.login)
|
||||||
return user_new
|
return user_new
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST, detail="An account with this information already exists."
|
||||||
detail="An account with this information already exists.")
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@api_router.put("/{user_id}")
|
@api_router.put("/{user_id}")
|
||||||
async def update_account(
|
async def update_account(
|
||||||
user_id: int,
|
user_id: int, request: Request, user_update: UserUpdate, connection: AsyncConnection = Depends(get_connection_dep)
|
||||||
request: Request,
|
):
|
||||||
user_update: UserUpdate,
|
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
|
||||||
):
|
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
|
|
||||||
|
|
||||||
user = await get_user_id(connection, user_id)
|
user = await get_user_id(connection, user_id)
|
||||||
if user is None:
|
if user is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="Account not found")
|
|
||||||
|
|
||||||
|
update_values = update_user_data_changes(user_update, user)
|
||||||
update_values = update_user_data_changes(user_update,user)
|
|
||||||
|
|
||||||
if update_values is None:
|
if update_values is None:
|
||||||
return user
|
return user
|
||||||
@ -107,42 +83,30 @@ async def update_account(
|
|||||||
|
|
||||||
await update_user_id(connection, update_values, user)
|
await update_user_id(connection, update_values, user)
|
||||||
|
|
||||||
|
|
||||||
user = await get_user_id(connection, user_id)
|
user = await get_user_id(connection, user_id)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
@api_router.delete("/{user_id}")
|
@api_router.delete("/{user_id}")
|
||||||
async def delete_account(
|
async def delete_account(user_id: int, request: Request, connection: AsyncConnection = Depends(get_connection_dep)):
|
||||||
user_id: int,
|
|
||||||
request: Request,
|
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
|
||||||
):
|
|
||||||
|
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
|
|
||||||
|
|
||||||
user = await get_user_id(connection, user_id)
|
user = await get_user_id(connection, user_id)
|
||||||
if user is None:
|
if user is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="Account not found")
|
|
||||||
|
|
||||||
|
|
||||||
user_update = UserUpdate(status=Status.DELETED.value)
|
user_update = UserUpdate(status=Status.DELETED.value)
|
||||||
|
|
||||||
update_values = update_user_data_changes(user_update,user)
|
update_values = update_user_data_changes(user_update, user)
|
||||||
|
|
||||||
if update_values is None:
|
if update_values is None:
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
await update_user_id(connection, update_values, user)
|
await update_user_id(connection, update_values, user)
|
||||||
|
|
||||||
|
|
||||||
user = await get_user_id(connection, user_id)
|
user = await get_user_id(connection, user_id)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
@ -24,7 +24,7 @@ from api.config import get_settings
|
|||||||
from api.db.connection.session import get_connection_dep
|
from api.db.connection.session import get_connection_dep
|
||||||
from api.services.auth import authenticate_user
|
from api.services.auth import authenticate_user
|
||||||
|
|
||||||
from api.db.logic.auth import add_new_refresh_token,upgrade_old_refresh_token
|
from api.db.logic.auth import add_new_refresh_token, upgrade_old_refresh_token
|
||||||
|
|
||||||
from api.schemas.endpoints.auth import Auth
|
from api.schemas.endpoints.auth import Auth
|
||||||
|
|
||||||
@ -54,12 +54,11 @@ def get_config():
|
|||||||
|
|
||||||
@api_router.post("")
|
@api_router.post("")
|
||||||
async def login_for_access_token(
|
async def login_for_access_token(
|
||||||
user: Auth,
|
user: Auth,
|
||||||
response: Response,
|
response: Response,
|
||||||
connection: AsyncConnection = Depends(get_connection_dep),
|
connection: AsyncConnection = Depends(get_connection_dep),
|
||||||
Authorize: AuthJWT = Depends(),
|
Authorize: AuthJWT = Depends(),
|
||||||
):
|
):
|
||||||
|
|
||||||
"""Авторизирует, выставляет токены в куки."""
|
"""Авторизирует, выставляет токены в куки."""
|
||||||
|
|
||||||
user = await authenticate_user(connection, user.login, user.password)
|
user = await authenticate_user(connection, user.login, user.password)
|
||||||
@ -73,44 +72,33 @@ async def login_for_access_token(
|
|||||||
# headers={"WWW-Authenticate": "Bearer"},
|
# headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
|
|
||||||
access_token_expires = timedelta(
|
access_token_expires = timedelta(minutes=get_settings().ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
minutes=get_settings().ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
||||||
|
|
||||||
refresh_token_expires = timedelta(
|
refresh_token_expires = timedelta(days=get_settings().REFRESH_TOKEN_EXPIRE_DAYS)
|
||||||
days=get_settings().REFRESH_TOKEN_EXPIRE_DAYS
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.debug(f"refresh_token_expires {refresh_token_expires}")
|
logger.debug(f"refresh_token_expires {refresh_token_expires}")
|
||||||
|
|
||||||
access_token = Authorize.create_access_token(
|
access_token = Authorize.create_access_token(subject=user.login, expires_time=access_token_expires)
|
||||||
subject=user.login, expires_time=access_token_expires
|
refresh_token = Authorize.create_refresh_token(subject=user.login, expires_time=refresh_token_expires)
|
||||||
)
|
|
||||||
refresh_token = Authorize.create_refresh_token(
|
|
||||||
subject=user.login, expires_time=refresh_token_expires
|
|
||||||
)
|
|
||||||
|
|
||||||
refresh_token_expires_time = datetime.now(timezone.utc) + refresh_token_expires
|
refresh_token_expires_time = datetime.now(timezone.utc) + refresh_token_expires
|
||||||
|
|
||||||
await add_new_refresh_token(connection,refresh_token,refresh_token_expires_time,user)
|
await add_new_refresh_token(connection, refresh_token, refresh_token_expires_time, user)
|
||||||
|
|
||||||
Authorize.set_refresh_cookies(refresh_token)
|
Authorize.set_refresh_cookies(refresh_token)
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"access_token": access_token,
|
"access_token": access_token,
|
||||||
# "access_token_expires": access_token_expires_time,
|
# "access_token_expires": access_token_expires_time,
|
||||||
# "refresh_token": refresh_token,
|
# "refresh_token": refresh_token,
|
||||||
# "refresh_token_expires": refresh_token_expires_time
|
# "refresh_token_expires": refresh_token_expires_time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@api_router.post("/refresh")
|
@api_router.post("/refresh")
|
||||||
async def refresh(
|
async def refresh(
|
||||||
request: Request,
|
request: Request, connection: AsyncConnection = Depends(get_connection_dep), Authorize: AuthJWT = Depends()
|
||||||
connection: AsyncConnection = Depends(get_connection_dep),
|
):
|
||||||
Authorize: AuthJWT = Depends()
|
|
||||||
):
|
|
||||||
|
|
||||||
refresh_token = request.cookies.get("refresh_token_cookie")
|
refresh_token = request.cookies.get("refresh_token_cookie")
|
||||||
# print("Refresh Token:", refresh_token)
|
# print("Refresh Token:", refresh_token)
|
||||||
|
|
||||||
@ -118,29 +106,24 @@ async def refresh(
|
|||||||
raise HTTPException(status_code=401, detail="Refresh token is missing")
|
raise HTTPException(status_code=401, detail="Refresh token is missing")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
Authorize.jwt_refresh_token_required()
|
Authorize.jwt_refresh_token_required()
|
||||||
current_user = Authorize.get_jwt_subject()
|
current_user = Authorize.get_jwt_subject()
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
await upgrade_old_refresh_token(connection, current_user, refresh_token)
|
||||||
await upgrade_old_refresh_token(connection,current_user,refresh_token)
|
|
||||||
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Invalid refresh token",
|
detail="Invalid refresh token",
|
||||||
)
|
)
|
||||||
|
|
||||||
access_token_expires = timedelta(minutes=get_settings().ACCESS_TOKEN_EXPIRE_MINUTES)
|
access_token_expires = timedelta(minutes=get_settings().ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
|
|
||||||
new_access_token = Authorize.create_access_token(
|
new_access_token = Authorize.create_access_token(subject=current_user, expires_time=access_token_expires)
|
||||||
subject=current_user, expires_time=access_token_expires
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"access_token": new_access_token,
|
"access_token": new_access_token,
|
||||||
# "access_token_expires": access_token_expires_time,
|
# "access_token_expires": access_token_expires_time,
|
||||||
# "refresh_token": refresh_token,
|
# "refresh_token": refresh_token,
|
||||||
# "refresh_token_expires": refresh_token_expires_time
|
# "refresh_token_expires": refresh_token_expires_time
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ from sqlalchemy.ext.asyncio import AsyncConnection
|
|||||||
|
|
||||||
from api.db.connection.session import get_connection_dep
|
from api.db.connection.session import get_connection_dep
|
||||||
|
|
||||||
from api.db.logic.keyring import get_key_id,create_key,update_key_id
|
from api.db.logic.keyring import get_key_id, create_key, update_key_id
|
||||||
|
|
||||||
|
|
||||||
from api.schemas.account.account import Status
|
from api.schemas.account.account import Status
|
||||||
@ -33,12 +33,7 @@ api_router = APIRouter(
|
|||||||
|
|
||||||
|
|
||||||
@api_router.get("/{user_id}/{key_id}")
|
@api_router.get("/{user_id}/{key_id}")
|
||||||
async def get_keyring(
|
async def get_keyring(key_id: str, request: Request, connection: AsyncConnection = Depends(get_connection_dep)):
|
||||||
key_id: str,
|
|
||||||
request: Request,
|
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
|
||||||
):
|
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
@ -46,22 +41,19 @@ async def get_keyring(
|
|||||||
keyring = await get_key_id(connection, key_id)
|
keyring = await get_key_id(connection, key_id)
|
||||||
|
|
||||||
if keyring is None:
|
if keyring is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Key not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="Key not found")
|
|
||||||
|
|
||||||
return keyring
|
return keyring
|
||||||
|
|
||||||
|
|
||||||
@api_router.post("/{user_id}/{key_id}")
|
@api_router.post("/{user_id}/{key_id}")
|
||||||
async def create_keyring(
|
async def create_keyring(
|
||||||
user_id: int,
|
user_id: int,
|
||||||
key_id: str,
|
key_id: str,
|
||||||
request: Request,
|
request: Request,
|
||||||
key: AccountKeyringUpdate,
|
key: AccountKeyringUpdate,
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
connection: AsyncConnection = Depends(get_connection_dep),
|
||||||
):
|
):
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
@ -69,82 +61,70 @@ async def create_keyring(
|
|||||||
keyring = await get_key_id(connection, key_id)
|
keyring = await get_key_id(connection, key_id)
|
||||||
|
|
||||||
if keyring is None:
|
if keyring is None:
|
||||||
user_new = await create_key(connection,key, key_id, )
|
user_new = await create_key(
|
||||||
|
connection,
|
||||||
|
key,
|
||||||
|
key_id,
|
||||||
|
)
|
||||||
return user_new
|
return user_new
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST, detail="An keyring with this information already exists."
|
||||||
detail="An keyring with this information already exists.")
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@api_router.put("/{user_id}/{key_id}")
|
@api_router.put("/{user_id}/{key_id}")
|
||||||
async def update_keyring(
|
async def update_keyring(
|
||||||
user_id: int,
|
user_id: int,
|
||||||
key_id: str,
|
key_id: str,
|
||||||
request: Request,
|
request: Request,
|
||||||
keyring_update: AccountKeyringUpdate,
|
keyring_update: AccountKeyringUpdate,
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
connection: AsyncConnection = Depends(get_connection_dep),
|
||||||
):
|
):
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
|
|
||||||
|
|
||||||
keyring = await get_key_id(connection, key_id)
|
keyring = await get_key_id(connection, key_id)
|
||||||
if keyring is None:
|
if keyring is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="keyring not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="keyring not found")
|
|
||||||
|
|
||||||
|
update_values = update_key_data_changes(keyring_update, keyring)
|
||||||
update_values = update_key_data_changes(keyring_update,keyring)
|
|
||||||
|
|
||||||
if update_values is None:
|
if update_values is None:
|
||||||
return keyring
|
return keyring
|
||||||
|
|
||||||
keyring_update_data = AccountKeyring.model_validate({**keyring.model_dump(), **update_values})
|
keyring_update_data = AccountKeyring.model_validate({**keyring.model_dump(), **update_values})
|
||||||
|
|
||||||
|
|
||||||
await update_key_id(connection, update_values, keyring)
|
await update_key_id(connection, update_values, keyring)
|
||||||
|
|
||||||
|
|
||||||
keyring = await get_key_id(connection, key_id)
|
keyring = await get_key_id(connection, key_id)
|
||||||
|
|
||||||
return keyring
|
return keyring
|
||||||
|
|
||||||
|
|
||||||
@api_router.delete("/{user_id}/{key_id}")
|
@api_router.delete("/{user_id}/{key_id}")
|
||||||
async def delete_keyring(
|
async def delete_keyring(
|
||||||
user_id: int,
|
user_id: int, key_id: str, request: Request, connection: AsyncConnection = Depends(get_connection_dep)
|
||||||
key_id: str,
|
):
|
||||||
request: Request,
|
|
||||||
connection: AsyncConnection = Depends(get_connection_dep)
|
|
||||||
):
|
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
authorize_user = await db_user_role_validation(connection, current_user)
|
authorize_user = await db_user_role_validation(connection, current_user)
|
||||||
|
|
||||||
|
|
||||||
keyring = await get_key_id(connection, key_id)
|
keyring = await get_key_id(connection, key_id)
|
||||||
if keyring is None:
|
if keyring is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="keyring not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="keyring not found")
|
|
||||||
|
|
||||||
|
|
||||||
keyring_update = AccountKeyringUpdate(status=Status.DELETED.value)
|
keyring_update = AccountKeyringUpdate(status=Status.DELETED.value)
|
||||||
|
|
||||||
update_values = update_key_data_changes(keyring_update,keyring)
|
update_values = update_key_data_changes(keyring_update, keyring)
|
||||||
|
|
||||||
if update_values is None:
|
if update_values is None:
|
||||||
return keyring
|
return keyring
|
||||||
|
|
||||||
await update_key_id(connection, update_values, keyring)
|
await update_key_id(connection, update_values, keyring)
|
||||||
|
|
||||||
|
|
||||||
keyring = await get_key_id(connection, key_id)
|
keyring = await get_key_id(connection, key_id)
|
||||||
|
|
||||||
return keyring
|
return keyring
|
||||||
|
@ -13,13 +13,12 @@ from fastapi import (
|
|||||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||||
|
|
||||||
from api.db.connection.session import get_connection_dep
|
from api.db.connection.session import get_connection_dep
|
||||||
from api.db.logic.account import get_user_id, update_user_id,get_user_login
|
from api.db.logic.account import get_user_id, update_user_id, get_user_login
|
||||||
from api.services.update_data_validation import update_user_data_changes
|
from api.services.update_data_validation import update_user_data_changes
|
||||||
|
|
||||||
from api.schemas.endpoints.account import UserUpdate
|
from api.schemas.endpoints.account import UserUpdate
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
api_router = APIRouter(
|
api_router = APIRouter(
|
||||||
prefix="/profile",
|
prefix="/profile",
|
||||||
tags=["User accountModel"],
|
tags=["User accountModel"],
|
||||||
@ -28,50 +27,42 @@ api_router = APIRouter(
|
|||||||
|
|
||||||
@api_router.get("")
|
@api_router.get("")
|
||||||
async def get_profile(
|
async def get_profile(
|
||||||
request: Request,
|
request: Request,
|
||||||
connection: AsyncConnection = Depends(get_connection_dep),
|
connection: AsyncConnection = Depends(get_connection_dep),
|
||||||
):
|
):
|
||||||
# Извлекаем текущего пользователя из request.state
|
# Извлекаем текущего пользователя из request.state
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
user = await get_user_login(connection, current_user)
|
user = await get_user_login(connection, current_user)
|
||||||
|
|
||||||
if user is None:
|
if user is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="Account not found")
|
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
@api_router.put("")
|
@api_router.put("")
|
||||||
async def update_profile(
|
async def update_profile(
|
||||||
request: Request,
|
request: Request,
|
||||||
user_updata: UserUpdate,
|
user_updata: UserUpdate,
|
||||||
connection: AsyncConnection = Depends(get_connection_dep),
|
connection: AsyncConnection = Depends(get_connection_dep),
|
||||||
|
):
|
||||||
):
|
|
||||||
|
|
||||||
current_user = request.state.current_user
|
current_user = request.state.current_user
|
||||||
|
|
||||||
user = await get_user_login(connection, current_user)
|
user = await get_user_login(connection, current_user)
|
||||||
if user is None:
|
if user is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="Account not found")
|
|
||||||
|
|
||||||
if user_updata.role == None and user_updata.login == None:
|
if user_updata.role == None and user_updata.login == None:
|
||||||
update_values = update_user_data_changes(user_updata,user)
|
update_values = update_user_data_changes(user_updata, user)
|
||||||
|
|
||||||
if update_values is None:
|
|
||||||
return user
|
|
||||||
|
|
||||||
await update_user_id(connection, update_values, user)
|
|
||||||
|
|
||||||
user = await get_user_id(connection, user.id)
|
|
||||||
|
|
||||||
|
if update_values is None:
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
await update_user_id(connection, update_values, user)
|
||||||
|
|
||||||
|
user = await get_user_id(connection, user.id)
|
||||||
|
|
||||||
|
return user
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Bad body")
|
||||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY ,
|
|
||||||
detail="Bad body")
|
|
||||||
|
@ -6,17 +6,20 @@ from pydantic import BaseModel, EmailStr, Field
|
|||||||
|
|
||||||
# Модель для хранения информации из запроса
|
# Модель для хранения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
class Role(Enum):
|
class Role(Enum):
|
||||||
OWNER = 'OWNER'
|
OWNER = "OWNER"
|
||||||
ADMIN = 'ADMIN'
|
ADMIN = "ADMIN"
|
||||||
EDITOR = 'EDITOR'
|
EDITOR = "EDITOR"
|
||||||
VIEWER = 'VIEWER'
|
VIEWER = "VIEWER"
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = 'ACTIVE'
|
ACTIVE = "ACTIVE"
|
||||||
DISABLED = 'DISABLED'
|
DISABLED = "DISABLED"
|
||||||
BLOCKED = 'BLOCKED'
|
BLOCKED = "BLOCKED"
|
||||||
DELETED = 'DELETED'
|
DELETED = "DELETED"
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
id: Optional[int] = None
|
id: Optional[int] = None
|
||||||
|
@ -6,12 +6,14 @@ from datetime import datetime
|
|||||||
|
|
||||||
# Модель для хранения информации из запроса
|
# Модель для хранения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
class TypeKey(Enum):
|
class TypeKey(Enum):
|
||||||
PASSWORD = "PASSWORD"
|
PASSWORD = "PASSWORD"
|
||||||
ACCESS_TOKEN = "ACCESS_TOKEN"
|
ACCESS_TOKEN = "ACCESS_TOKEN"
|
||||||
REFRESH_TOKEN = "REFRESH_TOKEN"
|
REFRESH_TOKEN = "REFRESH_TOKEN"
|
||||||
API_KEY = "API_KEY"
|
API_KEY = "API_KEY"
|
||||||
|
|
||||||
|
|
||||||
class StatusKey(Enum):
|
class StatusKey(Enum):
|
||||||
ACTIVE = "ACTIVE"
|
ACTIVE = "ACTIVE"
|
||||||
EXPIRED = "EXPIRED"
|
EXPIRED = "EXPIRED"
|
||||||
|
@ -5,17 +5,20 @@ from pydantic import BaseModel, EmailStr, Field
|
|||||||
|
|
||||||
# Таблица для получения информации из запроса
|
# Таблица для получения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
class Role(Enum):
|
class Role(Enum):
|
||||||
OWNER = 'OWNER'
|
OWNER = "OWNER"
|
||||||
ADMIN = 'ADMIN'
|
ADMIN = "ADMIN"
|
||||||
EDITOR = 'EDITOR'
|
EDITOR = "EDITOR"
|
||||||
VIEWER = 'VIEWER'
|
VIEWER = "VIEWER"
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = 'ACTIVE'
|
ACTIVE = "ACTIVE"
|
||||||
DISABLED = 'DISABLED'
|
DISABLED = "DISABLED"
|
||||||
BLOCKED = 'BLOCKED'
|
BLOCKED = "BLOCKED"
|
||||||
DELETED = 'DELETED'
|
DELETED = "DELETED"
|
||||||
|
|
||||||
|
|
||||||
class UserUpdate(BaseModel):
|
class UserUpdate(BaseModel):
|
||||||
id: Optional[int] = None
|
id: Optional[int] = None
|
||||||
|
@ -6,12 +6,14 @@ from datetime import datetime
|
|||||||
|
|
||||||
# Таблица для получения информации из запроса
|
# Таблица для получения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
class TypeKey(Enum):
|
class TypeKey(Enum):
|
||||||
PASSWORD = "PASSWORD"
|
PASSWORD = "PASSWORD"
|
||||||
ACCESS_TOKEN = "ACCESS_TOKEN"
|
ACCESS_TOKEN = "ACCESS_TOKEN"
|
||||||
REFRESH_TOKEN = "REFRESH_TOKEN"
|
REFRESH_TOKEN = "REFRESH_TOKEN"
|
||||||
API_KEY = "API_KEY"
|
API_KEY = "API_KEY"
|
||||||
|
|
||||||
|
|
||||||
class StatusKey(Enum):
|
class StatusKey(Enum):
|
||||||
ACTIVE = "ACTIVE"
|
ACTIVE = "ACTIVE"
|
||||||
EXPIRED = "EXPIRED"
|
EXPIRED = "EXPIRED"
|
||||||
|
@ -2,6 +2,7 @@ from pydantic import BaseModel
|
|||||||
|
|
||||||
# Таблица для получения информации из запроса
|
# Таблица для получения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
class Auth(BaseModel):
|
class Auth(BaseModel):
|
||||||
login: str
|
login: str
|
||||||
password: str
|
password: str
|
||||||
|
@ -14,6 +14,7 @@ class Status(Enum):
|
|||||||
DISABLED = "Disabled"
|
DISABLED = "Disabled"
|
||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class ListEvent(BaseModel):
|
class ListEvent(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
name: str = Field(..., max_length=40)
|
name: str = Field(..., max_length=40)
|
||||||
|
@ -3,12 +3,14 @@ from typing import Dict, Any
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = "Active"
|
ACTIVE = "Active"
|
||||||
STOPPING = "Stopping"
|
STOPPING = "Stopping"
|
||||||
STOPPED = "Stopped"
|
STOPPED = "Stopped"
|
||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class MyModel(BaseModel):
|
class MyModel(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
link_name: str = Field(..., max_length=20)
|
link_name: str = Field(..., max_length=20)
|
||||||
|
@ -3,12 +3,14 @@ from typing import Dict, Any
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = "Active"
|
ACTIVE = "Active"
|
||||||
STOPPING = "Stopping"
|
STOPPING = "Stopping"
|
||||||
STOPPED = "Stopped"
|
STOPPED = "Stopped"
|
||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class ProcessSchema(BaseModel):
|
class ProcessSchema(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
title: str = Field(..., max_length=100)
|
title: str = Field(..., max_length=100)
|
||||||
|
@ -2,6 +2,7 @@ from pydantic import BaseModel, Field
|
|||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class ProcessStatusSchema(BaseModel):
|
class ProcessStatusSchema(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
version: int
|
version: int
|
||||||
|
@ -5,14 +5,15 @@ from enum import Enum
|
|||||||
|
|
||||||
|
|
||||||
class NodeType(Enum):
|
class NodeType(Enum):
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = "Active"
|
ACTIVE = "Active"
|
||||||
DISABLED = "Disabled"
|
DISABLED = "Disabled"
|
||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class Ps_Node(BaseModel):
|
class Ps_Node(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
ps_id: int
|
ps_id: int
|
||||||
|
@ -1,21 +1,17 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||||
from api.db.logic.auth import get_user
|
from api.db.logic.auth import get_user
|
||||||
|
|
||||||
# # from backend.schemas.users.token import TokenData
|
# # from backend.schemas.users.token import TokenData
|
||||||
from api.schemas.account.account import User,Status
|
from api.schemas.account.account import User, Status
|
||||||
from api.utils.hasher import Hasher
|
from api.utils.hasher import Hasher
|
||||||
|
|
||||||
|
|
||||||
|
async def authenticate_user(connection: AsyncConnection, username: str, password: str) -> Optional[User]:
|
||||||
|
sql_user, sql_password = await get_user(connection, username)
|
||||||
|
|
||||||
|
if not sql_user or sql_user.status != Status.ACTIVE:
|
||||||
async def authenticate_user(
|
return None
|
||||||
connection: AsyncConnection, username: str, password: str
|
|
||||||
) -> Optional[User]:
|
|
||||||
|
|
||||||
sql_user,sql_password = await get_user(connection, username)
|
|
||||||
|
|
||||||
if not sql_user or sql_user.status != Status.ACTIVE :
|
|
||||||
return None
|
|
||||||
hasher = Hasher()
|
hasher = Hasher()
|
||||||
if not hasher.verify_data(password, sql_password.key_value):
|
if not hasher.verify_data(password, sql_password.key_value):
|
||||||
return None
|
return None
|
||||||
|
@ -14,52 +14,43 @@ from re import escape
|
|||||||
from fastapi_jwt_auth import AuthJWT
|
from fastapi_jwt_auth import AuthJWT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MiddlewareAccessTokenValidadtion(BaseHTTPMiddleware):
|
class MiddlewareAccessTokenValidadtion(BaseHTTPMiddleware):
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
super().__init__(app)
|
super().__init__(app)
|
||||||
|
|
||||||
self.prefix = escape(get_settings().PATH_PREFIX)
|
self.prefix = escape(get_settings().PATH_PREFIX)
|
||||||
self.excluded_routes = [
|
self.excluded_routes = [
|
||||||
re.compile(r'^' + re.escape(self.prefix) + r'/auth/refresh/?$'),
|
re.compile(r"^" + re.escape(self.prefix) + r"/auth/refresh/?$"),
|
||||||
re.compile(r'^' + re.escape(self.prefix) + r'/auth/?$')
|
re.compile(r"^" + re.escape(self.prefix) + r"/auth/?$"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
async def dispatch(self, request: Request, call_next):
|
||||||
|
|
||||||
async def dispatch(self,
|
|
||||||
request: Request,
|
|
||||||
call_next):
|
|
||||||
|
|
||||||
if request.method in ["GET", "POST", "PUT", "DELETE"]:
|
if request.method in ["GET", "POST", "PUT", "DELETE"]:
|
||||||
if any(pattern.match(request.url.path) for pattern in self.excluded_routes):
|
if any(pattern.match(request.url.path) for pattern in self.excluded_routes):
|
||||||
return await call_next(request)
|
return await call_next(request)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
auth_header = request.headers.get("Authorization")
|
auth_header = request.headers.get("Authorization")
|
||||||
if not auth_header:
|
if not auth_header:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
content={"detail": "Missing authorization header."},
|
content={"detail": "Missing authorization header."},
|
||||||
headers={"WWW-Authenticate": "Bearer"}
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
|
|
||||||
token = auth_header.split(" ")[1]
|
token = auth_header.split(" ")[1]
|
||||||
Authorize = AuthJWT(request)
|
Authorize = AuthJWT(request)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
current_user = Authorize.get_jwt_subject()
|
current_user = Authorize.get_jwt_subject()
|
||||||
request.state.current_user = current_user
|
request.state.current_user = current_user
|
||||||
return await call_next(request)
|
return await call_next(request)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
content={"detail": "The access token is invalid or expired."},
|
content={"detail": "The access token is invalid or expired."},
|
||||||
headers={"WWW-Authenticate": "Bearer"}
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# async with get_connection() as connection:
|
# async with get_connection() as connection:
|
||||||
# authorize_user = await get_user_login(connection, current_user)
|
# authorize_user = await get_user_login(connection, current_user)
|
||||||
|
@ -3,6 +3,7 @@ from typing import Optional
|
|||||||
from api.schemas.endpoints.account import UserUpdate, Role, Status
|
from api.schemas.endpoints.account import UserUpdate, Role, Status
|
||||||
from api.schemas.endpoints.account_keyring import AccountKeyringUpdate, StatusKey, TypeKey
|
from api.schemas.endpoints.account_keyring import AccountKeyringUpdate, StatusKey, TypeKey
|
||||||
|
|
||||||
|
|
||||||
def update_user_data_changes(update_data: UserUpdate, user) -> Optional[dict]:
|
def update_user_data_changes(update_data: UserUpdate, user) -> Optional[dict]:
|
||||||
"""
|
"""
|
||||||
Сравнивает данные для обновления с текущими значениями пользователя.
|
Сравнивает данные для обновления с текущими значениями пользователя.
|
||||||
@ -36,6 +37,7 @@ def update_user_data_changes(update_data: UserUpdate, user) -> Optional[dict]:
|
|||||||
|
|
||||||
return changes if changes else None
|
return changes if changes else None
|
||||||
|
|
||||||
|
|
||||||
def update_key_data_changes(update_data: AccountKeyringUpdate, key) -> Optional[dict]:
|
def update_key_data_changes(update_data: AccountKeyringUpdate, key) -> Optional[dict]:
|
||||||
"""
|
"""
|
||||||
Сравнивает данные для обновления с текущими значениями пользователя.
|
Сравнивает данные для обновления с текущими значениями пользователя.
|
||||||
|
@ -3,13 +3,11 @@ from fastapi import (
|
|||||||
status,
|
status,
|
||||||
)
|
)
|
||||||
from api.db.logic.account import get_user_login
|
from api.db.logic.account import get_user_login
|
||||||
from api.schemas.account.account import Role,Status
|
from api.schemas.account.account import Role, Status
|
||||||
|
|
||||||
|
|
||||||
async def db_user_role_validation(connection, current_user):
|
async def db_user_role_validation(connection, current_user):
|
||||||
|
|
||||||
authorize_user = await get_user_login(connection, current_user)
|
authorize_user = await get_user_login(connection, current_user)
|
||||||
if authorize_user.role not in {Role.OWNER, Role.ADMIN}:
|
if authorize_user.role not in {Role.OWNER, Role.ADMIN}:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You do not have enough permissions")
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
|
||||||
detail="You do not have enough permissions")
|
|
||||||
return authorize_user
|
return authorize_user
|
||||||
|
@ -2,6 +2,7 @@ import hashlib
|
|||||||
|
|
||||||
# Хешер для работы с паролем.
|
# Хешер для работы с паролем.
|
||||||
|
|
||||||
|
|
||||||
class Hasher:
|
class Hasher:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
@ -3,6 +3,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
# Генератор key_id для таблицы account_keyring
|
# Генератор key_id для таблицы account_keyring
|
||||||
|
|
||||||
|
|
||||||
def KeyIdGenerator():
|
def KeyIdGenerator():
|
||||||
random_number = random.randint(1000, 9999)
|
random_number = random.randint(1000, 9999)
|
||||||
result = f"{datetime.now().strftime('%Y-%m-%d')}-{random_number}"
|
result = f"{datetime.now().strftime('%Y-%m-%d')}-{random_number}"
|
||||||
|
32
api/poetry.lock
generated
32
api/poetry.lock
generated
@ -1,4 +1,4 @@
|
|||||||
# This file is automatically @generated by Poetry 2.1.1 and should not be changed by hand.
|
# This file is automatically @generated by Poetry 2.1.3 and should not be changed by hand.
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aio-pika"
|
name = "aio-pika"
|
||||||
@ -1052,6 +1052,34 @@ files = [
|
|||||||
{file = "python_multipart-0.0.20.tar.gz", hash = "sha256:8dd0cab45b8e23064ae09147625994d090fa46f5b0d1e13af944c331a7fa9d13"},
|
{file = "python_multipart-0.0.20.tar.gz", hash = "sha256:8dd0cab45b8e23064ae09147625994d090fa46f5b0d1e13af944c331a7fa9d13"},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ruff"
|
||||||
|
version = "0.11.10"
|
||||||
|
description = "An extremely fast Python linter and code formatter, written in Rust."
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.7"
|
||||||
|
groups = ["dev"]
|
||||||
|
files = [
|
||||||
|
{file = "ruff-0.11.10-py3-none-linux_armv6l.whl", hash = "sha256:859a7bfa7bc8888abbea31ef8a2b411714e6a80f0d173c2a82f9041ed6b50f58"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:968220a57e09ea5e4fd48ed1c646419961a0570727c7e069842edd018ee8afed"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-macosx_11_0_arm64.whl", hash = "sha256:1067245bad978e7aa7b22f67113ecc6eb241dca0d9b696144256c3a879663bca"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f4854fd09c7aed5b1590e996a81aeff0c9ff51378b084eb5a0b9cd9518e6cff2"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:8b4564e9f99168c0f9195a0fd5fa5928004b33b377137f978055e40008a082c5"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:5b6a9cc5b62c03cc1fea0044ed8576379dbaf751d5503d718c973d5418483641"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:607ecbb6f03e44c9e0a93aedacb17b4eb4f3563d00e8b474298a201622677947"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7b3a522fa389402cd2137df9ddefe848f727250535c70dafa840badffb56b7a4"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:2f071b0deed7e9245d5820dac235cbdd4ef99d7b12ff04c330a241ad3534319f"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4a60e3a0a617eafba1f2e4186d827759d65348fa53708ca547e384db28406a0b"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:da8ec977eaa4b7bf75470fb575bea2cb41a0e07c7ea9d5a0a97d13dbca697bf2"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:ddf8967e08227d1bd95cc0851ef80d2ad9c7c0c5aab1eba31db49cf0a7b99523"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-musllinux_1_2_i686.whl", hash = "sha256:5a94acf798a82db188f6f36575d80609072b032105d114b0f98661e1679c9125"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:3afead355f1d16d95630df28d4ba17fb2cb9c8dfac8d21ced14984121f639bad"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-win32.whl", hash = "sha256:dc061a98d32a97211af7e7f3fa1d4ca2fcf919fb96c28f39551f35fc55bdbc19"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-win_amd64.whl", hash = "sha256:5cc725fbb4d25b0f185cb42df07ab6b76c4489b4bfb740a175f3a59c70e8a224"},
|
||||||
|
{file = "ruff-0.11.10-py3-none-win_arm64.whl", hash = "sha256:ef69637b35fb8b210743926778d0e45e1bffa850a7c61e428c6b971549b5f5d1"},
|
||||||
|
{file = "ruff-0.11.10.tar.gz", hash = "sha256:d522fb204b4959909ecac47da02830daec102eeb100fb50ea9554818d47a5fa6"},
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sniffio"
|
name = "sniffio"
|
||||||
version = "1.3.1"
|
version = "1.3.1"
|
||||||
@ -1342,4 +1370,4 @@ propcache = ">=0.2.0"
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.1"
|
lock-version = "2.1"
|
||||||
python-versions = ">=3.11,<4.0"
|
python-versions = ">=3.11,<4.0"
|
||||||
content-hash = "146687a6e082e27748cc339242d924d2fb0741f7f2eb842a025e137f5fb41378"
|
content-hash = "22129fa3f5a2cc1190af6c7645f3dd8b4ab0f873b120987cd2e06772863e7dc8"
|
||||||
|
@ -25,3 +25,10 @@ dependencies = [
|
|||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||||
build-backend = "poetry.core.masonry.api"
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
||||||
|
[tool.poetry.group.dev.dependencies]
|
||||||
|
ruff = "^0.11.10"
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
line-length = 120
|
||||||
|
extend-exclude = ["alembic"]
|
||||||
|
Loading…
Reference in New Issue
Block a user