项目作者: steffenbrand

项目描述 :
non-static wrapper for php-jwt
高级语言: PHP
项目地址: git://github.com/steffenbrand/non-static-php-jwt.git
创建时间: 2018-08-24T10:51:26Z
项目社区:https://github.com/steffenbrand/non-static-php-jwt

开源协议:BSD 3-Clause "New" or "Revised" License

下载


non-static-php-jwt

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

  1. 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

  1. $jwt = new \SteffenBrand\NonStaticPhpJwt\Jwt();
  2. $key = 'example_key';
  3. $token = [
  4. 'iss' => 'http://example.org',
  5. 'aud' => 'http://example.com',
  6. 'iat' => 1356999524,
  7. 'nbf' => 1357000000
  8. ];
  9. $webToken = $jwt->encode($token, $key);
  10. $decoded = $jwt->decode($webToken, $key, ['HS256']);
  11. var_dump($decoded);
  12. 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.

  1. $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.

  1. <?php
  2. declare(strict_types=1);
  3. namespace SteffenBrand\NonStaticPhpJwt\Test;
  4. use PHPUnit\Framework\TestCase;
  5. use Prophecy\Prophecy\MethodProphecy;
  6. use Prophecy\Prophecy\ObjectProphecy;
  7. use SteffenBrand\NonStaticPhpJwt\Jwt;
  8. class JwtTest extends TestCase
  9. {
  10. /**
  11. * @var Jwt
  12. */
  13. private $jwt;
  14. protected function setUp()
  15. {
  16. parent::setUp();
  17. $this->jwt = $this->prophesize(Jwt::class);
  18. }
  19. public function getSut(): Dummy
  20. {
  21. return new Dummy($this->jwt->reveal());
  22. }
  23. public function testJwtEncodeCanBeProphecised(): void
  24. {
  25. $returnValue = 'string';
  26. $this->jwt->encode([], '')->shouldBeCalled()->willReturn($returnValue);
  27. $this->assertInstanceOf(ObjectProphecy::class, $this->jwt);
  28. $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('encode')[0]);
  29. $this->assertEquals($returnValue, $this->getSut()->encode());
  30. }
  31. public function testJwtDecodeCanBeProphecised(): void
  32. {
  33. $returnValue = new \stdClass();
  34. $this->jwt->decode('', '')->shouldBeCalled()->willReturn($returnValue);
  35. $this->assertInstanceOf(ObjectProphecy::class, $this->jwt);
  36. $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('decode')[0]);
  37. $this->assertEquals($returnValue, $this->getSut()->decode());
  38. }
  39. public function testJwtSignCanBeProphecised(): void
  40. {
  41. $returnValue = 'string';
  42. $this->jwt->sign('', '')->shouldBeCalled()->willReturn($returnValue);
  43. $this->assertInstanceOf(ObjectProphecy::class, $this->jwt);
  44. $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('sign')[0]);
  45. $this->assertEquals($returnValue, $this->getSut()->sign());
  46. }
  47. }