Skip to content

Commit 656417a

Browse files
msailesandclt
authored andcommitted
Adds the V2 version of the pre token generation event.
1 parent 02d2c06 commit 656417a

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
2+
3+
package com.amazonaws.services.lambda.runtime.events;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.EqualsAndHashCode;
9+
import lombok.NoArgsConstructor;
10+
import lombok.ToString;
11+
12+
import java.util.Map;
13+
14+
/**
15+
* Represent the class for the Cognito User Pool Pre Token Generation Lambda Trigger V2
16+
* <p>
17+
* See <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html">Pre Token Generation Lambda Trigger</a>
18+
*/
19+
@Data
20+
@EqualsAndHashCode(callSuper = true)
21+
@NoArgsConstructor
22+
@ToString(callSuper = true)
23+
public class CognitoUserPoolPreTokenGenerationEventV2 extends CognitoUserPoolEvent {
24+
/**
25+
* The request from the Amazon Cognito service.
26+
*/
27+
private Request request;
28+
29+
/**
30+
* The response from your Lambda trigger.
31+
*/
32+
private Response response;
33+
34+
@Builder(setterPrefix = "with")
35+
public CognitoUserPoolPreTokenGenerationEventV2(
36+
String version,
37+
String triggerSource,
38+
String region,
39+
String userPoolId,
40+
String userName,
41+
CallerContext callerContext,
42+
Request request,
43+
Response response) {
44+
super(version, triggerSource, region, userPoolId, userName, callerContext);
45+
this.request = request;
46+
this.response = response;
47+
}
48+
49+
@Data
50+
@EqualsAndHashCode(callSuper = true)
51+
@NoArgsConstructor
52+
@ToString(callSuper = true)
53+
public static class Request extends CognitoUserPoolEvent.Request {
54+
55+
private String[] scopes;
56+
private GroupConfiguration groupConfiguration;
57+
private Map<String, String> clientMetadata;
58+
59+
@Builder(setterPrefix = "with")
60+
public Request(Map<String, String> userAttributes, String[] scopes, GroupConfiguration groupConfiguration, Map<String, String> clientMetadata) {
61+
super(userAttributes);
62+
this.scopes = scopes;
63+
this.groupConfiguration = groupConfiguration;
64+
this.clientMetadata = clientMetadata;
65+
}
66+
}
67+
68+
@Data
69+
@AllArgsConstructor
70+
@Builder(setterPrefix = "with")
71+
@NoArgsConstructor
72+
public static class GroupConfiguration {
73+
/**
74+
* A list of the group names that are associated with the user that the identity token is issued for.
75+
*/
76+
private String[] groupsToOverride;
77+
/**
78+
* A list of the current IAM roles associated with these groups.
79+
*/
80+
private String[] iamRolesToOverride;
81+
/**
82+
* Indicates the preferred IAM role.
83+
*/
84+
private String preferredRole;
85+
}
86+
87+
@Data
88+
@AllArgsConstructor
89+
@Builder(setterPrefix = "with")
90+
@NoArgsConstructor
91+
public static class Response {
92+
private ClaimsAndScopeOverrideDetails claimsAndScopeOverrideDetails;
93+
}
94+
95+
@Data
96+
@AllArgsConstructor
97+
@Builder(setterPrefix = "with")
98+
@NoArgsConstructor
99+
public static class ClaimsAndScopeOverrideDetails {
100+
private IdTokenGeneration idTokenGeneration;
101+
private AccessTokenGeneration accessTokenGeneration;
102+
private GroupOverrideDetails groupOverrideDetails;
103+
}
104+
105+
@Data
106+
@AllArgsConstructor
107+
@Builder(setterPrefix = "with")
108+
@NoArgsConstructor
109+
public static class IdTokenGeneration {
110+
private Map<String, String> claimsToAddOrOverride;
111+
private String[] claimsToSuppress;
112+
}
113+
114+
@Data
115+
@AllArgsConstructor
116+
@Builder(setterPrefix = "with")
117+
@NoArgsConstructor
118+
public static class AccessTokenGeneration {
119+
private Map<String, String> claimsToAddOrOverride;
120+
private String[] claimsToSuppress;
121+
private String[] scopesToAdd;
122+
private String[] scopesToSuppress;
123+
}
124+
125+
@Data
126+
@AllArgsConstructor
127+
@Builder(setterPrefix = "with")
128+
@NoArgsConstructor
129+
public static class GroupOverrideDetails {
130+
private Map<String, String> groupsToOverride;
131+
private Map<String, String> iamRolesToOverride;
132+
private String preferredRole;
133+
}
134+
}

Diff for: aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public static RabbitMQEvent loadRabbitMQEvent(String filename) {
113113
return loadEvent(filename, RabbitMQEvent.class);
114114
}
115115

116+
public static CognitoUserPoolPreTokenGenerationEventV2 loadCognitoUserPoolPreTokenGenerationEventV2(String filename) {
117+
return loadEvent(filename, CognitoUserPoolPreTokenGenerationEventV2.class);
118+
}
119+
116120
public static <T> T loadEvent(String filename, Class<T> targetClass) {
117121

118122
if (!filename.endsWith("json")) {

Diff for: aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import static java.time.Instant.ofEpochSecond;
1616
import static org.assertj.core.api.Assertions.*;
17-
import static org.assertj.core.api.Assertions.from;
1817

1918
import com.amazonaws.services.lambda.runtime.events.*;
2019

@@ -363,4 +362,18 @@ public void testLoadRabbitMQEvent() {
363362
assertThat(header1.get("bytes")).contains(118, 97, 108, 117, 101, 49);
364363
assertThat((Integer) headers.get("numberInHeader")).isEqualTo(10);
365364
}
365+
366+
@Test
367+
public void testLoadCognitoUserPoolPreTokenGenerationEventV2() {
368+
CognitoUserPoolPreTokenGenerationEventV2 event = EventLoader.loadCognitoUserPoolPreTokenGenerationEventV2("cognito_user_pool_pre_token_generation_event_v2.json");
369+
assertThat(event).isNotNull();
370+
assertThat(event)
371+
.returns("2", from(CognitoUserPoolPreTokenGenerationEventV2::getVersion))
372+
.returns("us-east-1", from(CognitoUserPoolPreTokenGenerationEventV2::getRegion))
373+
.returns("TokenGeneration_Authentication", from(CognitoUserPoolPreTokenGenerationEventV2::getTriggerSource));
374+
375+
CognitoUserPoolPreTokenGenerationEventV2.Request request = event.getRequest();
376+
assertThat(request)
377+
.returns("aws.cognito.signin.user.admin", from(CognitoUserPoolPreTokenGenerationEventV2.Request::getScopes));
378+
}
366379
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "2",
3+
"triggerSource": "TokenGeneration_Authentication",
4+
"region": "us-east-1",
5+
"userPoolId": "us-east-1_EXAMPLE",
6+
"userName": "JaneDoe",
7+
"callerContext": {
8+
"awsSdkVersion": "aws-sdk-unknown-unknown",
9+
"clientId": "1example23456789"
10+
},
11+
"request": {
12+
"userAttributes": {
13+
"sub": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
14+
"cognito:user_status": "CONFIRMED",
15+
"email_verified": "true",
16+
"phone_number_verified": "true",
17+
"phone_number": "+12065551212",
18+
"family_name": "Zoe",
19+
"email": "[email protected]"
20+
},
21+
"groupConfiguration": {
22+
"groupsToOverride": ["group-1", "group-2", "group-3"],
23+
"iamRolesToOverride": ["arn:aws:iam::123456789012:role/sns_caller1", "arn:aws:iam::123456789012:role/sns_caller2", "arn:aws:iam::123456789012:role/sns_caller3"],
24+
"preferredRole": ["arn:aws:iam::123456789012:role/sns_caller"]
25+
},
26+
"scopes": [
27+
"aws.cognito.signin.user.admin", "openid", "email", "phone"
28+
]
29+
},
30+
"response": {
31+
"claimsAndScopeOverrideDetails": []
32+
}
33+
}

0 commit comments

Comments
 (0)