Class Key
- All Implemented Interfaces:
Serializable
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 Summary
Modifier and TypeMethodDescriptionstatic KeyCreates a new Key with the given prefix.booleanstatic KeyConstructs a new Key object from the given key string.getHash()Returns the hash associated with this Key object.Retrieves the long token associated with the key.Retrieves the prefix of the Key object.Returns the short token of the key.inthashCode()booleanChecks if the given short token and hash match the short token and hash of this Key object.toString()
-
Method Details
-
create
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
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 stringIllegalArgumentException- if the key is not in the valid format '{prefix}_{short token}_{long token}'
-
getPrefix
Retrieves the prefix of the Key object.- Returns:
- the prefix of the Key object
-
getShortToken
Returns the short token of the key.- Returns:
- the short token
-
getLongToken
Retrieves the long token associated with the key.- Returns:
- the long token
-
getHash
Returns the hash associated with this Key object.- Returns:
- the hash value associated with this Key object
-
matches
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 objecthash- The hash to be checked against the hash of this Key object- Returns:
- true if the short token and hash match, false otherwise
-
toString
-
equals
-
hashCode
public int hashCode()
-