Class Jwt

java.lang.Object
com.vonage.jwt.Jwt

public final class Jwt extends Object
Class which allows declaratively specifying claims for generating Json Web Tokens (JWTs). The builder() static method provides the entry point, from which the mandatory and optional parameters can be specified. After calling Jwt.Builder.build(), the options can be re-used to create new tokens using the generate() method.

Signed JWTs can be verified using the static verifySignature(String, String) method.

  • Method Details

    • generate

      public String generate()
      Creates a new Base64-encoded JWT using the settings specified in the builder.
      Returns:
      A new Json Web Token as a string.
    • getApplicationId

      Returns the application_id claim.
      Returns:
      The Vonage application UUID.
    • getClaims

      public Map<String,?> getClaims()
      Returns all claims, both standard and non-standard.
      Returns:
      The claims on this JWT as a Map.
    • getId

      public String getId()
      Returns the jti claim.
      Returns:
      The JWT ID as a string, or null if unspecified.
    • getIssuedAt

      public Instant getIssuedAt()
      Returns the iat claim.
      Returns:
      The issue time as an Instant, or null if unspecified.
    • getNotBefore

      Returns the nbf claim.
      Returns:
      The start (not before) time as an Instant, or null if unspecified.
    • getExpiresAt

      Returns the exp claim.
      Returns:
      The expiry time as an Instant, or null if unspecified.
    • getSubject

      public String getSubject()
      Returns the sub claim.
      Returns:
      The subject, or null if unspecified.
    • builder

      public static Jwt.Builder builder()
      Instantiate a new Builder for building Jwt objects.
      Returns:
      A new Builder.
    • verifySignature

      public static boolean verifySignature(String token, String secret)
      Determines whether the provided JSON Web Token was signed by a given SHA-256 HMAC secret.
      Parameters:
      secret - The 256-bit symmetric HMAC signature.
      token - The encoded JWT to check.
      Returns:
      true iff the token was signed by the secret, false otherwise.
      Since:
      1.1.0