Skip to content

Commit

Permalink
Use random number generator instead of time millis
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Dec 19, 2023
1 parent 5f33f96 commit 6cf9d29
Showing 1 changed file with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.annotation.Resource;
Expand Down Expand Up @@ -42,6 +43,8 @@ public class OrcidRefreshTokenTokenGranterTest extends DBUnitTest {
private static final String CLIENT_ID_2 = "APP-5555555555555556";
private static final String USER_ORCID = "0000-0000-0000-0001";

private Random random = new Random(System.currentTimeMillis());

@Resource
private OrcidOauth2TokenDetailService orcidOauth2TokenDetailService;

Expand Down Expand Up @@ -119,8 +122,8 @@ public void createRefreshTokenTest() {
// should be equal
long time = System.currentTimeMillis();
String scope = "/activities/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = null;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
Expand Down Expand Up @@ -154,8 +157,8 @@ public void createRefreshTokenWithNarrowerScopesTest() {
long time = System.currentTimeMillis();
String parentScope = "/activities/update";
String refreshScope = "/orcid-works/create";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
Expand Down Expand Up @@ -188,8 +191,8 @@ public void createRefreshTokenWithoutRevokeParent() {
// should be enabled, refresh should be enabled
long time = System.currentTimeMillis();
String parentScope = "/activities/update /read-limited";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = false;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
Expand Down Expand Up @@ -228,8 +231,8 @@ public void createRefreshTokenWithoutRevokeParentAndWithNarrowerScopes() {
long time = System.currentTimeMillis();
String parentScope = "/person/read-limited";
String refreshScope = "/orcid-bio/read-limited";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = false;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
Expand Down Expand Up @@ -262,8 +265,8 @@ public void createRefreshTokenWithExpirationOf10Secs() {
long time = System.currentTimeMillis();
String parentScope = "/person/read-limited";
String refreshScope = "/orcid-bio/read-limited";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = false;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = 5L;
Expand Down Expand Up @@ -299,8 +302,8 @@ public void tryToCreateRefreshTokenWithInvalidScopesTest() {
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String refreshScope = "/orcid-works/read-limited";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
Expand All @@ -322,8 +325,8 @@ public void tryToCreateRefreshTokenWithThatExpireAfterParentTokenTest() {
// token, fail
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = time + (15000);
Expand All @@ -345,8 +348,8 @@ public void tryToCreateRefreshTokenWithInvalidClientTest() {
// client # 2, fail
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
Expand All @@ -367,8 +370,8 @@ public void tryToCreateRefreshTokenWithInvalidClientTest() {
public void tryToRefreshAnExpiredTokenTest() {
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time - 10000);
Long expireIn = null;
Expand All @@ -390,8 +393,8 @@ public void tryToCreateRefreshTokenWithInvalidRefreshTokenTest() {
// fail
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
Expand All @@ -415,8 +418,8 @@ public void tryToCreateRefreshTokenWithInvalidParentTokenValueTest() {
// value, fail
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
String tokenValue = "parent-token-" + random.nextLong();
String refreshTokenValue = "refresh-token-" + random.nextLong();
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 15000);
Long expireIn = null;
Expand All @@ -428,7 +431,7 @@ public void tryToCreateRefreshTokenWithInvalidParentTokenValueTest() {
// We shouldn't care about the access token, it's not required and
// shouldn't really be there. If the refresh token and client
// credentials are good, we can generate the refresh token.
assertNotNull(refreshedToken);
assertNotNull(refreshedToken);
}

}

0 comments on commit 6cf9d29

Please sign in to comment.