Skip to content

Commit 66ac89b

Browse files
sgramponegenexusbot
authored andcommitted
Cherry pick branch 'genexuslabs:securityapicommons' into beta
1 parent 8d79019 commit 66ac89b

15 files changed

+1055
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.genexus.test.jwt.features;
2+
3+
import com.genexus.JWT.JWTCreator;
4+
import com.genexus.commons.JWTOptions;
5+
import com.genexus.securityapicommons.keys.SymmetricKeyGenerator;
6+
import com.genexus.test.commons.SecurityAPITestObject;
7+
8+
import junit.framework.Test;
9+
import junit.framework.TestSuite;
10+
11+
public class CreateFromJSONTest extends SecurityAPITestObject{
12+
13+
protected static String payload;
14+
protected static String key;
15+
protected static SymmetricKeyGenerator keyGen;
16+
protected static JWTCreator jwt;
17+
protected static JWTOptions options;
18+
19+
public static Test suite() {
20+
return new TestSuite(CreateFromJSONTest.class);
21+
}
22+
23+
@Override
24+
public void runTest() {
25+
testCreateFromJSON();
26+
}
27+
28+
@Override
29+
public void setUp() {
30+
payload = "{\"sub\":\"subject1\",\"aud\":\"audience1\",\"nbf\":1594116920,\"hola1\":\"hola1\",\"iss\":\"GXSA\",\"hola2\":\"hola2\",\"exp\":1909649720,\"iat\":1596449720,\"jti\":\"0696bb20-6223-4a1c-9ebf-e15c74387b9c, 0696bb20-6223-4a1c-9ebf-e15c74387b9c\"}";
31+
SymmetricKeyGenerator keyGen = new SymmetricKeyGenerator();
32+
key = keyGen.doGenerateKey("GENERICRANDOM", 256);
33+
jwt = new JWTCreator();
34+
options = new JWTOptions();
35+
}
36+
37+
public void testCreateFromJSON() {
38+
options.setSecret(key);
39+
String token = jwt.doCreateFromJSON("HS256", payload, options);
40+
boolean verifies = jwt.doVerifyJustSignature(token, "HS256", options);
41+
True(verifies, jwt);
42+
}
43+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.genexus.test.jwt.features;
2+
3+
import com.genexus.JWT.JWTCreator;
4+
import com.genexus.JWT.claims.PrivateClaims;
5+
import com.genexus.JWT.utils.DateUtil;
6+
import com.genexus.commons.JWTOptions;
7+
import com.genexus.securityapicommons.keys.SymmetricKeyGenerator;
8+
import com.genexus.test.commons.SecurityAPITestObject;
9+
10+
import junit.framework.Test;
11+
import junit.framework.TestSuite;
12+
13+
public class JwtDiverseDataTypesTest extends SecurityAPITestObject{
14+
15+
protected static JWTCreator jwt;
16+
protected static JWTOptions options;
17+
protected static SymmetricKeyGenerator keyGen;
18+
protected static DateUtil du;
19+
protected PrivateClaims claimslevel1;
20+
protected PrivateClaims claimslevel2;
21+
protected PrivateClaims claimslevel3;
22+
protected static String token;
23+
protected static String currentDate;
24+
protected static String hexaKey;
25+
26+
public static Test suite() {
27+
return new TestSuite(JwtDiverseDataTypesTest.class);
28+
}
29+
30+
@Override
31+
public void runTest() {
32+
testPositive();
33+
}
34+
35+
@Override
36+
public void setUp() {
37+
jwt = new JWTCreator();
38+
options = new JWTOptions();
39+
du = new DateUtil();
40+
keyGen = new SymmetricKeyGenerator();
41+
claimslevel1 = new PrivateClaims();
42+
claimslevel2 = new PrivateClaims();
43+
claimslevel3 = new PrivateClaims();
44+
45+
currentDate = du.getCurrentDate();
46+
hexaKey = keyGen.doGenerateKey("GENERICRANDOM", 256);
47+
48+
options.addRegisteredClaim("aud", "jitsi");
49+
options.addRegisteredClaim("iss", "my_client");
50+
options.addRegisteredClaim("sub", "meet.jit.si");
51+
options.addCustomTimeValidationClaim("exp", currentDate, "20");
52+
53+
claimslevel1.setClaim("room", "*");
54+
claimslevel1.setNumericClaim("uno", 1);
55+
claimslevel1.setBooleanClaim("boolean", true);
56+
//1607626804
57+
claimslevel1.setDateClaim("date", 1607626804);
58+
59+
60+
claimslevel1.setClaim("context", claimslevel2);
61+
62+
claimslevel2.setClaim("user", claimslevel3);
63+
claimslevel3.setClaim("avatar", "https:/gravatar.com/avatar/abc123");
64+
claimslevel3.setClaim("name", "John Doe");
65+
claimslevel3.setClaim("email", "[email protected]");
66+
claimslevel3.setClaim("id", "abcd:a1b2c3-d4e5f6-0abc1-23de-abcdef01fedcba");
67+
claimslevel2.setClaim("group", "a123-123-456-789");
68+
69+
options.setSecret(hexaKey);
70+
token = jwt.doCreate("HS256", claimslevel1, options);
71+
}
72+
73+
public void testPositive()
74+
{
75+
boolean verification = jwt.doVerify(token, "HS256", claimslevel1, options);
76+
True(verification, jwt);
77+
}
78+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.genexus.test.jwt.features;
2+
3+
import com.genexus.JWT.JWTCreator;
4+
import com.genexus.JWT.claims.PrivateClaims;
5+
import com.genexus.JWT.utils.DateUtil;
6+
import com.genexus.commons.JWTOptions;
7+
import com.genexus.securityapicommons.keys.SymmetricKeyGenerator;
8+
import com.genexus.test.commons.SecurityAPITestObject;
9+
10+
import junit.framework.Test;
11+
import junit.framework.TestSuite;
12+
13+
public class JwtHeaderParametersTest extends SecurityAPITestObject {
14+
15+
protected static JWTCreator jwt;
16+
protected static JWTOptions options;
17+
protected static SymmetricKeyGenerator keyGen;
18+
protected static DateUtil du;
19+
protected PrivateClaims claims;
20+
protected static String token;
21+
protected static String currentDate;
22+
protected static String hexaKey;
23+
24+
public static Test suite() {
25+
return new TestSuite(JwtHeaderParametersTest.class);
26+
}
27+
28+
@Override
29+
public void runTest() {
30+
testPositive();
31+
testNegative1();
32+
testNegative2();
33+
testNegative3();
34+
}
35+
36+
@Override
37+
public void setUp() {
38+
jwt = new JWTCreator();
39+
options = new JWTOptions();
40+
du = new DateUtil();
41+
keyGen = new SymmetricKeyGenerator();
42+
claims = new PrivateClaims();
43+
44+
45+
currentDate = du.getCurrentDate();
46+
hexaKey = keyGen.doGenerateKey("GENERICRANDOM", 256);
47+
48+
options.addRegisteredClaim("aud", "jitsi");
49+
options.addRegisteredClaim("iss", "my_client");
50+
options.addRegisteredClaim("sub", "meet.jit.si");
51+
options.addCustomTimeValidationClaim("exp", currentDate, "20");
52+
53+
claims.setClaim("hola", "hola");
54+
55+
options.addHeaderParameter("cty", "twilio-fpa;v=1");
56+
options.setSecret(hexaKey);
57+
58+
token = jwt.doCreate("HS256", claims, options);
59+
}
60+
61+
public void testPositive()
62+
{
63+
boolean verification = jwt.doVerify(token, "HS256", claims, options);
64+
True(verification, jwt);
65+
}
66+
67+
public void testNegative1()
68+
{
69+
options.addHeaderParameter("pepe", "whatever");
70+
boolean verification = jwt.doVerify(token, "HS256", claims, options);
71+
assertFalse(verification);
72+
assertFalse(jwt.hasError());
73+
}
74+
75+
public void testNegative2()
76+
{
77+
JWTOptions op = new JWTOptions();
78+
op.addRegisteredClaim("aud", "jitsi");
79+
op.addRegisteredClaim("iss", "my_client");
80+
op.addRegisteredClaim("sub", "meet.jit.si");
81+
op.addCustomTimeValidationClaim("exp", currentDate, "20");
82+
op.setSecret(hexaKey);
83+
op.addHeaderParameter("pepe", "whatever");
84+
85+
boolean verification = jwt.doVerify(token, "HS256", claims, op);
86+
assertFalse(verification);
87+
assertFalse(jwt.hasError());
88+
89+
}
90+
91+
public void testNegative3()
92+
{
93+
JWTOptions op = new JWTOptions();
94+
op.addRegisteredClaim("aud", "jitsi");
95+
op.addRegisteredClaim("iss", "my_client");
96+
op.addRegisteredClaim("sub", "meet.jit.si");
97+
op.addCustomTimeValidationClaim("exp", currentDate, "20");
98+
op.setSecret(hexaKey);
99+
100+
101+
boolean verification = jwt.doVerify(token, "HS256", claims, op);
102+
assertFalse(verification);
103+
assertFalse(jwt.hasError());
104+
}
105+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.genexus.test.jwt.features;
2+
3+
import com.genexus.JWT.JWTCreator;
4+
import com.genexus.JWT.claims.PrivateClaims;
5+
import com.genexus.JWT.utils.DateUtil;
6+
import com.genexus.commons.JWTOptions;
7+
import com.genexus.securityapicommons.keys.SymmetricKeyGenerator;
8+
import com.genexus.test.commons.SecurityAPITestObject;
9+
10+
import junit.framework.Test;
11+
import junit.framework.TestSuite;
12+
13+
public class JwtNestedClaimsTest extends SecurityAPITestObject {
14+
15+
protected static JWTCreator jwt;
16+
protected static JWTOptions options;
17+
protected static SymmetricKeyGenerator keyGen;
18+
protected static DateUtil du;
19+
protected PrivateClaims claimslevel1;
20+
protected PrivateClaims claimslevel2;
21+
protected PrivateClaims claimslevel3;
22+
protected static String token;
23+
protected static String currentDate;
24+
protected static String hexaKey;
25+
26+
public static Test suite() {
27+
return new TestSuite(JwtNestedClaimsTest.class);
28+
}
29+
30+
@Override
31+
public void runTest() {
32+
testPositive();
33+
testNegative1();
34+
testNegative2();
35+
}
36+
37+
@Override
38+
public void setUp() {
39+
jwt = new JWTCreator();
40+
options = new JWTOptions();
41+
du = new DateUtil();
42+
keyGen = new SymmetricKeyGenerator();
43+
claimslevel1 = new PrivateClaims();
44+
claimslevel2 = new PrivateClaims();
45+
claimslevel3 = new PrivateClaims();
46+
47+
currentDate = du.getCurrentDate();
48+
hexaKey = keyGen.doGenerateKey("GENERICRANDOM", 256);
49+
50+
options.addRegisteredClaim("aud", "jitsi");
51+
options.addRegisteredClaim("iss", "my_client");
52+
options.addRegisteredClaim("sub", "meet.jit.si");
53+
options.addCustomTimeValidationClaim("exp", currentDate, "20");
54+
55+
claimslevel1.setClaim("room", "*");
56+
57+
claimslevel1.setClaim("context", claimslevel2);
58+
59+
claimslevel2.setClaim("user", claimslevel3);
60+
claimslevel3.setClaim("avatar", "https:/gravatar.com/avatar/abc123");
61+
claimslevel3.setClaim("name", "John Doe");
62+
claimslevel3.setClaim("email", "[email protected]");
63+
claimslevel3.setClaim("id", "abcd:a1b2c3-d4e5f6-0abc1-23de-abcdef01fedcba");
64+
claimslevel2.setClaim("group", "a123-123-456-789");
65+
66+
options.setSecret(hexaKey);
67+
token = jwt.doCreate("HS256", claimslevel1, options);
68+
}
69+
70+
public void testPositive()
71+
{
72+
boolean verification = jwt.doVerify(token, "HS256", claimslevel1, options);
73+
True(verification, jwt);
74+
}
75+
76+
public void testNegative1()
77+
{
78+
claimslevel2.setClaim("pepe", "whatever");
79+
boolean verification = jwt.doVerify(token, "HS256", claimslevel1, options);
80+
assertFalse(verification);
81+
assertFalse(jwt.hasError());
82+
}
83+
84+
public void testNegative2()
85+
{
86+
PrivateClaims claimslevel11 = new PrivateClaims();
87+
PrivateClaims claimslevel21 = new PrivateClaims();
88+
PrivateClaims claimslevel31 = new PrivateClaims();
89+
claimslevel11.setClaim("room", "*");
90+
91+
claimslevel11.setClaim("context", claimslevel21);
92+
93+
claimslevel21.setClaim("user", claimslevel31);
94+
claimslevel31.setClaim("avatar", "https:/gravatar.com/avatar/abc123");
95+
claimslevel31.setClaim("name", "John Doe");
96+
claimslevel31.setClaim("email", "[email protected]");
97+
claimslevel31.setClaim("id", "abcd:a1b2c3-d4e5f6-0abc1-23de-abcdef01fedcba");
98+
99+
boolean verification = jwt.doVerify(token, "HS256", claimslevel11, options);
100+
assertFalse(verification);
101+
}
102+
103+
}

0 commit comments

Comments
 (0)