Skip to content

Commit 8f7ecb4

Browse files
committed
#60: use a random UUID directly for request ids
1 parent 64dba94 commit 8f7ecb4

2 files changed

Lines changed: 3 additions & 69 deletions

File tree

core/src/main/java/com/onelogin/saml2/util/Util.java

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,46 +1345,9 @@ private static SecretKey generateSymmetricKey() throws Exception {
13451345
* @return A unique string
13461346
*/
13471347
public static String generateUniqueID() {
1348-
try {
1349-
Random r = new Random();
1350-
Integer n = r.nextInt();
1351-
1352-
String id = uniqid(n.toString(), true);
1353-
1354-
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
1355-
crypt.reset();
1356-
crypt.update(id.getBytes());
1357-
final String uniqueIdSha1 = new BigInteger(1, crypt.digest()).toString(16);
1358-
1359-
return UNIQUE_ID_PREFIX + uniqueIdSha1;
1360-
} catch (Exception e) {
1361-
throw new RuntimeException("Error executing generateUniqueID: " + e.getMessage(), e);
1362-
}
1348+
return UNIQUE_ID_PREFIX + UUID.randomUUID();
13631349
}
13641350

1365-
/**
1366-
* Generates random UUID
1367-
*
1368-
* @param prefix
1369-
*
1370-
* @param more_entropy
1371-
*
1372-
* @return the random UUID
1373-
*/
1374-
public static String uniqid(String prefix, Boolean more_entropy) {
1375-
if (prefix != null && StringUtils.isEmpty(prefix)) {
1376-
prefix = StringUtils.EMPTY;
1377-
}
1378-
1379-
if (!more_entropy) {
1380-
return (String) (prefix + UUID.randomUUID().toString()).substring(
1381-
0, 13);
1382-
} else {
1383-
return (String) (prefix + UUID.randomUUID().toString() + UUID
1384-
.randomUUID().toString()).substring(0, 23);
1385-
}
1386-
}
1387-
13881351
/**
13891352
* Interprets a ISO8601 duration value relative to a current time timestamp.
13901353
*

core/src/test/java/com/onelogin/saml2/test/util/UtilsTest.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.hamcrest.CoreMatchers.containsString;
55
import static org.hamcrest.CoreMatchers.not;
66
import static org.hamcrest.CoreMatchers.equalTo;
7+
import static org.hamcrest.CoreMatchers.startsWith;
78
import static org.junit.Assert.assertEquals;
89
import static org.junit.Assert.assertFalse;
910
import static org.junit.Assert.assertNotEquals;
@@ -1893,7 +1894,7 @@ public void testGenerateNameId() throws URISyntaxException, IOException, Certifi
18931894
public void testGenerateUniqueID() {
18941895
String s1 = Util.generateUniqueID();
18951896

1896-
assertThat(s1, containsString(Util.UNIQUE_ID_PREFIX));
1897+
assertThat(s1, startsWith(Util.UNIQUE_ID_PREFIX));
18971898
assertTrue(s1.length() > 40);
18981899

18991900
String s2 = Util.generateUniqueID();
@@ -1903,36 +1904,6 @@ public void testGenerateUniqueID() {
19031904
assertNotEquals(s2, s3);
19041905
}
19051906

1906-
/**
1907-
* Tests the uniqid method
1908-
*
1909-
* @see com.onelogin.saml2.util.Util#uniqid
1910-
*/
1911-
@Test
1912-
public void testUniqid() {
1913-
String id_1 = Util.uniqid(null, false);
1914-
String id_2 = Util.uniqid(null, false);
1915-
assertNotEquals(id_1, id_2);
1916-
1917-
String id_3 = Util.uniqid(null, true);
1918-
String id_4 = Util.uniqid(null, true);
1919-
assertNotEquals(id_3, id_4);
1920-
1921-
assertNotEquals(id_1, id_3);
1922-
assertNotEquals(id_1, id_4);
1923-
assertNotEquals(id_2, id_3);
1924-
assertNotEquals(id_2, id_4);
1925-
1926-
String id_5 = Util.uniqid(Util.UNIQUE_ID_PREFIX, false);
1927-
String id_6 = Util.uniqid(Util.UNIQUE_ID_PREFIX, true);
1928-
assertThat(id_5, containsString(Util.UNIQUE_ID_PREFIX));
1929-
assertThat(id_6, containsString(Util.UNIQUE_ID_PREFIX));
1930-
assertNotEquals(id_5, id_6);
1931-
1932-
String id_7 = Util.uniqid("", false);
1933-
assertNotEquals(id_6, id_7);
1934-
}
1935-
19361907
/**
19371908
* Tests the parseDuration method
19381909
*

0 commit comments

Comments
 (0)