The master branch is under active development and functionality may change or break at any time!
Security

Signed tokens



The SignedToken class lets you create and validate time-limited signed tokens. The tokens are stateless, meaning that nothing has to be stored in a database or cache, which makes them a good fit for things like password reset links, email verification links and unsubscribe links.

A token only contains its expiration time and a HMAC signature. The identifier and metadata used when creating the token are never included in the token itself, so the same values must be provided when validating it.


Usage

First we'll need a SignedToken instance. You can use the default signer, which signs tokens using the application secret, or any other signer instance.

$signedToken = new SignedToken($this->signer);

// You can also use a signer with a different secret

$signedToken = new SignedToken(new Signer('secret_used_to_sign_tokens'));

Tokens are tied to the secret they were signed with, so changing the secret will invalidate all previously issued tokens.

The create method returns a URL-safe signed token. The first parameter is a string identifying what the token is for and the second parameter is the number of seconds the token should be valid for. The optional third parameter is an array of metadata (string keys with string or integer values) that binds additional data to the token.

$token = $signedToken->create("password-reset:{$user->getId()}", 3600);

// You can also bind metadata to the token

$token = $signedToken->create(
	"password-reset:{$user->getId()}",
	3600,
	['action_token' => $user->getActionToken()]
);

Make sure that the identifier and metadata uniquely identify both the purpose of the token and the subject it belongs to so that a token can't be reused for another purpose or user.

Note: Tokens can be used any number of times until they expire. If you need to invalidate a token once it has been used then you can bind it to a value that changes, such as the Gatekeeper action token, and regenerate the value upon a successful action.

The validate method returns true if the token is valid and hasn't expired and false if not. The identifier and metadata must be the same as the ones used when creating the token.

$valid = $signedToken->validate($token, "password-reset:{$user->getId()}");

// The same metadata that was used when creating the token is required for validation

$valid = $signedToken->validate(
	$token,
	"password-reset:{$user->getId()}",
	['action_token' => $user->getActionToken()]
);

Note: Metadata values are type-sensitive, so a token created with ['user' => 1] will not validate with ['user' => '1']. The order of the keys does not matter.

The validateOrThrow method works the same way but throws a TokenException instead of returning false.

$signedToken->validateOrThrow($token, "password-reset:{$user->getId()}");

// The same metadata that was used when creating the token is required for validation

$signedToken->validateOrThrow(
	$token,
	"password-reset:{$user->getId()}",
	['action_token' => $user->getActionToken()]
);