Package dev.orne.beans
Class IdentityTokenFormatter
java.lang.Object
dev.orne.beans.IdentityTokenFormatter
Formatter and parser of identity tokens.
- Since:
- 0.1
- Version:
- 2.0, 2023-12
- Author:
- (w) Iker Hernaez
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Regular expression for valid identity token bodies.static final String
Default identity token prefix.static final String
Regular expression for valid identity token Base64 encoded bodies.static final String
Encoded body prefix.static final String
Null body.static final String
Regular expression for valid token prefix strings.static final String
Regular expression for valid identity tokens.static final String
Regular expression for valid token characters.static final String
Regular expression for valid identity token unencoded bodies.static final String
Regular expression for valid unencoded body starting character.Compiled pattern to detect valid identity token bodies as predicate.Compiled pattern to detect valid identity token encoded bodies as predicate.Compiled pattern to detect valid identity token prefixes as predicate.Compiled pattern to detect valid identity tokens as predicate.Compiled pattern to detect valid identity token unencoded bodies as predicate. -
Method Summary
Modifier and TypeMethodDescriptionprotected static String
decodeBody
(@NotNull String encoded) Decodes the specified valid identity token body to the original identity token body.protected static @NotNull String
encodeBody
(String body) Encodes the specified identity token body as a valid identity token body.static @NotNull String
Formats a valid identity token for the specified identity token prefix and body.static @NotNull String
Formats a valid identity token for the specified identity token body and the default identity token prefix.static boolean
isValidBody
(@NotNull String body) Returnstrue
if the specified identity token body is valid.static boolean
isValidEncodedBody
(@NotNull String body) Returnstrue
if the specified identity token body is a valid encoded body.static boolean
isValidPrefix
(@NotNull String prefix) Returnstrue
if the specified identity token prefix is valid.static boolean
isValidToken
(@NotNull String token) Returnstrue
if the specified identity token is valid.static boolean
isValidUncodedBody
(@NotNull String body) Returnstrue
if the specified identity token body is a valid unencoded body.static String
Parses the specified identity token for the default identity token prefix.static String
Parses the specified identity token for the specified expected identity token prefix.
-
Field Details
-
TOKEN_CHAR
Regular expression for valid token characters.- See Also:
-
PREFIX
Regular expression for valid token prefix strings.- See Also:
-
UNENCODED_BODY_STARTING_CHAR
Regular expression for valid unencoded body starting character.- See Also:
-
UNENCODED_BODY
Regular expression for valid identity token unencoded bodies.- See Also:
-
ENCODED_BODY_PREFIX
Encoded body prefix.- See Also:
-
NULL_BODY
Null body.- See Also:
-
ENCODED_BODY
Regular expression for valid identity token Base64 encoded bodies.- See Also:
-
BODY
Regular expression for valid identity token bodies.- See Also:
-
TOKEN
Regular expression for valid identity tokens.- See Also:
-
DEFAULT_PREFIX
Default identity token prefix.- See Also:
-
VALID_PREFIX_PREDICATE
Compiled pattern to detect valid identity token prefixes as predicate. -
VALID_UNENCODED_BODY_PREDICATE
Compiled pattern to detect valid identity token unencoded bodies as predicate. -
VALID_ENCODED_BODY_PREDICATE
Compiled pattern to detect valid identity token encoded bodies as predicate. -
VALID_BODY_PREDICATE
Compiled pattern to detect valid identity token bodies as predicate. -
VALID_TOKEN_PREDICATE
Compiled pattern to detect valid identity tokens as predicate.
-
-
Method Details
-
encodeBody
Encodes the specified identity token body as a valid identity token body.
- If the specified body is
null
NULL_BODY
is returned. - If the specified body is empty an empty
String
is returned. - If the specified body is a valid unencoded body the passed body is returned.
- 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
- If the specified body is
-
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 isnull
UnrecognizedIdentityTokenException
- If the encoded body is not a valid identity token body
-
format
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
Formats a valid identity token for the specified identity token prefix and body.- Parameters:
prefix
- The identity token prefixbody
- The identity token body- Returns:
- The formatted identity token
- Throws:
NullPointerException
- If the prefix isnull
IllegalArgumentException
- If the prefix is not a valid identity token prefix
-
isValidPrefix
Returnstrue
if the specified identity token prefix is valid.- Parameters:
prefix
- The identity token prefix.- Returns:
- If the identity token prefix is valid.
-
isValidBody
Returnstrue
if the specified identity token body is valid.- Parameters:
body
- The identity token body.- Returns:
- If the identity token body is valid.
-
isValidUncodedBody
Returnstrue
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
Returnstrue
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
Returnstrue
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 isnull
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 prefixtoken
- The identity token- Returns:
- The original identity token body
- Throws:
NullPointerException
- If the prefix or the identity token arenull
IllegalArgumentException
- If the prefix is not a valid identity token prefixUnrecognizedIdentityTokenException
- If the identity token is not a valid identity token or it doesn't start with the expected prefix
-