* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Http\Tests\Firewall; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Http\Firewall\DigestData; /** * @group legacy */ class DigestDataTest extends TestCase { public function testGetResponse() { $digestAuth = new DigestData( 'username="user", realm="Welcome, robot!", '. 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $this->assertEquals('b52938fc9e6d7c01be7702ece9031b42', $digestAuth->getResponse()); } public function testGetUsername() { $digestAuth = new DigestData( 'username="user", realm="Welcome, robot!", '. 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $this->assertEquals('user', $digestAuth->getUsername()); } public function testGetUsernameWithQuote() { $digestAuth = new DigestData( 'username="\"user\"", realm="Welcome, robot!", '. 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $this->assertEquals('"user"', $digestAuth->getUsername()); } public function testGetUsernameWithQuoteAndEscape() { $digestAuth = new DigestData( 'username="\"u\\\\\"ser\"", realm="Welcome, robot!", '. 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $this->assertEquals('"u\\"ser"', $digestAuth->getUsername()); } public function testGetUsernameWithSingleQuote() { $digestAuth = new DigestData( 'username="\"u\'ser\"", realm="Welcome, robot!", '. 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $this->assertEquals('"u\'ser"', $digestAuth->getUsername()); } public function testGetUsernameWithSingleQuoteAndEscape() { $digestAuth = new DigestData( 'username="\"u\\\'ser\"", realm="Welcome, robot!", '. 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $this->assertEquals('"u\\\'ser"', $digestAuth->getUsername()); } public function testGetUsernameWithEscape() { $digestAuth = new DigestData( 'username="\"u\\ser\"", realm="Welcome, robot!", '. 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $this->assertEquals('"u\\ser"', $digestAuth->getUsername()); } /** * @group time-sensitive */ public function testValidateAndDecode() { $time = microtime(true); $key = 'ThisIsAKey'; $nonce = base64_encode($time.':'.md5($time.':'.$key)); $digestAuth = new DigestData( 'username="user", realm="Welcome, robot!", nonce="'.$nonce.'", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $digestAuth->validateAndDecode($key, 'Welcome, robot!'); sleep(1); $this->assertTrue($digestAuth->isNonceExpired()); } public function testCalculateServerDigest() { $this->calculateServerDigest('user', 'Welcome, robot!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5'); } public function testCalculateServerDigestWithQuote() { $this->calculateServerDigest('\"user\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5'); } public function testCalculateServerDigestWithQuoteAndEscape() { $this->calculateServerDigest('\"u\\\\\"ser\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5'); } public function testCalculateServerDigestEscape() { $this->calculateServerDigest('\"u\\ser\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5'); $this->calculateServerDigest('\"u\\ser\\\\\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5'); } public function testIsNonceExpired() { $time = microtime(true) + 10; $key = 'ThisIsAKey'; $nonce = base64_encode($time.':'.md5($time.':'.$key)); $digestAuth = new DigestData( 'username="user", realm="Welcome, robot!", nonce="'.$nonce.'", '. 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", '. 'response="b52938fc9e6d7c01be7702ece9031b42"' ); $digestAuth->validateAndDecode($key, 'Welcome, robot!'); $this->assertFalse($digestAuth->isNonceExpired()); } protected function setUp() { class_exists('Symfony\Component\Security\Http\Firewall\DigestAuthenticationListener', true); } private function calculateServerDigest($username, $realm, $password, $key, $nc, $cnonce, $qop, $method, $uri) { $time = microtime(true); $nonce = base64_encode($time.':'.md5($time.':'.$key)); $response = md5( md5($username.':'.$realm.':'.$password).':'.$nonce.':'.$nc.':'.$cnonce.':'.$qop.':'.md5($method.':'.$uri) ); $digest = sprintf('username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc=%s, qop="%s", response="%s"', $username, $realm, $nonce, $uri, $cnonce, $nc, $qop, $response ); $digestAuth = new DigestData($digest); $this->assertEquals($digestAuth->getResponse(), $digestAuth->calculateServerDigest($password, $method)); } } __halt_compiler();----SIGNATURE:----F9idO10Um37qAx5zYbyj12XKK9X6chWIZ4yx2a1gtzzt2oIOwJi7Tnsp3tEoV/9zGKhCqQl/tBg15VAwF5O62E5qQiL1tg4CvNSGZ+cXLR3dKd5XCUQaOZaj5fW6Vvdxv6vUu4uR8GtI9hDxnQvGjCGAka0AMkKtNGUy7ZGkENMQFkScuJdyyhKGm8Adggpnfe1NHEj9y614JWcuYFHgvHdl+CYyqpdWuehrGjdGA0ZKMfMk0Eb0jheB2OpWB/XiFFgX/BblbTiwo0e4l4pQpoY7+tJkxfG6tEtleGbSRWaQHVnd7dh9MqA3ILl0D2IGvKdFSkDH2Gf3s/A+e4UdYl15jrVnrttL82gpL1YCNRAsZrfNNWxTiA7hwGSd9L6AddzrR3F2Ismpv+lqgqMPZAp8c8syeOxhWSkLr3pxuEczkzF67mxK3heOYiHpugbY0wwbRoGp9letKJjeve9aSrmwaevUiNJW0Hwl+3WlIwyK77h+2B05EYNOgCy7Q85u7VFHgWD+DOkspfwfBFNJQJzTvsac5hBc7z/1fgNJL2OWZmbrUiIRkdV/JTW47bzpXH9AWZO5+lmIlpBxeKHp+LFQHEO7FXg4MaSFYoCX+sFp/A3VGPEKCzKwgC7GDX5IIWO/uqk0ylXq9juufrBwa6lGZmIyctN9khvtoj4d4NY=----ATTACHMENT:----OTM5Mzk3NDY0Nzg4NjMwIDYxNjgwMjgwMzU1Nzc0NTkgMjkzNDk2OTgyMDU2NzYwMQ==