Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metatx under resources control #1124

Merged
merged 16 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
390 changes: 342 additions & 48 deletions engineapi/engineapi/contracts_actions.py

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions engineapi/engineapi/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class UpdateContractRequest(BaseModel):
class RegisteredContractResponse(BaseModel):
id: UUID
blockchain: Optional[str] = None
chain_id: Optional[int] = None
address: str
metatx_requester_id: UUID
title: Optional[str] = None
Expand All @@ -267,6 +268,23 @@ class Config:
orm_mode = True


class RegisteredContractHolderResponse(BaseModel):
holder_id: UUID
holder_type: str
permissions: List[str] = Field(default_factory=list)
name: Optional[str] = None


class RegisteredContractWithHoldersResponse(RegisteredContractResponse):
holders: List[RegisteredContractHolderResponse] = Field(default_factory=list)


class MetatxRequestersResponse(BaseModel):
metatx_requester_id: UUID
registered_contracts_count: int
call_requests_count: int


class CallSpecification(BaseModel):
caller: str
method: str
Expand Down
23 changes: 11 additions & 12 deletions engineapi/engineapi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Awaitable, Callable, Dict, List, Optional, Sequence, Set, Tuple
from uuid import UUID

from bugout.data import BugoutResource, BugoutResources, BugoutUser
from bugout.data import BugoutResource, BugoutResources, BugoutUserWithGroups
from bugout.exceptions import BugoutResponseException
from eip712.messages import EIP712Message, _hash_eip191_message
from eth_account.messages import encode_defunct
Expand Down Expand Up @@ -72,11 +72,11 @@ def parse_auth_header(auth_header: str) -> Tuple[str, str]:
return auth_list[0], auth_list[1]


def bugout_auth(token: str) -> BugoutUser:
def bugout_auth(token: str) -> BugoutUserWithGroups:
"""
Extended bugout.get_user with additional checks.
"""
user: BugoutUser = bc.get_user(token)
user: BugoutUserWithGroups = bc.auth(token)
if not user.verified:
raise BugoutUnverifiedAuth("Only verified accounts can have access")
if str(user.application_id) != str(MOONSTREAM_APPLICATION_ID):
Expand All @@ -85,9 +85,9 @@ def bugout_auth(token: str) -> BugoutUser:
return user


def brood_auth(token: UUID) -> BugoutUser:
def brood_auth(token: UUID) -> BugoutUserWithGroups:
try:
user: BugoutUser = bugout_auth(token=token)
user: BugoutUserWithGroups = bugout_auth(token=token)
except BugoutUnverifiedAuth:
logger.info(f"Attempted access by unverified Brood account: {user.id}")
raise EngineHTTPException(
Expand Down Expand Up @@ -116,19 +116,17 @@ def brood_auth(token: UUID) -> BugoutUser:

async def request_user_auth(
token: UUID = Depends(oauth2_scheme),
) -> BugoutUser:
) -> Tuple[BugoutUserWithGroups, UUID]:
user = brood_auth(token=token)

return user
return user, token


async def request_none_or_user_auth(
authorization: str = Header(None),
) -> Optional[BugoutUser]:
) -> Optional[Tuple[BugoutUserWithGroups, UUID]]:
"""
Fetch Bugout user if authorization token provided.
"""
user: Optional[BugoutUser] = None
if authorization is not None:
token: str = ""
try:
Expand All @@ -143,8 +141,9 @@ async def request_none_or_user_auth(

if token != "":
user = brood_auth(token=token)
return user, token

return user
return None


async def metatx_verify_header(
Expand Down Expand Up @@ -239,7 +238,7 @@ async def dispatch(
return Response(status_code=500, content="Internal server error")

try:
user: BugoutUser = bugout_auth(token=user_token)
user: BugoutUserWithGroups = bugout_auth(token=user_token)
except BugoutUnverifiedAuth:
logger.info(f"Attempted access by unverified Brood account: {user.id}")
return Response(
Expand Down
7 changes: 5 additions & 2 deletions engineapi/engineapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
Column,
DateTime,
ForeignKey,
ForeignKeyConstraint,
Index,
Integer,
MetaData,
String,
UniqueConstraint,
ForeignKeyConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.ext.compiler import compiles
Expand Down Expand Up @@ -176,9 +176,12 @@ class CallRequestType(Base): # type: ignore
description = Column(String, nullable=True)


# TODO(kompotkot): Since this migrated under resource control, this table should be dropped, but
# first ForeignKey with CASCADE from registered_contracts and call_requests and replace it
# with simple UUID(as_uuid=True) column.
class MetatxRequester(Base): # type: ignore
"""
MetatxRequester represents id of user from bugout authorization.
MetatxRequester represents id of resource at Bugout.
"""

__tablename__ = "metatx_requesters"
Expand Down
Loading
Loading