finish register

This commit is contained in:
2025-12-03 16:27:07 -03:00
parent 967e275f29
commit 392dccebc1
11 changed files with 90 additions and 44 deletions

View File

@@ -44,6 +44,8 @@ def health():
@app.exception_handler(ServiceError)
def exc_error(exc: ServiceError):
logger.exception(exc)
return Response(
body={
'type': type(exc).__name__,

View File

@@ -4,9 +4,11 @@ from typing import TYPE_CHECKING
import boto3
if TYPE_CHECKING:
from mypy_boto3_cognito_idp import CognitoIdentityProviderClient
from mypy_boto3_dynamodb.client import DynamoDBClient
else:
DynamoDBClient = object
CognitoIdentityProviderClient = object
def get_dynamodb_client() -> DynamoDBClient:
@@ -17,3 +19,4 @@ def get_dynamodb_client() -> DynamoDBClient:
dynamodb_client: DynamoDBClient = get_dynamodb_client()
idp_client: CognitoIdentityProviderClient = boto3.client('cognito-idp')

View File

@@ -2,7 +2,6 @@ from http import HTTPStatus
from typing import Annotated
from uuid import uuid4
import boto3
from aws_lambda_powertools.event_handler import (
Response,
)
@@ -17,7 +16,7 @@ from layercake.dateutils import now, ttl
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
from passlib.hash import pbkdf2_sha256
from boto3clients import dynamodb_client
from boto3clients import dynamodb_client, idp_client
from config import (
OAUTH2_TABLE,
SESSION_EXPIRES_IN,
@@ -25,7 +24,6 @@ from config import (
router = Router()
dyn = DynamoDBPersistenceLayer(OAUTH2_TABLE, dynamodb_client)
idp = boto3.client('cognito-idp')
class InvalidCredentialsError(UnauthorizedError): ...
@@ -125,7 +123,7 @@ def _get_idp_user(
).digest()
try:
idp.initiate_auth(
idp_client.initiate_auth(
AuthFlow='USER_PASSWORD_AUTH',
AuthParameters={
'USERNAME': username,
@@ -155,7 +153,9 @@ def new_session(user_id: str) -> str:
exp = ttl(start_dt=now_, seconds=SESSION_EXPIRES_IN)
with dyn.transact_writer() as transact:
transact.delete(key=KeyPair(user_id, 'FAILED_ATTEMPTS'))
transact.delete(
key=KeyPair(user_id, 'FAILED_ATTEMPTS'),
)
transact.update(
key=KeyPair(user_id, '0'),
# Post-migration (users): uncomment the following line

View File

@@ -3,9 +3,11 @@ from http import HTTPStatus
from typing import Annotated
from uuid import uuid4
from aws_lambda_powertools.event_handler import content_types
from aws_lambda_powertools.event_handler.api_gateway import Response, Router
from aws_lambda_powertools.event_handler.exceptions import NotFoundError, ServiceError
from aws_lambda_powertools.event_handler.openapi.params import Body
from aws_lambda_powertools.shared.cookies import Cookie
from layercake.dateutils import now, ttl
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from layercake.extra_types import CpfStr, NameStr
@@ -14,7 +16,9 @@ from passlib.hash import pbkdf2_sha256
from pydantic import UUID4, EmailStr
from boto3clients import dynamodb_client
from config import OAUTH2_TABLE
from config import OAUTH2_TABLE, SESSION_EXPIRES_IN
from .authentication import new_session
router = Router()
dyn = DynamoDBPersistenceLayer(OAUTH2_TABLE, dynamodb_client)
@@ -68,15 +72,36 @@ def register(
)
return Response(
content_type=content_types.APPLICATION_JSON,
status_code=HTTPStatus.OK,
compress=True,
body=asdict(new_user),
cookies=[
_cookie(existing['id']),
],
)
_create_user(user=new_user, password=password)
return Response(
content_type=content_types.APPLICATION_JSON,
status_code=HTTPStatus.CREATED,
compress=True,
body=asdict(new_user),
cookies=[
_cookie(new_user.id),
],
)
def _cookie(user_id: str) -> Cookie:
return Cookie(
name='SID',
value=new_session(user_id),
http_only=True,
secure=True,
same_site=None,
max_age=SESSION_EXPIRES_IN,
)