UnsignedTokenTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
  4. *
  5. * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
  6. */
  7. namespace Lcobucci\JWT\FunctionalTests;
  8. use Lcobucci\JWT\Builder;
  9. use Lcobucci\JWT\Parser;
  10. use Lcobucci\JWT\Token;
  11. use Lcobucci\JWT\ValidationData;
  12. /**
  13. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  14. * @since 2.1.0
  15. */
  16. class UnsignedTokenTest extends \PHPUnit_Framework_TestCase
  17. {
  18. const CURRENT_TIME = 100000;
  19. /**
  20. * @test
  21. *
  22. * @covers Lcobucci\JWT\Builder
  23. * @covers Lcobucci\JWT\Token
  24. * @covers Lcobucci\JWT\Claim\Factory
  25. * @covers Lcobucci\JWT\Claim\Basic
  26. * @covers Lcobucci\JWT\Parsing\Encoder
  27. */
  28. public function builderCanGenerateAToken()
  29. {
  30. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  31. $token = (new Builder())->setId(1)
  32. ->setAudience('http://client.abc.com')
  33. ->setIssuer('http://api.abc.com')
  34. ->setExpiration(self::CURRENT_TIME + 3000)
  35. ->set('user', $user)
  36. ->getToken();
  37. $this->assertAttributeEquals(null, 'signature', $token);
  38. $this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
  39. $this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
  40. $this->assertEquals(self::CURRENT_TIME + 3000, $token->getClaim('exp'));
  41. $this->assertEquals($user, $token->getClaim('user'));
  42. return $token;
  43. }
  44. /**
  45. * @test
  46. *
  47. * @depends builderCanGenerateAToken
  48. *
  49. * @covers Lcobucci\JWT\Builder
  50. * @covers Lcobucci\JWT\Parser
  51. * @covers Lcobucci\JWT\Token
  52. * @covers Lcobucci\JWT\Claim\Factory
  53. * @covers Lcobucci\JWT\Claim\Basic
  54. * @covers Lcobucci\JWT\Parsing\Encoder
  55. * @covers Lcobucci\JWT\Parsing\Decoder
  56. */
  57. public function parserCanReadAToken(Token $generated)
  58. {
  59. $read = (new Parser())->parse((string) $generated);
  60. $this->assertEquals($generated, $read);
  61. $this->assertEquals('testing', $read->getClaim('user')->name);
  62. }
  63. /**
  64. * @test
  65. *
  66. * @depends builderCanGenerateAToken
  67. *
  68. * @covers Lcobucci\JWT\Builder
  69. * @covers Lcobucci\JWT\Parser
  70. * @covers Lcobucci\JWT\Token
  71. * @covers Lcobucci\JWT\ValidationData
  72. * @covers Lcobucci\JWT\Claim\Factory
  73. * @covers Lcobucci\JWT\Claim\Basic
  74. * @covers Lcobucci\JWT\Claim\EqualsTo
  75. * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo
  76. * @covers Lcobucci\JWT\Parsing\Encoder
  77. * @covers Lcobucci\JWT\Parsing\Decoder
  78. */
  79. public function tokenValidationShouldReturnWhenEverythingIsFine(Token $generated)
  80. {
  81. $data = new ValidationData(self::CURRENT_TIME - 10);
  82. $data->setAudience('http://client.abc.com');
  83. $data->setIssuer('http://api.abc.com');
  84. $this->assertTrue($generated->validate($data));
  85. }
  86. /**
  87. * @test
  88. *
  89. * @dataProvider invalidValidationData
  90. *
  91. * @depends builderCanGenerateAToken
  92. *
  93. * @covers Lcobucci\JWT\Builder
  94. * @covers Lcobucci\JWT\Parser
  95. * @covers Lcobucci\JWT\Token
  96. * @covers Lcobucci\JWT\ValidationData
  97. * @covers Lcobucci\JWT\Claim\Factory
  98. * @covers Lcobucci\JWT\Claim\Basic
  99. * @covers Lcobucci\JWT\Claim\EqualsTo
  100. * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo
  101. * @covers Lcobucci\JWT\Parsing\Encoder
  102. * @covers Lcobucci\JWT\Parsing\Decoder
  103. */
  104. public function tokenValidationShouldReturnFalseWhenExpectedDataDontMatch(ValidationData $data, Token $generated)
  105. {
  106. $this->assertFalse($generated->validate($data));
  107. }
  108. public function invalidValidationData()
  109. {
  110. $expired = new ValidationData(self::CURRENT_TIME + 3020);
  111. $expired->setAudience('http://client.abc.com');
  112. $expired->setIssuer('http://api.abc.com');
  113. $invalidAudience = new ValidationData(self::CURRENT_TIME - 10);
  114. $invalidAudience->setAudience('http://cclient.abc.com');
  115. $invalidAudience->setIssuer('http://api.abc.com');
  116. $invalidIssuer = new ValidationData(self::CURRENT_TIME - 10);
  117. $invalidIssuer->setAudience('http://client.abc.com');
  118. $invalidIssuer->setIssuer('http://aapi.abc.com');
  119. return [[$expired], [$invalidAudience], [$invalidIssuer]];
  120. }
  121. }