Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Updated date time APIs #392

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

Merged
merged 11 commits into from
Mar 5, 2020
Merged

Conversation

jimmyjames
Copy link
Contributor

@jimmyjames jimmyjames commented Feb 14, 2020

Changes

Adds new APIs for DateTime claims using java.time.Instant

Motivations:

  • JDK8 introduced new Date/time classes to address issues with the legacy Date functionality. Over time developers have transitioned to the new APIs.
  • Some of the improvements of Instant over date are immutability (thread-safe), better API design allowing for more natural time-based calculations, and nanosecond precision (as opposed to millisecond precision with java.util.Date).

This change adds the following new public APIs:

Claim.java:

  • Instant asInstant()

Clock.java:

  • Instant getNow()

JWTCreator.java:

  • Builder withNotBefore(Instant notBefore)
  • Builder withIssuedAt(Instant issuedAt)
  • Builder withClaim(String name, Instant value)

Payload.java:

Note that these methods are all post-fixed with Instant. This is to allow both the existing java.util.Date APIs to exist alongside the new APIs.

  • Instant getExpiresAtInstant()
  • Instant getNotBeforeInstant()
  • Instant getIssuedAtInstant()

Verification.java:

  • Verification withClaim(String name, Instant value)

A separate later PR will deprecate the java.util.Date APIs in favor of these new ones.

References

There are many articles and resources regarding the new date/time APIs introduced in JDK8. This article discusses it at a high level.

Testing

Tests updated to use the new Date/Time APIs.

Checklist

@jimmyjames jimmyjames added CH: Breaking Change large This PR may require significant effort to action, or contains many changes to review labels Feb 14, 2020
@jimmyjames jimmyjames added this to the v4-Next milestone Feb 14, 2020
@jimmyjames jimmyjames requested review from lbalmaceda and a team February 14, 2020 04:00
@stevehobbsdev
Copy link

stevehobbsdev commented Feb 14, 2020

@jimmyjames As someone reviewing this, it would be beneficial here to have some information in the PR about why these changes are being made. What are the benefits of Instant over Date?

@jimmyjames
Copy link
Contributor Author

@jimmyjames As someone reviewing this, it would be beneficial here to have some information in the PR about why these changes are being made. What are the benefits of Instant over Date?

I've updated the PR description to capture the motivations.

@jimmyjames jimmyjames force-pushed the updated-date-time-apis branch from 69415a3 to 1dcc415 Compare March 4, 2020 22:24
@jimmyjames jimmyjames requested a review from lbalmaceda March 4, 2020 22:26
@jimmyjames jimmyjames merged commit 6210713 into auth0:4.0.0-DEV Mar 5, 2020
@@ -1,15 +1,24 @@
package com.auth0.jwt.interfaces;

import java.time.Instant;
import java.util.Date;

/**
* The Clock class is used to wrap calls to Date class.
*/
public interface Clock {

Choose a reason for hiding this comment

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

... probably not appropriate to do it during this PR, but should this interface be deprecated in favor of java.time.Clock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CH: Added large This PR may require significant effort to action, or contains many changes to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants