mehdibo / paseto-bundle
一个用于使用 Paseto 令牌的 Symfony Bundle
v1.0.0
2022-01-13 18:08 UTC
Requires
- php: ^7.4|^8.0|^8.1
- paragonie/paseto: ^1.1
- symfony/console: ^5.2
- symfony/framework-bundle: ^5.2
- symfony/yaml: ^5.2
Requires (Dev)
- infection/infection: ^0.25.5
- phpstan/phpstan: ^0.12.76
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-14 00:37:17 UTC
README
PasetoBundle 是一个将 Paseto 集成到 Symfony 应用的 Bundle。
安装
请确保已全局安装 Composer,如 Composer 文档中的 安装章节 所述。
步骤 1: 安装 Bundle
打开命令控制台,进入您的项目目录并执行以下命令以下载此 Bundle 的最新稳定版本
$ composer require mehdibo/paseto-bundle
步骤 2: 配置
将环境变量添加到 .env
###> mehdibo/paseto-bundle ### PASETO_SYMMETRIC_KEY= PASETO_ASYMMETRIC_SECRET_KEY= ###< mehdibo/paseto-bundle ###
您可以使用 Bundle 的命令生成密钥
./bin/console mehdibo:paseto:generate-symmetric ./bin/console mehdibo:paseto:generate-generate-asymmetric
创建配置文件 config/packages/mehdibo_paseto.yaml
mehdibo_paseto: secret_keys: symmetric_key: '%env(PASETO_SYMMETRIC_KEY)%' asymmetric_key: '%env(PASETO_ASYMMETRIC_SECRET_KEY)%'
然后,通过将其添加到项目 config/bundles.php 文件中注册的 Bundle 列表中来启用该 Bundle
// config/bundles.php return [ // ... Mehdibo\Bundle\PasetoBundle\MehdiboPasetoBundle::class => ['all' => true], ];
使用
您可以在 ExampleController 中查看使用示例。
创建 Paseto 令牌
您可以使用 Bundle 的服务来创建令牌。
// For building local tokens $localBuilder = new \Mehdibo\Bundle\PasetoBundle\Services\LocalPasetoBuilder(); // For building public tokens $publicBuilder = new \Mehdibo\Bundle\PasetoBundle\Services\PublicPasetoBuilder();
从控制器中
namespace App\Controller; use Mehdibo\Bundle\PasetoBundle\Services\LocalPasetoBuilder; use Mehdibo\Bundle\PasetoBundle\Services\PublicPasetoBuilder; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class TokensController extends AbstractController { #[Route('/public', name: 'public')] public function public(PublicPasetoBuilder $builder): Response { $builder->setIssuedAt()->setClaims(['custom' => 'claim']); return new Response($builder->toString()); } #[Route('/local', name: 'local')] public function local(LocalPasetoBuilder $builder): Response { $builder->setIssuedAt()->setClaims(['custom' => 'claim']); return new Response($builder->toString()); } }
解码 Paseto 令牌
您可以使用 Bundle 的服务来解码令牌
// For parsing local tokens $localParser = new \Mehdibo\Bundle\PasetoBundle\Services\LocalPasetoParser(); // For parsing public tokens $publicParser = new \Mehdibo\Bundle\PasetoBundle\Services\PublicPasetoParser();
从控制器中
namespace App\Controller; use Mehdibo\Bundle\PasetoBundle\Services\LocalPasetoParser; use Mehdibo\Bundle\PasetoBundle\Services\PublicPasetoParser; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; class TokensController extends AbstractController { #[Route('/public/decode', name: 'public_decode')] public function publicDecode(PublicPasetoParser $parser): JsonResponse { $token = $parser->parse("PUBLIC_TOKEN_HERE"); return new JsonResponse($token->getClaims()); } #[Route('/local/decode', name: 'local_decode')] public function localDecode(LocalPasetoParser $parser): JsonResponse { $token = $parser->parse("LOCAL_TOKEN_HERE"); return new JsonResponse($token->getClaims()); } }
命令
该 Bundle 提供了一些命令来帮助您使用 Paseto 令牌。
mehdibo:paseto:generate-symmetric # Generate a symmetric key mehdibo:paseto:generate-asymmetric # Generate a asymmetric keys mehdibo:paseto:generate-token # Generate a Paseto token
$> ./bin/console mehdibo:paseto:generate-token --purpose local --expires_at P01D --claim uid --claim 13 --claim article_id --claim 37 v2.local.nn7biqHnkvU3JgJdfeVNqHlxsub_QEOsSAeGg2hdEVvPi_lxYwL01dSGjYw43P8PE0zorghJq2S6Czo8ztTxQ_UlSeYqPehXJ498Rk3Y9ouwqj2Z9j0Bk1uSbEBSqXPdr1GeeM0kpPk