Skip to content

Commit adb326e

Browse files
committed
Incorporate feedback
1 parent d32eaf0 commit adb326e

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

model/src/test/java/org/cloudfoundry/identity/uaa/zone/LoginConsentTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.cloudfoundry.identity.uaa.zone;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import org.junit.jupiter.api.Disabled;
54
import org.junit.jupiter.api.Test;
65

76
import static org.assertj.core.api.Assertions.assertThat;
87

9-
@Disabled
108
class LoginConsentTest {
119

1210
private final ObjectMapper objectMapper = new ObjectMapper();

server/src/main/java/org/cloudfoundry/identity/uaa/login/LoginConsentHashUtil.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.cloudfoundry.identity.uaa.login;
22

3-
import org.apache.http.impl.auth.UnsupportedDigestAlgorithmException;
43
import org.cloudfoundry.identity.uaa.zone.LoginConsent;
54
import org.springframework.util.StringUtils;
65

76
import java.nio.charset.StandardCharsets;
87
import java.security.MessageDigest;
98
import java.security.NoSuchAlgorithmException;
10-
import java.util.WeakHashMap;
9+
import java.util.concurrent.ConcurrentHashMap;
1110

1211
/**
1312
* Utility class for calculating consent hashes used to track consent versioning.
@@ -16,7 +15,8 @@
1615
*/
1716
public class LoginConsentHashUtil {
1817

19-
private static final WeakHashMap<String, String> consentHashCache = new WeakHashMap<>();
18+
private static final int DEFAULT_SESSION_DURATION = 24 * 60 * 60; // 24 hours in seconds
19+
private static final ConcurrentHashMap<String, String> consentHashCache = new ConcurrentHashMap<>();
2020

2121
private LoginConsentHashUtil() {
2222
// Utility class
@@ -45,7 +45,7 @@ public static String calculateConsentHash(LoginConsent consent) {
4545
byte[] hash = digest.digest(c.getBytes(StandardCharsets.UTF_8));
4646
return bytesToHex(hash);
4747
} catch (NoSuchAlgorithmException e) {
48-
throw new UnsupportedDigestAlgorithmException("SHA-256 algorithm not available", e);
48+
throw new IllegalStateException("SHA-256 algorithm not available", e);
4949
}
5050
});
5151
}
@@ -73,11 +73,12 @@ private static String bytesToHex(byte[] bytes) {
7373
* Supports formats: 0, 15m, 12h, 7d, 1w, 1y
7474
*
7575
* @param duration the duration string (e.g., "12h", "7d", "0")
76-
* @return duration in seconds, or 0 if duration is "0" or invalid
76+
* @return duration in seconds; returns 0 only if duration is "0", otherwise defaults
77+
* to 24 hours (in seconds) for missing or invalid values
7778
*/
7879
public static long parseDurationToSeconds(String duration) {
7980
if (!StringUtils.hasText(duration)) {
80-
return 24 * 60 * 60; // Default: 24 hours
81+
return DEFAULT_SESSION_DURATION;
8182
}
8283

8384
duration = duration.trim();
@@ -91,7 +92,7 @@ public static long parseDurationToSeconds(String duration) {
9192
// Extract number and unit
9293
int length = duration.length();
9394
if (length < 2) {
94-
return 24 * 60 * 60; // Default if invalid
95+
return DEFAULT_SESSION_DURATION; // Default if invalid
9596
}
9697

9798
String numberPart = duration.substring(0, length - 1);
@@ -105,10 +106,10 @@ public static long parseDurationToSeconds(String duration) {
105106
case "d" -> value * 24 * 60 * 60; // days
106107
case "w" -> value * 7 * 24 * 60 * 60; // weeks (7 days)
107108
case "y" -> value * 365 * 24 * 60 * 60; // years (365 days)
108-
default -> 24 * 60 * 60; // Default: 1 day (24 hours)
109+
default -> DEFAULT_SESSION_DURATION;
109110
};
110111
} catch (NumberFormatException e) {
111-
return 24 * 60 * 60; // Default: 24 hours
112+
return DEFAULT_SESSION_DURATION; // Default: if not parsable
112113
}
113114
}
114115
}

server/src/main/java/org/cloudfoundry/identity/uaa/login/LoginInfoEndpoint.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,6 @@ protected Map<String, String> getSelfServiceLinks() {
10041004
}
10051005

10061006
private void addLoginConsentToModel(Model model) {
1007-
log.info("in addLoginConsentToModel()");
10081007
IdentityZone zone = IdentityZoneHolder.get();
10091008
if (zone.getConfig() != null &&
10101009
zone.getConfig().getBranding() != null &&
@@ -1014,7 +1013,6 @@ private void addLoginConsentToModel(Model model) {
10141013

10151014
if (loginConsent.isEnabled()) {
10161015
model.addAttribute("loginConsent", loginConsent);
1017-
log.info("addLoginConsentToModel: " + loginConsent);
10181016

10191017
// Calculate and add the consent hash for client-side verification
10201018
String consentHash = LoginConsentHashUtil.calculateConsentHash(loginConsent);

server/src/test/java/org/cloudfoundry/identity/uaa/login/LoginInfoEndpointTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,8 +1846,8 @@ void testLoginConsentAddedToModelWhenEnabled() throws Exception {
18461846
@Test
18471847
void testLoginConsentNotAddedWhenDisabled() throws Exception {
18481848
// Set up login consent configuration (disabled)
1849-
org.cloudfoundry.identity.uaa.zone.BrandingInformation branding = new org.cloudfoundry.identity.uaa.zone.BrandingInformation();
1850-
org.cloudfoundry.identity.uaa.zone.LoginConsent loginConsent = new org.cloudfoundry.identity.uaa.zone.LoginConsent();
1849+
BrandingInformation branding = new BrandingInformation();
1850+
LoginConsent loginConsent = new LoginConsent();
18511851
loginConsent.setEnabled(false);
18521852
branding.setLoginConsent(loginConsent);
18531853

server/src/test/java/org/cloudfoundry/identity/uaa/zone/LoginConsentValidatorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,13 @@ void testValidateValidHttpsUrl() {
165165

166166
@Test
167167
void testValidateValidHttpUrl() {
168+
//noinspection HttpUrlsUsage
168169
LoginConsent consent = new LoginConsent(
169170
true,
170171
"Notice",
171172
"Text",
172173
"Accept",
173-
"https://example.com",
174+
"http://example.com",
174175
"12h"
175176
);
176177

0 commit comments

Comments
 (0)