-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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)); | ||
} | ||
catch(DateTimeParseException e){ | ||
greyfairer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
return ZonedDateTime.from(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse((String)claimValue)) | ||
.toInstant(); | ||
} | ||
catch(DateTimeParseException e2){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
throw new IllegalArgumentException("Unable to convert claim '" + claim + | ||
"' from string '" + claimValue + "' to Instant."); | ||
} | ||
} | ||
} | ||
throw new IllegalArgumentException("Unable to convert claim '" + claim + | ||
greyfairer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"' of type '" + claimValue.getClass() + "' to Instant."); | ||
|
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
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() { | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 letDateTimeFormatter.ISO_ZONED_DATE_TIME
handle both cases. Can you ensure we have 2 tests for: