Class Key

java.lang.Object
com.helixframework.apikey.Key
All Implemented Interfaces:
Serializable

public class Key extends Object implements Serializable
A class representing a prefixed api key.

This class provides methods for creating and manipulating keys consisting of a prefix, short token, and long token.

Prefixed api keys look like this: ``` foopartner_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG ``` Let's break down each component of the API key... ``` foopartner ..._... BRTRKFsL ..._... 51FwqftsmMDHHbJAMEXXHCgG ^ ^ ^ Prefix Short Token Long Token ``` - The Prefix is used to identify the company or service creating the API Key. This is very helpful in secret scanning. - The Short Token is stored by both the server and the key bearer/customer, it can be used to identify an API key in logs or displayed on a customer's dashboard. A token can be blacklisted by its short token. - The Long Token is how we authenticate this key. The long token is never stored on the server, but a hash of it is stored on the server. When you receive an incoming request, you search your database for `short_token` and `hash(long_token)`.

See Also:
  • Method Details

    • create

      public static Key create(String prefix)
      Creates a new Key with the given prefix.
      Parameters:
      prefix - the prefix for the Key
      Returns:
      a new Key object
      Throws:
      IllegalArgumentException - if the prefix is null or a blank string
    • from

      public static Key from(String key)
      Constructs a new Key object from the given key string.
      Parameters:
      key - the key string
      Returns:
      a new Key object
      Throws:
      IllegalArgumentException - if the key is null or a blank string
      IllegalArgumentException - if the key is not in the valid format '{prefix}_{short token}_{long token}'
    • getPrefix

      public String getPrefix()
      Retrieves the prefix of the Key object.
      Returns:
      the prefix of the Key object
    • getShortToken

      public String getShortToken()
      Returns the short token of the key.
      Returns:
      the short token
    • getLongToken

      public String getLongToken()
      Retrieves the long token associated with the key.
      Returns:
      the long token
    • getHash

      public String getHash()
      Returns the hash associated with this Key object.
      Returns:
      the hash value associated with this Key object
    • matches

      public boolean matches(String shortToken, String hash)
      Checks if the given short token and hash match the short token and hash of this Key object.
      Parameters:
      shortToken - The short token to be checked against the short token of this Key object
      hash - The hash to be checked against the hash of this Key object
      Returns:
      true if the short token and hash match, false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object