Class UnsubscribeTokenCodec
java.lang.Object
fr.becpg.repo.activity.unsubscribe.UnsubscribeTokenCodec
Creates and reads the signed tokens carried by the one-click unsubscribe link of the activity emails.
A token reads base64url(userName).expiry.base64url(signature), where the expiry is in epoch seconds and the
signature is the HMAC-SHA256, under the key of the instance, of the instance host, the encoded user name and the
expiry. Signing the host keeps a token of one instance from working on a copy of it that shares the same key.
Thread-safe: instances are immutable.
- Author:
- valentin
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Clockprivate static final Base64.Decoderprivate static final Base64.Encoderprivate static final Stringprivate static final Stringprivate final Stringprivate final SecretKeySpecprivate static final Stringprivate static final Stringprivate static final intstatic final DurationHow long an unsubscribe link stays valid after the email was sent. -
Constructor Summary
ConstructorsConstructorDescriptionUnsubscribeTokenCodec(byte[] key, String instanceHost, Clock clock) Builds a codec bound to one instance. -
Method Summary
Modifier and TypeMethodDescriptioncreateToken(String userName) Creates a token designating the given user, valid forVALIDITY.private booleanprivate booleanisSignedBy(String[] parts) readUserName(String token) Reads the user designated by a token.private byte[]
-
Field Details
-
VALIDITY
How long an unsubscribe link stays valid after the email was sent. -
HMAC_ALGORITHM
- See Also:
-
SEPARATOR
- See Also:
-
SEPARATOR_REGEX
- See Also:
-
HOST_SEPARATOR
- See Also:
-
TOKEN_PARTS
private static final int TOKEN_PARTS- See Also:
-
ENCODER
-
DECODER
-
key
-
instanceHost
-
clock
-
-
Constructor Details
-
UnsubscribeTokenCodec
Builds a codec bound to one instance.- Parameters:
key- the secret key of the instanceinstanceHost- the host name of the instance, part of the signed dataclock- the clock giving the current time
-
-
Method Details
-
createToken
Creates a token designating the given user, valid forVALIDITY.- Parameters:
userName- the user to unsubscribe- Returns:
- the URL-safe token
-
readUserName
Reads the user designated by a token.- Parameters:
token- the token received- Returns:
- the user name, or empty when the token is malformed, altered, expired or signed for another instance
-
readParts
-
isSignedBy
-
isExpired
-
sign
-