Fetch user in middleware via auth endpoint with groups

pull/1124/head
kompotkot 2024-08-13 17:10:15 +00:00
rodzic 88a6d47d1d
commit 863f9e41e8
4 zmienionych plików z 13 dodań i 13 usunięć

Wyświetl plik

@ -4,7 +4,7 @@ import logging
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
@ -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):
@ -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(
@ -116,7 +116,7 @@ def brood_auth(token: UUID) -> BugoutUser:
async def request_user_auth(
token: UUID = Depends(oauth2_scheme),
) -> BugoutUser:
) -> BugoutUserWithGroups:
user = brood_auth(token=token)
return user
@ -124,11 +124,11 @@ async def request_user_auth(
async def request_none_or_user_auth(
authorization: str = Header(None),
) -> Optional[BugoutUser]:
) -> Optional[BugoutUserWithGroups]:
"""
Fetch Bugout user if authorization token provided.
"""
user: Optional[BugoutUser] = None
user: Optional[BugoutUserWithGroups] = None
if authorization is not None:
token: str = ""
try:
@ -239,7 +239,7 @@ class BroodAuthMiddleware(BaseHTTPMiddleware):
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(

Wyświetl plik

@ -9,12 +9,12 @@ from sqlalchemy import (
Column,
DateTime,
ForeignKey,
ForeignKeyConstraint,
Index,
Integer,
MetaData,
String,
UniqueConstraint,
ForeignKeyConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.ext.compiler import compiles
@ -178,7 +178,7 @@ class CallRequestType(Base): # type: ignore
class MetatxRequester(Base): # type: ignore
"""
MetatxRequester represents id of user from bugout authorization.
MetatxRequester represents id of resource at Bugout.
"""
__tablename__ = "metatx_requesters"

Wyświetl plik

@ -7,7 +7,7 @@ base58==2.1.1
bitarray==2.7.6
boto3==1.27.0
botocore==1.30.0
bugout==0.2.15
bugout==0.2.17
certifi==2023.5.7
charset-normalizer==3.1.0
click==8.1.3

Wyświetl plik

@ -13,7 +13,7 @@ setup(
packages=find_packages(),
install_requires=[
"boto3",
"bugout>=0.2.15",
"bugout>=0.2.17",
"eip712==0.1.0",
"eth-typing>=2.3.0",
"fastapi",