PHP code example of idct / single-use-token-manager
1. Go to this page and download the library: Download idct/single-use-token-manager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
idct / single-use-token-manager example snippets
use IDCT\SingleUseTokenManager\TokenService;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;
$service = new TokenService(new Psr16Cache(new ArrayAdapter()));
// Issue a token that lives for an hour.
$token = $service->createToken('reset', ['user_id' => 123], 3600);
$token->getUid(); // '7c6b0c0c-d9b0-45af-a9c6-79ab7dd5c35d'
$token->getType(); // 'reset'
$token->getPayload(); // ['user_id' => 123]
// Redeem it. The token is removed from the cache as it is returned.
$redeemed = $service->consumeToken($token->getUid());
// A second attempt finds nothing, which is what makes it single use.
$service->consumeToken($token->getUid()); // null
// Rendering a "your reset link is valid" page. Nothing is spent.
$token = $service->consumeToken($uid, keepToken: true);
// Wrong. Two requests can both pass the check and both do the work.
if (null === $service->consumeToken($uid, keepToken: true)) {
throw new NotFoundHttpException();
}
doTheExpensiveThing();
$service->consumeToken($uid);
$token = $service->consumeToken($uid);
if (null === $token) {
throw new NotFoundHttpException();
}
// The token is spent. Nobody else can redeem it.
doTheExpensiveThing($token->getPayload());
$service->createToken(
type: 'reset',
payload: ['code' => $code, 'email' => $newAddress],
ttl: 600,
uid: "reset.{$userId}",
);
// later, from {id, code} in the request body
$token = $service->consumeToken("reset.{$userId}");
$token = $service->consumeToken("reset.{$userId}");
if (null === $token || !hash_equals($token->getPayload()['code'], $submittedCode)) {
throw new AccessDeniedException();
}
public function take(string $key): mixed; // return the value and remove it, atomically
use IDCT\Cache\RapidCacheClient;
use IDCT\Cache\RedisConnectionConfig;
$cache = new RapidCacheClient(new RedisConnectionConfig(host: '127.0.0.1', port: 6379));
// Tagging and atomic redemption are both picked up automatically.
$service = new TokenService($cache);
use IDCT\Cache\RapidCacheClient;
use IDCT\Cache\RedisConnectionConfig;
use IDCT\SingleUseTokenManager\TokenService;
$cache = new RapidCacheClient(new RedisConnectionConfig(host: '127.0.0.1', port: 6379));
$cache->set('unrelated', 'some other value');
$service = new TokenService($cache);
$service->createToken('reset', ['user_id' => 123]);
$service->clearAllTokens();
$cache->get('unrelated'); // still 'some other value'
$resets = new TokenService($cache, 'reset_');
$invites = new TokenService($cache, 'invite_');
$token = $resets->createToken('reset', ['user_id' => 123]);
$invites->consumeToken($token->getUid()); // null, it is not theirs
$invites->clearAllTokens(); // leaves the reset tokens alone
use IDCT\SingleUseTokenManager\Model\TokenIdentifier;
use Symfony\Component\Validator\Validation;
$validator = Validation::createValidatorBuilder()
->enableAttributeMapping()
->getValidator();
$identifier = new TokenIdentifier();
$identifier->token = $request->get('token');
if (count($validator->validate($identifier)) > 0) {
throw new BadRequestHttpException();
}
$token = $service->consumeToken($identifier->token);
bash
composer install
php example.php
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.