| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * SPDX-License-Identifier: AGPL-3.0-only | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * SPDX-FileCopyrightText: Copyright 2025 grommunio GmbH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Unit tests for Token class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use PHPUnit\Framework\TestCase; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @coversNothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class TokenTest extends TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	private function createValidJWT(): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		$header = base64_encode(json_encode(['alg' => 'HS256', 'typ' => 'JWT'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		$payload = base64_encode(json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 			'sub' => '1234567890', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			'name' => 'Test User', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 			'exp' => time() + 3600, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			'iat' => time(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$signature = base64_encode('test-signature'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		return "{$header}.{$payload}.{$signature}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	public function testTokenConstructorWithValidJWT(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$jwt = $this->createValidJWT(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		$token = new Token($jwt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$this->assertIsArray($token->token_header); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$this->assertIsArray($token->token_payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$this->assertIsString($token->signed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	public function testTokenConstructorWithInvalidJWT(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$token = new Token('invalid.token'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		// Should have default payload with expires_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$this->assertIsArray($token->token_payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$this->assertArrayHasKey('expires_at', $token->token_payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$this->assertEquals(0, $token->token_payload['expires_at']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public function testTokenConstructorWithMalformedJWT(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		// Token with only 2 parts instead of 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$token = new Token('part1.part2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		// Should gracefully handle and set default payload | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$this->assertIsArray($token->token_payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	public function testGetSignature(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$jwt = $this->createValidJWT(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$token = new Token($jwt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$signature = $token->get_signature(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$this->assertNotNull($signature); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 	public function testGetSigned(): void { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 		$jwt = $this->createValidJWT(); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		$token = new Token($jwt); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		$signed = $token->get_signed(); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 		$this->assertIsString($signed); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		$this->assertStringContainsString('.', $signed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	public function testGetPayload(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$jwt = $this->createValidJWT(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$token = new Token($jwt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$payload = $token->get_payload(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$this->assertEquals($jwt, $payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	public function testGetClaims(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$jwt = $this->createValidJWT(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$token = new Token($jwt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$name = $token->get_claims('name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$this->assertEquals('Test User', $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		// Non-existent claim should return empty string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$nonExistent = $token->get_claims('non_existent'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$this->assertEquals('', $nonExistent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	public function testIsExpiredWithValidToken(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$jwt = $this->createValidJWT(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$token = new Token($jwt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$this->assertFalse($token->is_expired()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	public function testIsExpiredWithExpiredToken(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$header = base64_encode(json_encode(['alg' => 'HS256', 'typ' => 'JWT'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$payload = base64_encode(json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			'sub' => '1234567890', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			'exp' => time() - 3600, // Expired 1 hour ago | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			'iat' => time() - 7200, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$signature = base64_encode('test-signature'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$jwt = "{$header}.{$payload}.{$signature}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$token = new Token($jwt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		$this->assertTrue($token->is_expired()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths