| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Support\Facades\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WWON\JwtGuard\Contract\TokenManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use WWON\JwtGuard\JwtService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class JwtServiceTest extends PHPUnit_Framework_TestCase | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var Mockery\MockInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $tokenManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var JwtService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $jwtService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * setUp method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         Config::shouldReceive('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->with('jwt.secret') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->andReturn('abcdefg'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         Config::shouldReceive('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ->with('jwt.leeway') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ->andReturn(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->tokenManager = Mockery::mock(TokenManager::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->jwtService = new JwtService($this->tokenManager); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * tearDown method | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     public function tearDown() | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         unset($this->jwtService); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         parent::tearDown(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * testGetTokenForUser method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function testGetTokenForUser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $now = Carbon::now()->timestamp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $token = $this->getToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $items = explode('.', $token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $claimBody = json_decode(base64_decode($items[1])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->assertEquals('http://www.test.com', $claimBody->iss); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertEquals($now, $claimBody->iat); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertEquals($now + 6000, $claimBody->exp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertEquals($now + 6000, $claimBody->nat); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * testGetUserIdFromToken method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 73 |  | View Code Duplication |     public function testGetUserIdFromToken() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $token = $this->getToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->tokenManager->shouldReceive('check') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ->with(Mockery::on(function($claim) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 return $claim->sub == 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     && $claim->iss == 'http://www.test.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             })) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             ->andReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $userId = $this->jwtService->getUserIdFromToken($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertEquals(5, $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * testGetUserIdFromTokenWithInvalidClaim method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 93 |  | View Code Duplication |     public function testGetUserIdFromTokenWithInvalidClaim() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $token = $this->getToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->tokenManager->shouldReceive('check') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->with(Mockery::on(function($claim) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 return $claim->sub == 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 && $claim->iss == 'http://www.test.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             })) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->andReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->setExpectedException(\WWON\JwtGuard\Exceptions\InvalidTokenException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $userId = $this->jwtService->getUserIdFromToken($token); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * getToken method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param int $ttl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function getToken($ttl = 100) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         Config::shouldReceive('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             ->with('app.url') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ->andReturn('http://www.test.com'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         Config::shouldReceive('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             ->with('jwt.ttl') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             ->andReturn($ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         Config::shouldReceive('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ->with('jwt.ttl') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             ->andReturn($ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         Config::shouldReceive('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             ->with('jwt.leeway') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             ->andReturn(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         Config::shouldReceive('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             ->with('jwt.algo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             ->andReturn('HS256'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->tokenManager->shouldReceive('add') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             ->with(Mockery::on(function($claim) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 return $claim->sub == 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 && $claim->iss == 'http://www.test.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $token = $this->jwtService->getTokenForUser(new User()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 155 |  |  | } | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.