Skip to content

Commit ec56cb7

Browse files
marknevesbmoffatt
authored andcommitted
Add event types for Cognito PreAuthentication Lambda trigger (#214)
* Add Cognito trigger: PreAuthentication * Add PreAuthentication test event
1 parent a5ae086 commit ec56cb7

4 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Sample Function
2+
3+
The following is a sample Lambda function that receives Amazon Cognito User Pools pre-authentication event as an input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)
4+
5+
Please see instructions for setting up the Cognito triggers at https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html .
6+
7+
```go
8+
package main
9+
10+
import (
11+
"fmt"
12+
13+
"github.com/aws/aws-lambda-go/lambda"
14+
"github.com/aws/aws-lambda-go/events"
15+
)
16+
17+
func handler(event events.CognitoEventUserPoolsPreAuthentication) (events.CognitoEventUserPoolsPreAuthentication, error) {
18+
fmt.Printf("PreAuthentication of user: %s\n", event.UserName)
19+
return event, nil
20+
}
21+
22+
func main() {
23+
lambda.Start(handler)
24+
}
25+
```

events/cognito.go

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ type CognitoEventUserPoolsPreSignup struct {
2828
Response CognitoEventUserPoolsPreSignupResponse `json:"response"`
2929
}
3030

31+
// CognitoEventUserPoolsPreAuthentication is sent by AWS Cognito User Pools when a user submits their information
32+
// to be authenticated, allowing you to perform custom validations to accept or deny the sign in request.
33+
type CognitoEventUserPoolsPreAuthentication struct {
34+
CognitoEventUserPoolsHeader
35+
Request CognitoEventUserPoolsPreAuthenticationRequest `json:"request"`
36+
Response CognitoEventUserPoolsPreAuthenticationResponse `json:"response"`
37+
}
38+
3139
// CognitoEventUserPoolsPostConfirmation is sent by AWS Cognito User Pools after a user is confirmed,
3240
// allowing the Lambda to send custom messages or add custom logic.
3341
type CognitoEventUserPoolsPostConfirmation struct {
@@ -89,6 +97,16 @@ type CognitoEventUserPoolsPreSignupResponse struct {
8997
AutoVerifyPhone bool `json:"autoVerifyPhone"`
9098
}
9199

100+
// CognitoEventUserPoolsPreAuthenticationRequest contains the request portion of a PreAuthentication event
101+
type CognitoEventUserPoolsPreAuthenticationRequest struct {
102+
UserAttributes map[string]string `json:"userAttributes"`
103+
ValidationData map[string]string `json:"validationData"`
104+
}
105+
106+
// CognitoEventUserPoolsPreAuthenticationResponse contains the response portion of a PreAuthentication event
107+
type CognitoEventUserPoolsPreAuthenticationResponse struct {
108+
}
109+
92110
// CognitoEventUserPoolsPostConfirmationRequest contains the request portion of a PostConfirmation event
93111
type CognitoEventUserPoolsPostConfirmationRequest struct {
94112
UserAttributes map[string]string `json:"userAttributes"`

events/cognito_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ func TestCognitoUserPoolsPreSignupMarshalingMalformedJson(t *testing.T) {
6464
test.TestMalformedJson(t, CognitoEventUserPoolsPreSignup{})
6565
}
6666

67+
func TestCognitoEventUserPoolsPreAuthenticationMarshaling(t *testing.T) {
68+
69+
// read json from file
70+
inputJSON, err := ioutil.ReadFile("./testdata/cognito-event-userpools-preauthentication.json")
71+
if err != nil {
72+
t.Errorf("could not open test file. details: %v", err)
73+
}
74+
75+
// de-serialize into CognitoEvent
76+
var inputEvent CognitoEventUserPoolsPreAuthentication
77+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
78+
t.Errorf("could not unmarshal event. details: %v", err)
79+
}
80+
81+
// serialize to json
82+
outputJSON, err := json.Marshal(inputEvent)
83+
if err != nil {
84+
t.Errorf("could not marshal event. details: %v", err)
85+
}
86+
87+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
88+
}
89+
90+
func TestCognitoUserPoolsPreAuthenticationMarshalingMalformedJson(t *testing.T) {
91+
test.TestMalformedJson(t, CognitoEventUserPoolsPreAuthentication{})
92+
}
93+
6794
func TestCognitoEventUserPoolsPostConfirmationMarshaling(t *testing.T) {
6895

6996
// read json from file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "1",
3+
"triggerSource": "PreAuthentication_Authentication",
4+
"region": "<region>",
5+
"userPoolId": "<userPoolId>",
6+
"userName": "<userName>",
7+
"callerContext": {
8+
"awsSdkVersion": "<calling aws sdk with version>",
9+
"clientId": "<apps client id>"
10+
},
11+
"request": {
12+
"userAttributes": {
13+
"email": "<email>"
14+
},
15+
"validationData": {
16+
"k1": "v1",
17+
"k2": "v2"
18+
}
19+
},
20+
"response": {}
21+
}

0 commit comments

Comments
 (0)