From 0341d025a7c417681ea26f38ce697a7a2fe37188 Mon Sep 17 00:00:00 2001 From: mattrx Date: Mon, 13 May 2019 14:21:03 +0200 Subject: [PATCH] Added cognito PostAuthentication event struct --- events/cognito.go | 18 +++++++++++++++ events/cognito_test.go | 23 +++++++++++++++++++ ...to-event-userpools-postauthentication.json | 19 +++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 events/testdata/cognito-event-userpools-postauthentication.json diff --git a/events/cognito.go b/events/cognito.go index a518abf8..9d93fc2f 100644 --- a/events/cognito.go +++ b/events/cognito.go @@ -44,6 +44,14 @@ type CognitoEventUserPoolsPreTokenGen struct { Response CognitoEventUserPoolsPreTokenGenResponse `json:"response"` } +// CognitoEventUserPoolsPostAuthentication is sent by AWS Cognito User Pools after a user is authenticated, +// allowing the Lambda to add custom logic. +type CognitoEventUserPoolsPostAuthentication struct { + CognitoEventUserPoolsHeader + Request CognitoEventUserPoolsPostAuthenticationRequest `json:"request"` + Response CognitoEventUserPoolsPostAuthenticationResponse `json:"response"` +} + // CognitoEventUserPoolsCallerContext contains information about the caller type CognitoEventUserPoolsCallerContext struct { AWSSDKVersion string `json:"awsSdkVersion"` @@ -93,6 +101,16 @@ type CognitoEventUserPoolsPreTokenGenResponse struct { ClaimsOverrideDetails ClaimsOverrideDetails `json:"claimsOverrideDetails"` } +// CognitoEventUserPoolsPostAuthenticationRequest contains the request portion of a PostAuthentication event +type CognitoEventUserPoolsPostAuthenticationRequest struct { + NewDeviceUsed bool `json:"newDeviceUsed"` + UserAttributes map[string]string `json:"userAttributes"` +} + +// CognitoEventUserPoolsPostAuthenticationResponse contains the response portion of a PostAuthentication event +type CognitoEventUserPoolsPostAuthenticationResponse struct { +} + // ClaimsOverrideDetails allows lambda to add, supress or override claims in the token type ClaimsOverrideDetails struct { GroupOverrideDetails GroupConfiguration `json:"groupOverrideDetails"` diff --git a/events/cognito_test.go b/events/cognito_test.go index a619e47a..d7b805cb 100644 --- a/events/cognito_test.go +++ b/events/cognito_test.go @@ -112,3 +112,26 @@ func TestCognitoEventUserPoolsPreTokenGenMarshaling(t *testing.T) { test.AssertJsonsEqual(t, inputJSON, outputJSON) } + +func TestCognitoEventUserPoolsPostAuthenticationMarshaling(t *testing.T) { + + // read json from file + inputJSON, err := ioutil.ReadFile("./testdata/cognito-event-userpools-postauthentication.json") + if err != nil { + t.Errorf("could not open test file. details: %v", err) + } + + // de-serialize into CognitoEvent + var inputEvent CognitoEventUserPoolsPostAuthentication + 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)) +} diff --git a/events/testdata/cognito-event-userpools-postauthentication.json b/events/testdata/cognito-event-userpools-postauthentication.json new file mode 100644 index 00000000..43ca52ef --- /dev/null +++ b/events/testdata/cognito-event-userpools-postauthentication.json @@ -0,0 +1,19 @@ +{ + "version": "1", + "triggerSource": "PostAuthentication_Authentication", + "region": "", + "userPoolId": "", + "userName": "", + "callerContext": { + "awsSdkVersion": "", + "clientId": "" + }, + "request": { + "newDeviceUsed": true, + "userAttributes" : { + "email": "", + "phone_number": "" + } + }, + "response": {} +} \ No newline at end of file