Class JOSEHeader

java.lang.Object
de.christofreichardt.json.websignature.JOSEHeader
All Implemented Interfaces:
de.christofreichardt.diagnosis.Traceable

public class JOSEHeader extends Object implements de.christofreichardt.diagnosis.Traceable
A convenient class useful to define a JOSE Header containing the parameters describing the cryptographic operations and parameters employed. At present only a subset of the header parameter specified within RFC 7515 is supported. To create an actual JOSEHeader instance use one of the static factory methods. Two of them provide special builders to programmatically create a JOSEHeader instance. The third expects a suitable JsonObject.

Example

First we generate a key pair and build a JSON Web Key:
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256r1");
keyPairGenerator.initialize(ecGenParameterSpec);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
JsonWebPublicKey jsonWebPublicKey = JsonWebPublicKey.of(keyPair.getPublic())
    .withKid(UUID.randomUUID().toString())
    .build();
Now we can programmatically build a JOSEHeader:
JOSEHeader joseHeader = JOSEHeader.of(jsonWebPublicKey)
    .withTyp("JWT")
    .build();
Author:
Christof Reichardt
See Also:
  • Method Details

    • getAlg

      public String getAlg()
      Returns the configured "alg" (Algorithm).
      Returns:
      the configured "alg".
      See Also:
    • getTyp

      public String getTyp()
      Returns the configured "typ" (Type).
      Returns:
      the configured "typ".
      See Also:
    • getKid

      public String getKid()
      Returns the configured "kid" (Key ID)
      Returns:
      the configured "kid".
      See Also:
    • getJsonWebPublicKey

      public JsonWebPublicKey getJsonWebPublicKey()
      Returns the configured "jwk" (JSON Web Key)
      Returns:
      the configured "jwk".
      See Also:
    • equals

      public boolean equals(Object object)
      Checks if this JOSE header is equal to another header. Compares this JOSEHeader with another ensuring that it contains the same configuration. Only objects of type JOSEHeader are considered, other types return false.
      Overrides:
      equals in class Object
      Parameters:
      object - the object to check, null returns false
      Returns:
      true if this is equal to the other header
    • hashCode

      public int hashCode()
      A hash code for this header.
      Overrides:
      hashCode in class Object
      Returns:
      a suitable hash code.
    • of

      public static JOSEHeader.AlgorithmBuilder of(String alg)
      Creates a special builder for a JOSEHeader. Use this variant to build a JOSEHeader if you don't want to specify a JSON web key within the header.
      Parameters:
      alg - the desired "alg" parameter.
      Returns:
      an AlgorithmBuilder.
    • of

      public static JOSEHeader.PublicKeyBuilder of(JsonWebPublicKey jsonWebPublicKey)
      Creates a special builder for a JOSEHeader. Use this variant to build a JOSEHeader if you want to specify a JSON web key within the header. The corresponding "alg" parameter will be deduced from the given public key.
      Parameters:
      jsonWebPublicKey - the desired public key referenced by the "jwk" parameter.
      Returns:
      a PublicKeyBuilder.
    • toJson

      public jakarta.json.JsonObject toJson()
      Converts this JOSEHeader into a plain JsonObject.
      Returns:
      a JsonObject corresponding to this JOSEHeader
      See Also:
    • fromJson

      public static JOSEHeader fromJson(jakarta.json.JsonObject joseHeaderView) throws GeneralSecurityException
      Factory method to create a JOSEHeader instance from a plain JsonObject.
      Parameters:
      joseHeaderView - the given JsonObject.
      Returns:
      a JOSEHeader
      Throws:
      GeneralSecurityException - forwarded from the Java Platform API, indicates difficulties when creating a JSON web key from the given JsonObject.
      See Also:
    • getCurrentTracer

      public de.christofreichardt.diagnosis.AbstractTracer getCurrentTracer()
      Specified by:
      getCurrentTracer in interface de.christofreichardt.diagnosis.Traceable