Class SDObjectBuilder

java.lang.Object
com.authlete.sd.SDObjectBuilder

public class SDObjectBuilder extends Object
A utility to create a Map instance that represents a JSON object which may contain the "_sd" array and the "_sd_alg" claim.

Example:

 Disclosure disclosure = Disclosure.parse(
     "WyI2cU1RdlJMNWhhaiIsICJmYW1pbHlfbmFtZSIsICJNw7ZiaXVzIl0");

 // Create an SDObjectBuilder instance with the default
 // hash algorithm "sha-256".
 SDObjectBuilder builder = new SDObjectBuilder();

 // Add the digest of the disclosure.
 builder.putSDClaim(disclosure);

 // Add an arbitrary claim.
 String claimName  = "my_claim_name";
 String claimValue = "my_claim_value";
 builder.putClaim(claimName, claimValue);

 // Build a map that represents a JSON object.
 Map<String, Object> map = builder.build(true);

 // map ->
 // {
 //   "my_claim_name": "my_claim_value",
 //   "_sd": [
 //     "uutlBuYeMDyjLLTpf6Jxi7yNkEF35jdyWMn9U7b_RYY"
 //   ],
 //   "_sd_alg": "sha-256"
 // }
 
Since:
1.0
  • Constructor Details

    • SDObjectBuilder

      public SDObjectBuilder()
      Constructor with the default hash algorithm ("sha-256").

      The hash algorithm is used when computing digest values that are to be listed in the "_sd" array.

    • SDObjectBuilder

      public SDObjectBuilder(String hashAlgorithm)
      Constructor with the specified hash algorithm.

      The hash algorithm is used when computing digest values that are to be listed in the "_sd" array.

      Parameters:
      hashAlgorithm - A hash algorithm. If null is given, the default hash algorithm ("sha-256") is used.
  • Method Details

    • getHashAlgorithm

      public String getHashAlgorithm()
      Get the hash algorithm that has been specified by the constructor.

      The hash algorithm is used when computing digest values that are to be listed in the "_sd" array.

      Returns:
      The hash algorithm.
    • putClaim

      public void putClaim(String claimName, Object claimValue)
      Put a normal claim.
      Parameters:
      claimName - A claim name. Must not be null.
      claimValue - A claim value. May be null.
      Throws:
      IllegalArgumentException - The given claim name is null or a key reserved by the SD-JWT specification.
    • putSDClaim

      public Disclosure putSDClaim(Disclosure disclosure)
      Put the digest value of a selectively-disclosable claim.

      The digest of the given disclosure will appear in the "_sd" array.

      Parameters:
      disclosure - The disclosure of the claim.
      Returns:
      The Disclosure instance given to this method.
      Throws:
      IllegalArgumentException - The given disclosure is null, or the disclosure is not for an object property.
    • putSDClaim

      public Disclosure putSDClaim(String claimName, Object claimValue)
      Put the digest value of a selectively-disclosable claim.

      This method is an alias of putSDClaim(new Disclosure(claimName, claimValue)).

      Parameters:
      claimName - A claim name. Must not be null.
      claimValue - A claim value. May be null.
      Returns:
      A Disclosure instance that was created for the specified claim.
      Throws:
      IllegalArgumentException - The given claim name is null.
    • putSDClaim

      public Disclosure putSDClaim(String salt, String claimName, Object claimValue)
      Put the digest value of a selectively-disclosable claim.

      This method is an alias of putSDClaim(new Disclosure(salt, claimName, claimValue)).

      Parameters:
      salt - A salt. Must not be null.
      claimName - A claim name. Must not be null.
      claimValue - A claim value. May be null.
      Returns:
      A Disclosure instance that was created for the specified claim.
      Throws:
      IllegalArgumentException - The given salt and/or claim name are null.
    • putDecoyDigest

      public String putDecoyDigest()
      Put a decoy digest value, which will appear in the "_sd" array.
      Returns:
      The base64url-encoded digest value of a randomly-generated value computed with the hash algorithm.
    • putDecoyDigests

      public List<String> putDecoyDigests(int count)
      Put decoy digest values, which will appear in the "_sd" array.
      Parameters:
      count - The number of decoy digest values to add.
      Returns:
      A list of base64url-encoded digest values of randomly-generated values computed with the hash algorithm.
    • build

      public Map<String,Object> build()
      Create a Map instance that represents a JSON object which may include the "_sd" array.

      This method is an alias of build(false).

      Returns:
      A Map instance that represents a JSON object.
    • build

      public Map<String,Object> build(boolean hashAlgorithmIncluded)
      Create a Map instance that represents a JSON object which may include the "_sd" array and the "_sd_alg" claim.

      The "_sd" array will not appear in the created map if no digest value has been put to this builder. In other words, if none of the putSDClaim method variants, the putDecoyDigest() method and the putDecoyDigests(int) method have been called, the "_sd" array will not appear.

      Parameters:
      hashAlgorithmIncluded - true to include the "_sd_alg" claim in the created map.
      Returns:
      A Map instance that represents a JSON object.