Class IdentityTokenFormatter

java.lang.Object
dev.orne.beans.IdentityTokenFormatter

@API(status=INTERNAL, since="0.1") public final class IdentityTokenFormatter extends Object
Formatter and parser of identity tokens.
Since:
0.1
Version:
2.0, 2023-12
Author:
(w) Iker Hernaez
  • Field Details

    • TOKEN_CHAR

      public static final String TOKEN_CHAR
      Regular expression for valid token characters.
      See Also:
    • PREFIX

      public static final String PREFIX
      Regular expression for valid token prefix strings.
      See Also:
    • UNENCODED_BODY_STARTING_CHAR

      public static final String UNENCODED_BODY_STARTING_CHAR
      Regular expression for valid unencoded body starting character.
      See Also:
    • UNENCODED_BODY

      public static final String UNENCODED_BODY
      Regular expression for valid identity token unencoded bodies.
      See Also:
    • ENCODED_BODY_PREFIX

      public static final String ENCODED_BODY_PREFIX
      Encoded body prefix.
      See Also:
    • NULL_BODY

      public static final String NULL_BODY
      Null body.
      See Also:
    • ENCODED_BODY

      public static final String ENCODED_BODY
      Regular expression for valid identity token Base64 encoded bodies.
      See Also:
    • BODY

      public static final String BODY
      Regular expression for valid identity token bodies.
      See Also:
    • TOKEN

      public static final String TOKEN
      Regular expression for valid identity tokens.
      See Also:
    • DEFAULT_PREFIX

      public static final String DEFAULT_PREFIX
      Default identity token prefix.
      See Also:
    • VALID_PREFIX_PREDICATE

      protected static final Predicate<String> VALID_PREFIX_PREDICATE
      Compiled pattern to detect valid identity token prefixes as predicate.
    • VALID_UNENCODED_BODY_PREDICATE

      protected static final Predicate<String> VALID_UNENCODED_BODY_PREDICATE
      Compiled pattern to detect valid identity token unencoded bodies as predicate.
    • VALID_ENCODED_BODY_PREDICATE

      protected static final Predicate<String> VALID_ENCODED_BODY_PREDICATE
      Compiled pattern to detect valid identity token encoded bodies as predicate.
    • VALID_BODY_PREDICATE

      protected static final Predicate<String> VALID_BODY_PREDICATE
      Compiled pattern to detect valid identity token bodies as predicate.
    • VALID_TOKEN_PREDICATE

      protected static final Predicate<String> VALID_TOKEN_PREDICATE
      Compiled pattern to detect valid identity tokens as predicate.
  • Method Details

    • encodeBody

      @NotNull protected static @NotNull String encodeBody(String body)

      Encodes the specified identity token body as a valid identity token body.

      1. If the specified body is null NULL_BODY is returned.
      2. If the specified body is empty an empty String is returned.
      3. If the specified body is a valid unencoded body the passed body is returned.
      4. Otherwise the passed body is encoded in URI friendly Base64, with paddings removed and prefixed by ENCODED_BODY_PREFIX.
      Parameters:
      body - The identity token body to encode
      Returns:
      The encoded identity token part
    • decodeBody

      protected static String decodeBody(@NotNull @NotNull String encoded) throws UnrecognizedIdentityTokenException

      Decodes the specified valid identity token body to the original identity token body.

      Parameters:
      encoded - The valid identity token body to decode
      Returns:
      The original identity token body
      Throws:
      NullPointerException - If the encoded body is null
      UnrecognizedIdentityTokenException - If the encoded body is not a valid identity token body
    • format

      @NotNull public static @NotNull String format(String body)
      Formats a valid identity token for the specified identity token body and the default identity token prefix.
      Parameters:
      body - The identity token body
      Returns:
      The formatted identity token
    • format

      @NotNull public static @NotNull String format(@NotNull @NotNull String prefix, String body)
      Formats a valid identity token for the specified identity token prefix and body.
      Parameters:
      prefix - The identity token prefix
      body - The identity token body
      Returns:
      The formatted identity token
      Throws:
      NullPointerException - If the prefix is null
      IllegalArgumentException - If the prefix is not a valid identity token prefix
    • isValidPrefix

      public static boolean isValidPrefix(@NotNull @NotNull String prefix)
      Returns true if the specified identity token prefix is valid.
      Parameters:
      prefix - The identity token prefix.
      Returns:
      If the identity token prefix is valid.
    • isValidBody

      public static boolean isValidBody(@NotNull @NotNull String body)
      Returns true if the specified identity token body is valid.
      Parameters:
      body - The identity token body.
      Returns:
      If the identity token body is valid.
    • isValidUncodedBody

      public static boolean isValidUncodedBody(@NotNull @NotNull String body)
      Returns true if the specified identity token body is a valid unencoded body.
      Parameters:
      body - The identity token body.
      Returns:
      If the identity token body is valid.
    • isValidEncodedBody

      public static boolean isValidEncodedBody(@NotNull @NotNull String body)
      Returns true if the specified identity token body is a valid encoded body.
      Parameters:
      body - The identity token body.
      Returns:
      If the identity token body is valid.
    • isValidToken

      public static boolean isValidToken(@NotNull @NotNull String token)
      Returns true if the specified identity token is valid.
      Parameters:
      token - The identity token.
      Returns:
      If the identity token is valid.
    • parse

      public static String parse(@NotNull @NotNull String token) throws UnrecognizedIdentityTokenException
      Parses the specified identity token for the default identity token prefix. If the token is valid and starts with the default prefix the original identity token body used when formatted is returned.
      Parameters:
      token - The identity token
      Returns:
      The original identity token body
      Throws:
      NullPointerException - If the identity token is null
      UnrecognizedIdentityTokenException - If the identity token is not a valid identity token or it doesn't start with the expected prefix
    • parse

      public static String parse(@NotNull @NotNull String prefix, @NotNull @NotNull String token) throws UnrecognizedIdentityTokenException
      Parses the specified identity token for the specified expected identity token prefix. If the token is valid and starts with the specified prefix the original identity token body used when formatted is returned.
      Parameters:
      prefix - The expected identity token prefix
      token - The identity token
      Returns:
      The original identity token body
      Throws:
      NullPointerException - If the prefix or the identity token are null
      IllegalArgumentException - If the prefix is not a valid identity token prefix
      UnrecognizedIdentityTokenException - If the identity token is not a valid identity token or it doesn't start with the expected prefix