ucarsolutions/uri-signer
Composer 安装命令:
composer require ucarsolutions/uri-signer
包简介
A minimal package for securely signing PSR-7 URIs in PHP applications.
README 文档
README
A URL signer implementation in PHP that generates secure, signed URLs with an expiration date. This package allows you to sign full URLs or just query parameters, adding a layer of security for accessing resources or sharing sensitive information.
Features
- Sign a given URI or an array of parameters
- Include an expiration date as part of the signature
- Ensure URL integrity and prevent unauthorized modifications
- Easy integration with Laminas or other PHP-based frameworks
Installation
Install the package via Composer:
composer require ucarsolutions/uri-signer
Usage
Sign URL with Expiration Date
You can sign the uri with:
<?php require_once __DIR__ . '/vendor/autoload.php'; $signerService = new \UcarSolutions\UriSigner\Service\UriSignerService( new \UcarSolutions\UriSigner\Resolver\DefaultParameterNameResolver(), new \doganoo\DIP\DateTime\DateTimeService(), new \Psr\Log\NullLogger() ); $key = new class implements \UcarSolutions\UriSigner\Entity\KeyInterface { public function getKey(): string { return "t0psecret"; } }; $uri = $signerService->sign( new \Laminas\Diactoros\Uri("https://example.com"), $key ); dump((string)$uri); // https://example.com/?__us_signature=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3VjYXItc29sdXRpb25zLmRlL3VyaS1zaWduZXIiLCJleHAiOjE3MjU5MDYzMDQsInN1YiI6IlNpZ25lZCBVUkwiLCJ1cmwiOiJodHRwczovL2V4YW1wbGUug29tIiwidWlkIjoiNzM3YTgwNzAtZGU5MS00MTQ3LWohYmMtZTY1OWZiOGZmNWZyIn0.CH7E-fHYhtfGHUljB85dIWL-ZYGr8wRMVef0gY_SRLE
Example Verifying with $uri above:
<?php $result = $signerService->verify($uri,$key); dump($result->isVerified());
Same for parameters:
<?php require_once __DIR__ . '/vendor/autoload.php'; $signerService = new \UcarSolutions\UriSigner\Service\ParameterSignerService( new \doganoo\DIP\DateTime\DateTimeService(), new \Psr\Log\NullLogger() ); $key = new class implements \UcarSolutions\UriSigner\Entity\KeyInterface { public function getKey(): string { return "t0psecret"; } }; $token = $signerService->sign( ['leadId' => '123', 'list' => 'marketing', 'aud' => 'dmarcflow.com'], $key ); dump($token); // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3VjYXItc29sdXRpb25zLmRlL3VyaS1zaWduZXIiLCJpYXQiOjE3NTg2OTIwMDUsImV4cCI6MTc1ODY5MjE4NSwic3ViIjoiU2lnbmVkIFBheWxvYWQiLCJqdGkiOiJhNjRlNGE4Yi0wMWUzLTRjZDEtYWRlZi02ZmQzNjY1Y2E0ZDYiLCJ1aWQiOiJlZjM1YzEwNC1lYzM2LTQ3YTItOTE2Ni1lOGJiNmU0MThiMzIiLCJkYXRhIjp7ImxlYWRJZCI6IjEyMyIsImxpc3QiOiJtYXJrZXRpbmciLCJhdWQiOiJkbWFyY2Zsb3cuY29tIn19.Ovt1TnqJLTXdc0fQykDxCbiLdxG0_mKASyFB2JKidbA
Example Verifying with $token above:
<?php $result = $signerService->verify($token,$key); dump($result->isVerified());
Expiration
The expiration date is added to the signature and is included in the signed data to ensure the URL becomes invalid after the expiration time.
If no expiration date is provided, a default of 3 minutes from the current time is used.
Configuration
You can configure the expiration time and the secret key for signing URLs.
Tests
Run the tests with PHPUnit:
vendor/bin/phpunit
Contributing
Contributions are welcome! Please submit a pull request or open an issue for any suggestions or bug reports.
Contribution Guidelines:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Write tests for your changes.
- Make sure all tests pass.
- Submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
ucarsolutions/uri-signer 适用场景与选型建议
ucarsolutions/uri-signer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 521 次下载、GitHub Stars 达 25, 最近一次更新时间为 2024 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ucarsolutions/uri-signer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ucarsolutions/uri-signer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 521
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-09