Skip to content

Commit 0fe3a11

Browse files
sgramponeBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:gamutils_eo' into beta
1 parent 66b8125 commit 0fe3a11

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

gamutils/src/main/java/com/genexus/gam/GamUtilsEO.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.genexus.gam;
22

33
import com.genexus.gam.utils.Encoding;
4+
import com.genexus.gam.utils.Pkce;
45
import com.genexus.gam.utils.Random;
56
import com.genexus.gam.utils.cryptography.Encryption;
67
import com.genexus.gam.utils.cryptography.Hash;
@@ -95,5 +96,11 @@ public static String base64ToBase64Url(String base64) {
9596

9697
public static String base64ToHexa(String base64) { return Encoding.base64ToHexa(base64); }
9798

99+
//**PKCE**//
100+
101+
public static String pkce_create(int len, String option) { return Pkce.create(len, option); }
102+
103+
public static boolean pkce_verify(String code_verifier, String code_challenge, String option) { return Pkce.verify(code_verifier, code_challenge, option); }
104+
98105
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/
99106
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.genexus.gam.utils;
2+
3+
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
import org.bouncycastle.crypto.Digest;
7+
import org.bouncycastle.crypto.digests.SHA256Digest;
8+
import org.bouncycastle.util.encoders.UrlBase64;
9+
10+
import java.nio.charset.StandardCharsets;
11+
import java.text.MessageFormat;
12+
13+
@SuppressWarnings("LoggingSimilarMessage")
14+
public class Pkce {
15+
16+
private static final Logger logger = LogManager.getLogger(Pkce.class);
17+
18+
public static String create(int len, String option) {
19+
logger.trace("create");
20+
String code_verifier = Random.alphanumeric(len);
21+
switch (option.toUpperCase().trim()) {
22+
case "S256":
23+
byte[] digest = hash(new SHA256Digest(), code_verifier.getBytes(StandardCharsets.UTF_8));
24+
return MessageFormat.format("{0},{1}", code_verifier.trim(), new String(UrlBase64.encode(digest)));
25+
case "PLAIN":
26+
return MessageFormat.format("{0},{1}", code_verifier.trim(), Encoding.toBase64Url(code_verifier.trim()));
27+
default:
28+
logger.error("Unknown PKCE option");
29+
return "";
30+
}
31+
}
32+
33+
public static boolean verify(String code_verifier, String code_challenge, String option) {
34+
logger.trace("verify");
35+
switch (option.toUpperCase().trim()) {
36+
case "S256":
37+
byte[] digest = hash(new SHA256Digest(), code_verifier.trim().getBytes(StandardCharsets.UTF_8));
38+
return (new String(UrlBase64.encode(digest))).equals(code_challenge.trim());
39+
case "PLAIN":
40+
byte[] bytes_plain = UrlBase64.decode(code_challenge.trim().getBytes(StandardCharsets.UTF_8));
41+
return new String(bytes_plain).equals(code_verifier.trim());
42+
default:
43+
logger.error("Unknown PKCE option");
44+
return false;
45+
}
46+
}
47+
48+
private static byte[] hash(Digest digest, byte[] inputBytes) {
49+
byte[] retValue = new byte[digest.getDigestSize()];
50+
digest.update(inputBytes, 0, inputBytes.length);
51+
digest.doFinal(retValue, 0);
52+
return retValue;
53+
}
54+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.genexus.gam.utils.test;
2+
3+
import com.genexus.gam.GamUtilsEO;
4+
import com.genexus.gam.utils.Pkce;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.text.MessageFormat;
9+
10+
public class PkceTest {
11+
12+
@Test
13+
public void testPkceS256()
14+
{
15+
int i = 0;
16+
while (i<50)
17+
{
18+
String[] s256_true = GamUtilsEO.pkce_create(20, "S256").split(",");
19+
Assert.assertTrue("testPkceS256 true", GamUtilsEO.pkce_verify(s256_true[0], s256_true[1], "S256"));
20+
21+
String[] s256_false = GamUtilsEO.pkce_create(20, "S256").split(",");
22+
Assert.assertFalse("testPkceS256 false", GamUtilsEO.pkce_verify(MessageFormat.format("{0}tralala",s256_false[0]), s256_false[1], "S256"));
23+
i++;
24+
}
25+
}
26+
27+
@Test
28+
public void testPkcePlain()
29+
{
30+
int i = 0;
31+
while (i<50)
32+
{
33+
String[] plain_true = GamUtilsEO.pkce_create(20, "PLAIN").split(",");
34+
Assert.assertTrue("testPkceS256", GamUtilsEO.pkce_verify(plain_true[0], plain_true[1], "PLAIN"));
35+
36+
String[] plain_false = GamUtilsEO.pkce_create(20, "PLAIN").split(",");
37+
Assert.assertFalse("testPkceS256 false", GamUtilsEO.pkce_verify(MessageFormat.format("{0}tralala",plain_false[0]), plain_false[1], "PLAIN"));
38+
i++;
39+
}
40+
}
41+
42+
}

0 commit comments

Comments
 (0)