steffenbrand/non-static-php-jwt
Composer 安装命令:
composer require steffenbrand/non-static-php-jwt
包简介
non-static wrapper for php-jwt
README 文档
README
non-static-php-jwt is a wrapper for firebase/php-jwt to make it easily mockable with phpspec/prophecy (or any other mocking library) within your phpunit tests.
Installation
composer require steffenbrand/non-static-php-jwt
Versioning
The releases will match the release versions of firebase/php-jwt starting with ^5.0.
The supported PHP versions will be ^7.1, since return types and type hinting are used in this library.
Usage
It's just a wrapper for firebase/php-jwt, so the usage is almost the same, except the fact that you have to create an instance of \SteffenBrand\NonStaticPhpJwt\Jwt first.
Encoding and decoding
$jwt = new \SteffenBrand\NonStaticPhpJwt\Jwt(); $key = 'example_key'; $token = [ 'iss' => 'http://example.org', 'aud' => 'http://example.com', 'iat' => 1356999524, 'nbf' => 1357000000 ]; $webToken = $jwt->encode($token, $key); $decoded = $jwt->decode($webToken, $key, ['HS256']); var_dump($decoded); print_r((array) $decoded);
Adding a leeway
You can add a leeway to account for when there is a clock skew times between the signing and verifying servers. It is recommended that this leeway should not be bigger than a few minutes.
Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
The leeway if the fourth parameter of the decode method and defaults to 0.
$jwt->decode($jwt, $key, ['HS256'], $leeway = 60);
Prophecising
The primary goal of this library is to allow prophecising the results of JWT methods within you phpunit tests.
<?php declare(strict_types=1); namespace SteffenBrand\NonStaticPhpJwt\Test; use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\ObjectProphecy; use SteffenBrand\NonStaticPhpJwt\Jwt; class JwtTest extends TestCase { /** * @var Jwt */ private $jwt; protected function setUp() { parent::setUp(); $this->jwt = $this->prophesize(Jwt::class); } public function getSut(): Dummy { return new Dummy($this->jwt->reveal()); } public function testJwtEncodeCanBeProphecised(): void { $returnValue = 'string'; $this->jwt->encode([], '')->shouldBeCalled()->willReturn($returnValue); $this->assertInstanceOf(ObjectProphecy::class, $this->jwt); $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('encode')[0]); $this->assertEquals($returnValue, $this->getSut()->encode()); } public function testJwtDecodeCanBeProphecised(): void { $returnValue = new \stdClass(); $this->jwt->decode('', '')->shouldBeCalled()->willReturn($returnValue); $this->assertInstanceOf(ObjectProphecy::class, $this->jwt); $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('decode')[0]); $this->assertEquals($returnValue, $this->getSut()->decode()); } public function testJwtSignCanBeProphecised(): void { $returnValue = 'string'; $this->jwt->sign('', '')->shouldBeCalled()->willReturn($returnValue); $this->assertInstanceOf(ObjectProphecy::class, $this->jwt); $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('sign')[0]); $this->assertEquals($returnValue, $this->getSut()->sign()); } }
steffenbrand/non-static-php-jwt 适用场景与选型建议
steffenbrand/non-static-php-jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.09k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 steffenbrand/non-static-php-jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 steffenbrand/non-static-php-jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-08-24