| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Gandung\JWT\Tests\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Gandung\JWT\JWTFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Gandung\JWT\Validator\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Gandung\JWT\ValidatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Paulus Gandung Prakosa <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class ValidatorTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->token = $this->buildJWTToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function testIsAnInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->assertInstanceOf(Validator::class, new Validator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function testCanNormallyValidateCurrentToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\Algorithm); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\Type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\ContentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\IssuedBy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\ExpirationTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $isTokenValid = $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertInternalType('boolean', $isTokenValid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->assertTrue($isTokenValid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @expectedException \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function testCanRaiseExceptionWhenValidatingInvalidToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $invalidToken = explode('.', $this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         unset($invalidToken[2]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $invalidToken = join('.', array_values($invalidToken)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\Algorithm); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\Type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\ContentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\IssuedBy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\ExpirationTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $isTokenValid = $validator->validate($invalidToken); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoJWKSetUrlInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\JWKSetUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoJsonWebKeyInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\JsonWebKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoKeyIDInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\KeyID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoX509CertChainInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\X509CertificateChain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoX509UrlInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Jose\X509Url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoAudienceInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\IntendedFor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoIssuedAtInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\IssuedAt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoJWTIDInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\JWTID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoNotBeforeInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\NotBefore); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @expectedException \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     public function testCanRaiseExceptionWhenValidatingTokenWithNoRelatedToInIt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $validator = new Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $validator->addConstraint(new \Gandung\JWT\Validator\Constraints\Payload\RelatedTo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $validator->validate($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |     private function buildJWTToken() | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |         $key = JWTFactory::getKeyManager(); | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |         $key->setContentFromCertFile('cert/dummy256.pem'); | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |         $key->setPassphrase('umar123'); | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |         $jose = JWTFactory::getJoseBuilder() | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |             ->algorithm(\Gandung\JWT\Token\Algorithm::RS256) | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |             ->type('JWT') | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |             ->contentType('application/json'); | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |         $claim = JWTFactory::getClaimBuilder() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |             ->issuedBy('Paulus Gandung Prakosa') | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |             ->expireAt(new \DateTimeImmutable('@' . (time() + (3600 * 3)))); | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |         $payload = JWTFactory::getPayloadBuilder() | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |             ->claim($claim) | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |             ->userData([ | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |                 'credentials' => [ | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |                     'username' => 'gandung', | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |                     'password' => 'whatever_i_like' | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |                 ] | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |             ]); | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |         $jwt = JWTFactory::getJwt(); | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |         $token = $jwt->createToken($jose, $payload, $key); | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 184 |  |  |         return $token; | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 186 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 187 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.