Skip to content

Commit ebe38d9

Browse files
committed
add support for Cognito Inbound federation Lambda trigger
Signed-off-by: Maxim Rubchinsky <maxim@rubchinsky.com>
1 parent 71624ac commit ebe38d9

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

events/cognito.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,43 @@ type CognitoEventUserPoolsCustomMessageResponse struct {
368368
EmailMessage string `json:"emailMessage"`
369369
EmailSubject string `json:"emailSubject"`
370370
}
371+
372+
// CognitoFederationProviderType is the type of the external identity provider.
373+
type CognitoFederationProviderType string
374+
375+
const (
376+
CognitoFederationProviderTypeOIDC CognitoFederationProviderType = "OIDC"
377+
CognitoFederationProviderTypeSAML CognitoFederationProviderType = "SAML"
378+
CognitoFederationProviderTypeFacebook CognitoFederationProviderType = "Facebook"
379+
CognitoFederationProviderTypeGoogle CognitoFederationProviderType = "Google"
380+
CognitoFederationProviderTypeSignInWithApple CognitoFederationProviderType = "SignInWithApple"
381+
CognitoFederationProviderTypeLoginWithAmazon CognitoFederationProviderType = "LoginWithAmazon"
382+
)
383+
384+
// CognitoEventUserPoolsInboundFederation is sent by Amazon Cognito User Pools when a user signs in
385+
// through a third-party identity provider, allowing a Lambda to inspect and transform federated user attributes.
386+
type CognitoEventUserPoolsInboundFederation struct {
387+
CognitoEventUserPoolsHeader
388+
Request CognitoEventUserPoolsInboundFederationRequest `json:"request"`
389+
Response CognitoEventUserPoolsInboundFederationResponse `json:"response"`
390+
}
391+
392+
// CognitoEventUserPoolsInboundFederationRequest contains the request portion of an InboundFederation event
393+
type CognitoEventUserPoolsInboundFederationRequest struct {
394+
ProviderName string `json:"providerName"`
395+
ProviderType CognitoFederationProviderType `json:"providerType"`
396+
Attributes CognitoEventUserPoolsInboundFederationAttributes `json:"attributes"`
397+
}
398+
399+
// CognitoEventUserPoolsInboundFederationAttributes contains the identity provider attributes
400+
type CognitoEventUserPoolsInboundFederationAttributes struct {
401+
TokenResponse map[string]string `json:"tokenResponse,omitempty"`
402+
IDToken map[string]string `json:"idToken,omitempty"`
403+
UserInfo map[string]string `json:"userInfo,omitempty"`
404+
SAMLResponse map[string]string `json:"samlResponse,omitempty"`
405+
}
406+
407+
// CognitoEventUserPoolsInboundFederationResponse contains the response portion of an InboundFederation event
408+
type CognitoEventUserPoolsInboundFederationResponse struct {
409+
UserAttributesToMap map[string]string `json:"userAttributesToMap"`
410+
}

events/cognito_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,17 @@ func TestCognitoEventUserPoolsCustomMessageMarshaling(t *testing.T) {
284284
func TestCognitoUserPoolsCustomMessageMarshalingMalformedJson(t *testing.T) {
285285
test.TestMalformedJson(t, CognitoEventUserPoolsCustomMessage{})
286286
}
287+
288+
func TestCognitoEventUserPoolsInboundFederationOIDCMarshaling(t *testing.T) {
289+
var inputEvent CognitoEventUserPoolsInboundFederation
290+
test.AssertJsonFile(t, "./testdata/cognito-event-userpools-inbound-federation-oidc.json", &inputEvent)
291+
}
292+
293+
func TestCognitoEventUserPoolsInboundFederationSAMLMarshaling(t *testing.T) {
294+
var inputEvent CognitoEventUserPoolsInboundFederation
295+
test.AssertJsonFile(t, "./testdata/cognito-event-userpools-inbound-federation-saml.json", &inputEvent)
296+
}
297+
298+
func TestCognitoEventUserPoolsInboundFederationMarshalingMalformedJson(t *testing.T) {
299+
test.TestMalformedJson(t, CognitoEventUserPoolsInboundFederation{})
300+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"version": "1",
3+
"triggerSource": "InboundFederation_ExternalProvider",
4+
"region": "us-east-1",
5+
"userPoolId": "us-east-1_EXAMPLE",
6+
"userName": "testuser",
7+
"callerContext": {
8+
"awsSdkVersion": "aws-sdk-unknown-unknown",
9+
"clientId": "1example23456789"
10+
},
11+
"request": {
12+
"providerName": "ExampleOIDCProvider",
13+
"providerType": "OIDC",
14+
"attributes": {
15+
"tokenResponse": {
16+
"access_token": "eyExample",
17+
"token_type": "Bearer",
18+
"expires_in": "3600"
19+
},
20+
"idToken": {
21+
"sub": "user123",
22+
"email": "testuser@example.com",
23+
"email_verified": "true"
24+
},
25+
"userInfo": {
26+
"email": "testuser@example.com",
27+
"given_name": "Test",
28+
"family_name": "User"
29+
}
30+
}
31+
},
32+
"response": {
33+
"userAttributesToMap": {
34+
"email": "testuser@example.com",
35+
"given_name": "Test",
36+
"family_name": "User"
37+
}
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"version": "1",
3+
"triggerSource": "InboundFederation_ExternalProvider",
4+
"region": "us-east-1",
5+
"userPoolId": "us-east-1_EXAMPLE",
6+
"userName": "testuser",
7+
"callerContext": {
8+
"awsSdkVersion": "aws-sdk-unknown-unknown",
9+
"clientId": "1example23456789"
10+
},
11+
"request": {
12+
"providerName": "ExampleSAMLProvider",
13+
"providerType": "SAML",
14+
"attributes": {
15+
"samlResponse": {
16+
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "testuser@example.com",
17+
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname": "Test",
18+
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname": "User"
19+
}
20+
}
21+
},
22+
"response": {
23+
"userAttributesToMap": {
24+
"email": "testuser@example.com",
25+
"given_name": "Test",
26+
"family_name": "User"
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)