Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat<events.cognito>: add support for custom email sender #419

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions events/cognito.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,24 @@ type CognitoEventUserPoolsCustomMessageResponse struct {
EmailMessage string `json:"emailMessage"`
EmailSubject string `json:"emailSubject"`
}

// CognitoEventUserPoolsCustomEmailSender is sent by AWS Cognito User Pools before each mail to send.
type CognitoEventUserPoolsCustomEmailSender struct {
CognitoEventUserPoolsHeader
Request CognitoEventUserPoolsCustomEmailSenderRequest `json:"request"`
Response CognitoEventUserPoolsCustomEmailSenderResponse `json:"response"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs don't mention a returned response, I think the sender integration isn't quite the same as the Custom Message integration

Email Sender: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-email-sender.html

The custom code of your function must then process and deliver the message.

Compare with Custom Message Trigger: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html#cognito-user-pools-lambda-trigger-syntax-custom-message

In the response, specify the custom text to use in messages to your users.

}

// CognitoEventUserPoolsCustomEmailSenderRequest contains the request portion of a CustomEmailSender event
type CognitoEventUserPoolsCustomEmailSenderRequest struct {
UserAttributes map[string]interface{} `json:"userAttributes"`
Code string `json:"code"`
ClientMetadata map[string]string `json:"clientMetadata"`
Type string `json:"type"`
}

// CognitoEventUserPoolsCustomEmailSenderResponse contains the response portion of a CustomEmailSender event
type CognitoEventUserPoolsCustomEmailSenderResponse struct {
EmailMessage string `json:"emailMessage"`
EmailSubject string `json:"emailSubject"`
}
26 changes: 26 additions & 0 deletions events/cognito_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,29 @@ func TestCognitoEventUserPoolsCustomMessageMarshaling(t *testing.T) {
func TestCognitoUserPoolsCustomMessageMarshalingMalformedJson(t *testing.T) {
test.TestMalformedJson(t, CognitoEventUserPoolsCustomMessage{})
}

func TestCognitoEventUserPoolsCustomEmailSenderMarshaling(t *testing.T) {
// read json from file
inputJSON, err := ioutil.ReadFile("./testdata/cognito-event-userpools-customemailsender.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}

// de-serialize into CognitoEvent
var inputEvent CognitoEventUserPoolsCustomEmailSender
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}

// serialize to json
outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}

assert.JSONEq(t, string(inputJSON), string(outputJSON))
}

func TestCognitoUserPoolsCustomEmailSenderMarshalingMalformedJson(t *testing.T) {
test.TestMalformedJson(t, CognitoEventUserPoolsCustomEmailSender{})
}
26 changes: 26 additions & 0 deletions events/testdata/cognito-event-userpools-customemailsender.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "1",
"triggerSource": "CustomEmailSender_SignUp/CustomEmailSender_ResendCode/CustomEmailSender_ForgotPassword/CustomEmailSender_UpdateUserAttribute/CustomEmailSender_VerifyUserAttribute/CustomEmailSender_AdminCreateUser",
"region": "<region>",
"userPoolId": "<userPoolId>",
"userName": "<userName>",
"callerContext": {
"awsSdkVersion": "<calling aws sdk with version>",
"clientId": "<apps client id>"
},
"request": {
"userAttributes": {
"phone_number_verified": true,
"email_verified": false
},
"code": "example code",
"clientMetadata": {
"exampleMetadataKey": "example metadata value"
},
"type": "example type"
},
"response": {
"emailMessage": "<custom message to be sent in the email>",
"emailSubject": "<custom email subject>"
}
}