Skip to content

Commit

Permalink
Added event for Cognito custom message (#185)
Browse files Browse the repository at this point in the history
* Added custom messgage event

* Added new line

* update testdata json

* review comments resolved
  • Loading branch information
JerzyLa authored and bmoffatt committed May 21, 2019
1 parent 7c08e63 commit c84016c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
21 changes: 21 additions & 0 deletions events/cognito.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,24 @@ type GroupConfiguration struct {
IAMRolesToOverride []string `json:"iamRolesToOverride"`
PreferredRole *string `json:"preferredRole"`
}

// CognitoEventUserPoolsCustomMessage is sent by AWS Cognito User Pools before a verification or MFA message is sent,
// allowing a user to customize the message dynamically.
type CognitoEventUserPoolsCustomMessage struct {
CognitoEventUserPoolsHeader
Request CognitoEventUserPoolsCustomMessageRequest `json:"request"`
Response CognitoEventUserPoolsCustomMessageResponse `json:"response"`
}

// CognitoEventUserPoolsCustomMessageRequest contains the request portion of a CustomMessage event
type CognitoEventUserPoolsCustomMessageRequest struct {
UserAttributes map[string]interface{} `json:"userAttributes"`
CodeParameter string `json:"codeParameter"`
}

// CognitoEventUserPoolsCustomMessageResponse contains the response portion of a CustomMessage event
type CognitoEventUserPoolsCustomMessageResponse struct {
SMSMessage string `json:"smsMessage"`
EmailMessage string `json:"emailMessage"`
EmailSubject string `json:"emailSubject"`
}
27 changes: 27 additions & 0 deletions events/cognito_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,30 @@ func TestCognitoEventUserPoolsPostAuthenticationMarshaling(t *testing.T) {

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

func TestCognitoEventUserPoolsCustomMessageMarshaling(t *testing.T) {

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

// de-serialize into CognitoEvent
var inputEvent CognitoEventUserPoolsCustomMessage
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 TestCognitoUserPoolsCustomMessageMarshalingMalformedJson(t *testing.T) {
test.TestMalformedJson(t, CognitoEventUserPoolsCustomMessage{})
}
23 changes: 23 additions & 0 deletions events/testdata/cognito-event-userpools-custommessage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "1",
"triggerSource": "CustomMessage_SignUp/CustomMessage_ResendCode/CustomMessage_ForgotPassword/CustomMessage_VerifyUserAttribute",
"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
},
"codeParameter": "####"
},
"response": {
"smsMessage": "<custom message to be sent in the message with code parameter>",
"emailMessage": "<custom message to be sent in the message with code parameter>",
"emailSubject": "<custom email subject>"
}
}

0 comments on commit c84016c

Please sign in to comment.