Skip to content

Add ClaimAccessor Support for ISO Date formatted timestamps #6189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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 @@ -20,6 +20,9 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -98,8 +101,24 @@ default Instant getClaimAsInstant(String claim) {
if (Instant.class.isAssignableFrom(claimValue.getClass())) {
return (Instant) claimValue;
}
if (String.class.isAssignableFrom(claimValue.getClass())) {
try {
return Instant.from(DateTimeFormatter.ISO_INSTANT.parse((String)claimValue));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second one: DateTimeFormatter.ISO_ZONED_DATE_TIME works for UTC timestamps too (i.e. ending with Z), so I'm not sure if doing the ISO_INSTANT first is necessary, but I'll keep it as per your request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok let's remove DateTimeFormatter.ISO_INSTANT and let DateTimeFormatter.ISO_ZONED_DATE_TIME handle both cases. Can you ensure we have 2 tests for:

  • 2011-12-03T10:15:30Z
  • 2011-12-03T10:15:30+01:00

}
catch(DateTimeParseException e){
try {
return ZonedDateTime.from(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse((String)claimValue))
.toInstant();
}
catch(DateTimeParseException e2){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you ensure the code formatting is consistent with the class for try/catch - see getClaimAsURL

throw new IllegalArgumentException("Unable to convert claim '" + claim +
"' from string '" + claimValue + "' to Instant.");
}
}
}
throw new IllegalArgumentException("Unable to convert claim '" + claim +
"' of type '" + claimValue.getClass() + "' to Instant.");

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ public void getClaimAsInstantWhenDoubleTypeSecondsThenReturnInstant() {
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
}

// gh-6187
@Test
public void getClaimAsInstantWhenISODateStringThenReturnInstant() {
Instant expectedClaimValue = Instant.parse("2011-12-03T09:15:30Z");
String claimName = "isoDate";

this.claims.put(claimName, "2011-12-03T09:15:30Z");
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
this.claims.put(claimName, "2011-12-03T10:15:30+01:00");
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
this.claims.put(claimName, "2011-12-03T10:15:30+01:00[Europe/Paris]");
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
}

// gh-6187
@Test(expected = IllegalArgumentException.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove expected = IllegalArgumentException.class and use assertThat(this.claimAccessor... to be consistent with other tests in this class. Also, assertThat(..) is the preferred pattern for testing.

public void getClaimAsInstantWithoutTimezoneThrowsIAE() {
String claimName = "isoDate";
this.claims.put(claimName, "2011-12-03T09:15:30");
this.claimAccessor.getClaimAsInstant(claimName);
}

// gh-5608
@Test
public void getClaimAsStringWhenValueIsNullThenReturnNull() {
Expand Down