Skip to content

Commit

Permalink
#2489 replace millis sec with sec
Browse files Browse the repository at this point in the history
  • Loading branch information
marevol committed Oct 17, 2020
1 parent 12f29bf commit 7d6a3fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
13 changes: 10 additions & 3 deletions src/main/java/org/codelibs/fess/helper/RoleQueryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,14 @@ protected void addRoleFromCookieMapping(final Set<String> roleNameList, final Co
protected void parseRoleSet(final String value, final boolean encrypted, final Set<String> roleSet) {
String rolesStr = value;
if (encrypted && cipher != null) {
rolesStr = cipher.decryptoText(rolesStr);
try {
rolesStr = cipher.decryptoText(rolesStr);
} catch (final Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to decrypt {}", rolesStr, e);
}
return;
}
}

if (logger.isDebugEnabled()) {
Expand All @@ -245,15 +252,15 @@ protected void parseRoleSet(final String value, final boolean encrypted, final S
final String[] values = rolesStr.split(valueSeparator);
if (maxAge > 0) {
try {
final long time = getCurrentTime() - Long.parseLong(values[0]);
final long time = getCurrentTime() / 1000 - Long.parseLong(values[0]);
if (time > maxAge || time < 0) {
if (logger.isDebugEnabled()) {
logger.debug("role info is expired: {} > {}", time, maxAge);
}
return;
}
} catch (NumberFormatException e) {
logger.warn("Invalid role infor: {}", rolesStr, e);
logger.warn("Invalid role info: {}", rolesStr, e);
return;
}
}
Expand Down
44 changes: 16 additions & 28 deletions src/test/java/org/codelibs/fess/helper/RoleQueryHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected long getCurrentTime() {
assertEquals(0, roleSet.size());

roleQueryHelperImpl.encryptedParameterValue = false;
getMockRequest().setParameter("fess1", System.currentTimeMillis() + "\nrole1,role2,role3");
getMockRequest().setParameter("fess1", System.currentTimeMillis() / 1000 + "\nrole1,role2,role3");
roleSet = buildByParameter(roleQueryHelperImpl, getMockRequest());
assertEquals(3, roleSet.size());
assertTrue(roleSet.contains("role1"));
Expand All @@ -90,20 +90,16 @@ protected long getCurrentTime() {

roleQueryHelperImpl.cipher = cipher;
roleQueryHelperImpl.encryptedParameterValue = true;
getMockRequest().setParameter("fess2", cipher.encryptoText(System.currentTimeMillis() + "\nrole1,role2,role3"));
getMockRequest().setParameter("fess2", cipher.encryptoText(System.currentTimeMillis() / 1000 + "\nrole1,role2,role3"));
roleSet = buildByParameter(roleQueryHelperImpl, getMockRequest());
assertEquals(3, roleSet.size());
assertTrue(roleSet.contains("role1"));
assertTrue(roleSet.contains("role2"));
assertTrue(roleSet.contains("role3"));

getMockRequest().setParameter("fess2", "fail");
try {
roleSet = buildByParameter(roleQueryHelperImpl, getMockRequest());
fail();
} catch (final IllegalBlockSizeRuntimeException e) {
// ok
}
roleSet = buildByParameter(roleQueryHelperImpl, getMockRequest());
assertEquals(0, roleSet.size());

roleQueryHelperImpl.parameterKey = "fess3";

Expand Down Expand Up @@ -143,7 +139,7 @@ protected long getCurrentTime() {
assertEquals(0, roleSet.size());

roleQueryHelperImpl.encryptedHeaderValue = false;
getMockRequest().addHeader("fess1", System.currentTimeMillis() + "\nrole1,role2,role3");
getMockRequest().addHeader("fess1", System.currentTimeMillis() / 1000 + "\nrole1,role2,role3");
roleSet = buildByHeader(roleQueryHelperImpl, getMockRequest());
assertEquals(3, roleSet.size());
assertTrue(roleSet.contains("role1"));
Expand All @@ -154,7 +150,7 @@ protected long getCurrentTime() {

roleQueryHelperImpl.cipher = cipher;
roleQueryHelperImpl.encryptedHeaderValue = true;
getMockRequest().addHeader("fess2", cipher.encryptoText(System.currentTimeMillis() + "\nrole1,role2,role3"));
getMockRequest().addHeader("fess2", cipher.encryptoText(System.currentTimeMillis() / 1000 + "\nrole1,role2,role3"));
roleSet = buildByHeader(roleQueryHelperImpl, getMockRequest());
assertEquals(3, roleSet.size());
assertTrue(roleSet.contains("role1"));
Expand All @@ -163,12 +159,8 @@ protected long getCurrentTime() {

roleQueryHelperImpl.headerKey = "fess2x";
getMockRequest().addHeader("fess2x", "fail");
try {
roleSet = buildByHeader(roleQueryHelperImpl, getMockRequest());
fail();
} catch (final IllegalBlockSizeRuntimeException e) {
// ok
}
roleSet = buildByHeader(roleQueryHelperImpl, getMockRequest());
assertEquals(0, roleSet.size());

roleQueryHelperImpl.headerKey = "fess3";

Expand Down Expand Up @@ -212,7 +204,7 @@ protected long getCurrentTime() {
assertEquals(0, roleSet.size());

roleQueryHelperImpl.encryptedCookieValue = false;
cookie = new Cookie("fess1", System.currentTimeMillis() + "\nrole1,role2,role3");
cookie = new Cookie("fess1", System.currentTimeMillis() / 1000 + "\nrole1,role2,role3");
getMockRequest().addCookie(cookie);
roleSet = buildByCookie(roleQueryHelperImpl, getMockRequest());
assertEquals(3, roleSet.size());
Expand All @@ -224,7 +216,7 @@ protected long getCurrentTime() {

roleQueryHelperImpl.cipher = cipher;
roleQueryHelperImpl.encryptedCookieValue = true;
cookie = new Cookie("fess2", cipher.encryptoText(System.currentTimeMillis() + "\nrole1,role2,role3"));
cookie = new Cookie("fess2", cipher.encryptoText(System.currentTimeMillis() / 1000 + "\nrole1,role2,role3"));
getMockRequest().addCookie(cookie);
roleSet = buildByCookie(roleQueryHelperImpl, getMockRequest());
assertEquals(3, roleSet.size());
Expand All @@ -238,12 +230,8 @@ protected long getCurrentTime() {
roleQueryHelperImpl.encryptedCookieValue = true;
cookie = new Cookie("fess2x", "fail");
getMockRequest().addCookie(cookie);
try {
roleSet = buildByCookie(roleQueryHelperImpl, getMockRequest());
fail();
} catch (final Exception e) {
// ok
}
roleSet = buildByCookie(roleQueryHelperImpl, getMockRequest());
assertEquals(0, roleSet.size());

roleQueryHelperImpl.cookieKey = "fess3";

Expand Down Expand Up @@ -286,13 +274,13 @@ protected long getCurrentTime() {
assertEquals(0, roleSet.size());

encrypted = false;
value = System.currentTimeMillis() + "\nrole1";
value = System.currentTimeMillis() / 1000 + "\nrole1";
roleSet = decodedRoleList(roleQueryHelperImpl, value, encrypted);
assertEquals(1, roleSet.size());
assertTrue(roleSet.contains("role1"));

encrypted = false;
value = System.currentTimeMillis() + "\nrole1,role2";
value = System.currentTimeMillis() / 1000 + "\nrole1,role2";
roleSet = decodedRoleList(roleQueryHelperImpl, value, encrypted);
assertEquals(2, roleSet.size());
assertTrue(roleSet.contains("role1"));
Expand Down Expand Up @@ -355,13 +343,13 @@ protected long getCurrentTime() {
assertEquals(0, roleSet.size());

encrypted = true;
value = cipher.encryptoText(System.currentTimeMillis() + "\nrole1");
value = cipher.encryptoText(System.currentTimeMillis() / 1000 + "\nrole1");
roleSet = decodedRoleList(roleQueryHelperImpl, value, encrypted);
assertEquals(1, roleSet.size());
assertTrue(roleSet.contains("role1"));

encrypted = true;
value = cipher.encryptoText(System.currentTimeMillis() + "\nrole1,role2");
value = cipher.encryptoText(System.currentTimeMillis() / 1000 + "\nrole1,role2");
roleSet = decodedRoleList(roleQueryHelperImpl, value, encrypted);
assertEquals(2, roleSet.size());
assertTrue(roleSet.contains("role1"));
Expand Down

0 comments on commit 7d6a3fa

Please sign in to comment.