add register

This commit is contained in:
2025-12-02 22:54:27 -03:00
parent e5ac436c5a
commit d461a507f9
6 changed files with 279 additions and 44 deletions

View File

@@ -25,6 +25,12 @@ dyn = DynamoDBPersistenceLayer(OAUTH2_TABLE, dynamodb_client)
idp = boto3.client('cognito-idp')
class InvalidCredentialsError(ForbiddenError): ...
class UserNotFoundError(NotFoundError): ...
@router.post('/authentication')
def authentication(
username: Annotated[str, Body()],
@@ -36,7 +42,7 @@ def authentication(
_get_idp_user(user_id, username, password)
else:
if not pbkdf2_sha256.verify(password, password_hash):
raise ForbiddenError('Invalid credentials')
raise InvalidCredentialsError('Invalid credentials')
return Response(
status_code=HTTPStatus.OK,
@@ -61,7 +67,7 @@ def _get_user(username: str) -> tuple[str, str | None]:
)
if not user:
raise UserNotFoundError()
raise UserNotFoundError('User not found')
password = dyn.collection.get_item(
KeyPair(
@@ -121,13 +127,13 @@ def _get_idp_user(
}
)
except Exception:
raise ForbiddenError('Invalid credentials')
raise InvalidCredentialsError('Invalid credentials')
return True
def new_session(sub: str) -> str:
sid = str(uuid4())
def new_session(user_id: str) -> str:
session_id = str(uuid4())
now_ = now()
exp = ttl(start_dt=now_, seconds=SESSION_EXPIRES_IN)
@@ -135,24 +141,19 @@ def new_session(sub: str) -> str:
transact.put(
item={
'id': 'SESSION',
'sk': sid,
'user_id': sub,
'sk': session_id,
'user_id': user_id,
'ttl': exp,
'created_at': now_,
}
)
transact.put(
item={
'id': sub,
'sk': f'SESSION#{sid}',
'id': user_id,
'sk': f'SESSION#{session_id}',
'ttl': exp,
'created_at': now_,
}
)
return f'{sid}:{sub}'
class UserNotFoundError(NotFoundError):
def __init__(self, *_):
super().__init__('User not found')
return f'{session_id}:{user_id}'