fix(exchange): fix exchange refusing to exchange

This commit is contained in:
Alexandre Teles 2023-01-05 22:01:48 -03:00
parent 7c46b4589e
commit 47b1e588eb
4 changed files with 19 additions and 37 deletions

View File

@ -30,7 +30,7 @@ class Ballot:
await self.redis.json().set( await self.redis.json().set(
name=discord_hashed_id, name=discord_hashed_id,
path=".", path=".",
obj=ballot, obj=ballot.dict(),
nx=True nx=True
) )
await self.BallotLogger.log("STORE_BALLOT", None, discord_hashed_id) await self.BallotLogger.log("STORE_BALLOT", None, discord_hashed_id)

View File

@ -64,25 +64,3 @@ class Clients:
return banned return banned
async def voted(self, token: str, discord_id: str) -> bool:
"""Check if the user already voted
Args:
token (str): Token to check
Returns:
bool: True if the user voted, False otherwise
"""
voted: bool = False
try:
if (await self.is_token_banned(token) or
await self.ballot.exists(discord_id)):
voted = True
except aioredis.RedisError as e:
await self.UserLogger.log("AUTH_CHECKS", e)
raise e
return voted

View File

@ -8,6 +8,7 @@ from app.controllers.Ballot import Ballot
import app.models.ClientModels as ClientModels import app.models.ClientModels as ClientModels
import app.models.GeneralErrors as GeneralErrors import app.models.GeneralErrors as GeneralErrors
import app.models.ResponseModels as ResponseModels import app.models.ResponseModels as ResponseModels
from loguru import logger
router = APIRouter( router = APIRouter(
prefix="/auth", prefix="/auth",
@ -59,7 +60,7 @@ async def auth(request: Request, response: Response, client: ClientModels.Client
} }
) )
@router.put("/exchange", response_model=ResponseModels.ClientAuthTokenResponse, status_code=status.HTTP_200_OK) @router.post("/exchange", response_model=ResponseModels.ClientAuthTokenResponse, status_code=status.HTTP_200_OK)
async def exchange_token(request: Request, response: Response, Authorize: AuthPASETO = Depends(), Authorization: str = Header(None)) -> dict: async def exchange_token(request: Request, response: Response, Authorize: AuthPASETO = Depends(), Authorization: str = Header(None)) -> dict:
"""Exchange a token for a new one. """Exchange a token for a new one.
@ -70,13 +71,18 @@ async def exchange_token(request: Request, response: Response, Authorize: AuthPA
Authorize.paseto_required() Authorize.paseto_required()
user_claims: dict[str, str | bool] = {} user_claims: dict[str, str | bool] = {}
user_claims['discord_id_hash'] = Authorize.get_user_claims()['discord_id_hash']
user_claims['is_exchange_token'] = True user_id = Authorize.get_token_payload()['discord_id_hash']
access_token = Authorize.create_access_token(subject=Authorize.get_subject(),
user_claims=user_claims, if not await ballot.exists(user_id):
fresh=True)
if not await ballot.exists(Authorize.get_subject()):
if await clients.ban_token(Authorize.get_jti()): if await clients.ban_token(Authorize.get_jti()):
user_claims['discord_id_hash'] = user_id
user_claims['is_exchange_token'] = True
access_token = Authorize.create_access_token(
subject=Authorize.get_subject(),
user_claims=user_claims
)
return {"access_token": access_token} return {"access_token": access_token}
else: else:
raise HTTPException(status_code=500, detail={ raise HTTPException(status_code=500, detail={

View File

@ -28,16 +28,14 @@ async def cast_ballot(request: Request, response: Response,
Authorize.paseto_required() Authorize.paseto_required()
if (Authorize.get_paseto_claims()['is_exchange_token'] and if (Authorize.get_token_payload()['is_exchange_token'] and
not client.voted( not await ballot_controller.exists(
Authorize.get_jti(), Authorize.get_token_payload()['discord_id_hash']
Authorize.get_paseto_claims()['discord_hashed_id']
)): )):
stored: bool = await ballot_controller.store( stored: bool = await ballot_controller.store(
Authorize.get_paseto_claims()['discord_hashed_id'], Authorize.get_token_payload()['discord_id_hash'],
ballot ballot)
)
if stored: if stored:
await client.ban_token(Authorize.get_jti()) await client.ban_token(Authorize.get_jti())