finish register
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user