Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,25 @@ private boolean isInvalid(Date actualDate, int timeConstant, String configName,
}

String start = policy.getConfig().get(configName);
if (start != null) {
if (!isBlank(start)) {
String end = policy.getConfig().get(configName + "End");
if (end != null) {
if (dateField < Integer.parseInt(start) || dateField > Integer.parseInt(end)) {
if (!isBlank(end)) {
if (dateField < Integer.parseInt(start.trim()) || dateField > Integer.parseInt(end.trim())) {
return true;
}
} else {
if (dateField != Integer.parseInt(start)) {
if (dateField != Integer.parseInt(start.trim())) {
return true;
}
}
}
return false;
}

private static boolean isBlank(String value) {
return value == null || value.trim().isEmpty();
}

static String format(String notBefore) {
String trimmed = notBefore.trim();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,71 @@ public static void testCheckDateAndTime(KeycloakSession session) {
assertEquals(Effect.DENY, evaluation.getEffect());
}

@Test
public void testCheckDateAndTimeWithBlankRepeatFields() {testingClient.server().run(PolicyEvaluationTest::testCheckDateAndTimeWithBlankRepeatFields);}

public static void testCheckDateAndTimeWithBlankRepeatFields(KeycloakSession session) {
session.getContext().setRealm(session.realms().getRealmByName("authz-test"));
AuthorizationProvider authorization = session.getProvider(AuthorizationProvider.class);
ClientModel clientModel = session.clients().getClientByClientId(session.getContext().getRealm(), "resource-server-test");
StoreFactory storeFactory = authorization.getStoreFactory();
ResourceServer resourceServer = storeFactory.getResourceServerStore().findByClient(clientModel);
Map<String, Collection<String>> attributes = new HashMap<>();
attributes.put("kc.time.date_time", Arrays.asList("2026-10-05 10:30:00"));

// blank repeat fields, as stored by the admin console, are ignored and only nbf/noa are evaluated
TimePolicyRepresentation policyRepresentation = createTimePolicyWithBlankRepeatFields("testCheckDateAndTimeWithBlankRepeatFields");
policyRepresentation.setNotBefore("2026-10-01 00:00:00");
policyRepresentation.setNotOnOrAfter("2026-10-31 23:59:59");
assertTimePolicyEffect(session, authorization, resourceServer, policyRepresentation, attributes, Effect.PERMIT);

// a blank end is treated as unset, so the start must match exactly
policyRepresentation = createTimePolicyWithBlankRepeatFields("testCheckDateAndTimeWithBlankEndMatch");
policyRepresentation.setMonth("10");
assertTimePolicyEffect(session, authorization, resourceServer, policyRepresentation, attributes, Effect.PERMIT);

policyRepresentation = createTimePolicyWithBlankRepeatFields("testCheckDateAndTimeWithBlankEndNoMatch");
policyRepresentation.setMonth("11");
policyRepresentation.setMonthEnd(" ");
assertTimePolicyEffect(session, authorization, resourceServer, policyRepresentation, attributes, Effect.DENY);

// ranges keep working when the other repeat fields are blank
policyRepresentation = createTimePolicyWithBlankRepeatFields("testCheckDateAndTimeWithRangeMatch");
policyRepresentation.setHour("9");
policyRepresentation.setHourEnd("11");
assertTimePolicyEffect(session, authorization, resourceServer, policyRepresentation, attributes, Effect.PERMIT);

policyRepresentation = createTimePolicyWithBlankRepeatFields("testCheckDateAndTimeWithRangeNoMatch");
policyRepresentation.setHour("11");
policyRepresentation.setHourEnd("12");
assertTimePolicyEffect(session, authorization, resourceServer, policyRepresentation, attributes, Effect.DENY);
}

private static TimePolicyRepresentation createTimePolicyWithBlankRepeatFields(String name) {
TimePolicyRepresentation policyRepresentation = new TimePolicyRepresentation();
policyRepresentation.setName(name);
policyRepresentation.setDayMonth("");
policyRepresentation.setDayMonthEnd("");
policyRepresentation.setMonth("");
policyRepresentation.setMonthEnd("");
policyRepresentation.setYear("");
policyRepresentation.setYearEnd("");
policyRepresentation.setHour("");
policyRepresentation.setHourEnd("");
policyRepresentation.setMinute("");
policyRepresentation.setMinuteEnd("");
return policyRepresentation;
}

private static void assertTimePolicyEffect(KeycloakSession session, AuthorizationProvider authorization, ResourceServer resourceServer,
TimePolicyRepresentation policyRepresentation, Map<String, Collection<String>> attributes, Effect expected) {
Policy policy = authorization.getStoreFactory().getPolicyStore().create(resourceServer, policyRepresentation);
PolicyProvider provider = authorization.getProvider(policy.getType());
DefaultEvaluation evaluation = createEvaluation(session, authorization, null, resourceServer, policy, attributes);
provider.evaluate(evaluation);
assertEquals(expected, evaluation.getEffect());
}

@Test
public void testCheckUserInGroup() {
testingClient.server().run(PolicyEvaluationTest::testCheckUserInGroup);
Expand Down
Loading