Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions aws_lambda_powertools/utilities/parser/models/cognito.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Common context model for Cognito triggers
class CognitoCallerContextModel(BaseModel):
awsSdkVersion: str
clientId: str
clientId: str | None = None


# Base model for all Cognito triggers
Expand All @@ -33,7 +33,7 @@ class CognitoPreSignupResponseModel(BaseModel):


class CognitoPreSignupTriggerModel(CognitoTriggerBaseSchema):
triggerSource: Literal["PreSignUp_SignUp"]
triggerSource: Literal["PreSignUp_SignUp", "PreSignUp_AdminCreateUser", "PreSignUp_ExternalProvider"]
request: CognitoPreSignupRequestModel
response: CognitoPreSignupResponseModel

Expand All @@ -45,7 +45,7 @@ class CognitoPostConfirmationRequestModel(BaseModel):


class CognitoPostConfirmationTriggerModel(CognitoTriggerBaseSchema):
triggerSource: Literal["PostConfirmation_ConfirmSignUp"]
triggerSource: Literal["PostConfirmation_ConfirmSignUp", "PostConfirmation_ConfirmForgotPassword"]
request: CognitoPostConfirmationRequestModel
response: dict[str, Any] = {}

Expand Down Expand Up @@ -157,18 +157,36 @@ class CognitoCustomEmailSMSSenderRequestModel(BaseModel):


class CognitoCustomEmailSenderTriggerModel(CognitoTriggerBaseSchema):
triggerSource: Literal["CustomEmailSender_SignUp"]
triggerSource: Literal[
"CustomEmailSender_SignUp",
"CustomEmailSender_ResendCode",
"CustomEmailSender_ForgotPassword",
"CustomEmailSender_UpdateUserAttribute",
"CustomEmailSender_VerifyUserAttribute",
"CustomEmailSender_AdminCreateUser",
"CustomEmailSender_Authentication",
"CustomEmailSender_AccountTakeOverNotification",
]
request: CognitoCustomEmailSMSSenderRequestModel


class CognitoCustomSMSSenderTriggerModel(CognitoTriggerBaseSchema):
triggerSource: Literal["CustomSMSSender_SignUp"]
triggerSource: Literal[
"CustomSMSSender_SignUp",
"CustomSMSSender_ResendCode",
"CustomSMSSender_ForgotPassword",
"CustomSMSSender_UpdateUserAttribute",
"CustomSMSSender_VerifyUserAttribute",
"CustomSMSSender_AdminCreateUser",
"CustomSMSSender_Authentication",
]
request: CognitoCustomEmailSMSSenderRequestModel


# Models for Challenge Authentication flows
class CognitoChallengeResultModel(BaseModel):
challengeName: Literal[
"CUSTOM_CHALLENGE",
"SRP_A",
"PASSWORD_VERIFIER",
"SMS_MFA",
Expand Down Expand Up @@ -222,7 +240,7 @@ class CognitoVerifyAuthChallengeRequestModel(BaseModel):


class CognitoVerifyAuthChallengeResponseModel(BaseModel):
answerCorrect: bool
answerCorrect: bool | None = None


class CognitoVerifyAuthChallengeTriggerModel(CognitoTriggerBaseSchema):
Expand Down
104 changes: 104 additions & 0 deletions tests/unit/parser/_pydantic/test_cognito_triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,107 @@ def test_cognito_trigger_models_parse_success(filename, model):
def test_cognito_trigger_models_invalid_raises():
with pytest.raises(ValidationError):
parse(event={"foo": "bar"}, model=CognitoPreSignupTriggerModel)


@pytest.mark.parametrize(
"filename,model,trigger_source",
[
("cognitoPreSignUpEvent.json", CognitoPreSignupTriggerModel, "PreSignUp_AdminCreateUser"),
("cognitoPreSignUpEvent.json", CognitoPreSignupTriggerModel, "PreSignUp_ExternalProvider"),
(
"cognitoPostConfirmationEvent.json",
CognitoPostConfirmationTriggerModel,
"PostConfirmation_ConfirmForgotPassword",
),
("cognitoCustomEmailSenderEvent.json", CognitoCustomEmailSenderTriggerModel, "CustomEmailSender_ResendCode"),
(
"cognitoCustomEmailSenderEvent.json",
CognitoCustomEmailSenderTriggerModel,
"CustomEmailSender_ForgotPassword",
),
(
"cognitoCustomEmailSenderEvent.json",
CognitoCustomEmailSenderTriggerModel,
"CustomEmailSender_UpdateUserAttribute",
),
(
"cognitoCustomEmailSenderEvent.json",
CognitoCustomEmailSenderTriggerModel,
"CustomEmailSender_VerifyUserAttribute",
),
(
"cognitoCustomEmailSenderEvent.json",
CognitoCustomEmailSenderTriggerModel,
"CustomEmailSender_AdminCreateUser",
),
(
"cognitoCustomEmailSenderEvent.json",
CognitoCustomEmailSenderTriggerModel,
"CustomEmailSender_Authentication",
),
(
"cognitoCustomEmailSenderEvent.json",
CognitoCustomEmailSenderTriggerModel,
"CustomEmailSender_AccountTakeOverNotification",
),
("cognitoCustomSMSSenderEvent.json", CognitoCustomSMSSenderTriggerModel, "CustomSMSSender_ResendCode"),
("cognitoCustomSMSSenderEvent.json", CognitoCustomSMSSenderTriggerModel, "CustomSMSSender_ForgotPassword"),
(
"cognitoCustomSMSSenderEvent.json",
CognitoCustomSMSSenderTriggerModel,
"CustomSMSSender_UpdateUserAttribute",
),
(
"cognitoCustomSMSSenderEvent.json",
CognitoCustomSMSSenderTriggerModel,
"CustomSMSSender_VerifyUserAttribute",
),
("cognitoCustomSMSSenderEvent.json", CognitoCustomSMSSenderTriggerModel, "CustomSMSSender_AdminCreateUser"),
("cognitoCustomSMSSenderEvent.json", CognitoCustomSMSSenderTriggerModel, "CustomSMSSender_Authentication"),
],
)
def test_cognito_trigger_models_accept_documented_trigger_sources(filename, model, trigger_source):
event = load_event(filename)
event["triggerSource"] = trigger_source

parsed = parse(event=event, model=model)

assert parsed.triggerSource == trigger_source


@pytest.mark.parametrize(
"filename,model",
[
("cognitoDefineAuthChallengeEvent.json", CognitoDefineAuthChallengeTriggerModel),
("cognitoCreateAuthChallengeEvent.json", CognitoCreateAuthChallengeTriggerModel),
],
)
def test_cognito_auth_challenge_models_accept_custom_challenge_in_session(filename, model):
event = load_event(filename)
event["request"]["session"].append(
{"challengeName": "CUSTOM_CHALLENGE", "challengeResult": True, "challengeMetadata": "CAPTCHA_CHALLENGE"},
)

parsed = parse(event=event, model=model)

assert parsed.request.session[-1].challengeName == "CUSTOM_CHALLENGE"


def test_cognito_verify_auth_challenge_model_accepts_null_answer_correct():
# Cognito sends answerCorrect as null, and the function sets it
event = load_event("cognitoVerifyAuthChallengeResponseEvent.json")
event["response"]["answerCorrect"] = None

parsed = parse(event=event, model=CognitoVerifyAuthChallengeTriggerModel)

assert parsed.response.answerCorrect is None


def test_cognito_trigger_models_accept_null_client_id():
# Admin API operations such as AdminConfirmSignUp send clientId as null
event = load_event("cognitoPostConfirmationEvent.json")
event["callerContext"]["clientId"] = None

parsed = parse(event=event, model=CognitoPostConfirmationTriggerModel)

assert parsed.callerContext.clientId is None
Loading