1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \Security \Tests \Http \Firewall ;
13
+
14
+ use Symfony \Component \Security \Http \Firewall \DigestData ;
15
+
16
+ class DigestDataTest extends \PHPUnit_Framework_TestCase
17
+ {
18
+ public function setUp ()
19
+ {
20
+ class_exists ('Symfony\Component\Security\Http\Firewall\DigestAuthenticationListener ' , true );
21
+ }
22
+
23
+ public function testGetResponse ()
24
+ {
25
+ $ digestAuth = new DigestData (
26
+ 'username="user", realm="Welcome, robot!", ' .
27
+ 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", ' .
28
+ 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", ' .
29
+ 'response="b52938fc9e6d7c01be7702ece9031b42" '
30
+ );
31
+
32
+ $ this ->assertEquals ('b52938fc9e6d7c01be7702ece9031b42 ' , $ digestAuth ->getResponse ());
33
+ }
34
+
35
+ public function testGetUsername ()
36
+ {
37
+ $ digestAuth = new DigestData (
38
+ 'username="user", realm="Welcome, robot!", ' .
39
+ 'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", ' .
40
+ 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", ' .
41
+ 'response="b52938fc9e6d7c01be7702ece9031b42" '
42
+ );
43
+
44
+ $ this ->assertEquals ('user ' , $ digestAuth ->getUsername ());
45
+ }
46
+
47
+ public function testValidateAndDecode ()
48
+ {
49
+ $ time = microtime (true );
50
+ $ key = 'ThisIsAKey ' ;
51
+ $ nonce = base64_encode ($ time . ': ' . md5 ($ time . ': ' . $ key ));
52
+
53
+ $ digestAuth = new DigestData (
54
+ 'username="user", realm="Welcome, robot!", nonce=" ' . $ nonce . '", ' .
55
+ 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", ' .
56
+ 'response="b52938fc9e6d7c01be7702ece9031b42" '
57
+ );
58
+
59
+ try {
60
+ $ digestAuth ->validateAndDecode ($ key , 'Welcome, robot! ' );
61
+ } catch (\Exception $ e ) {
62
+ $ this ->fail (sprintf ('testValidateAndDecode fail with message: %s ' , $ e ->getMessage ()));
63
+ }
64
+ }
65
+
66
+ public function testCalculateServerDigest ()
67
+ {
68
+ $ username = 'user ' ;
69
+ $ realm = 'Welcome, robot! ' ;
70
+ $ password = 'pass,word=password ' ;
71
+ $ time = microtime (true );
72
+ $ key = 'ThisIsAKey ' ;
73
+ $ nonce = base64_encode ($ time . ': ' . md5 ($ time . ': ' . $ key ));
74
+ $ nc = '00000001 ' ;
75
+ $ cnonce = 'MDIwODkz ' ;
76
+ $ qop = 'auth ' ;
77
+ $ method = 'GET ' ;
78
+ $ uri = '/path/info?p1=5&p2=5 ' ;
79
+
80
+ $ response = md5 (
81
+ md5 ($ username . ': ' . $ realm . ': ' . $ password ) .
82
+ ': ' . $ nonce . ': ' . $ nc . ': ' . $ cnonce . ': ' . $ qop . ': ' . md5 ($ method . ': ' . $ uri )
83
+ );
84
+
85
+ $ digest = sprintf ('username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc="%s", qop="%s", response="%s" ' ,
86
+ $ username , $ realm , $ nonce , $ uri , $ cnonce , $ nc , $ qop , $ response
87
+ );
88
+
89
+ $ digestAuth = new DigestData ($ digest );
90
+
91
+ $ this ->assertEquals ($ digestAuth ->getResponse (), $ digestAuth ->calculateServerDigest ($ password , $ method ));
92
+ }
93
+
94
+ public function testIsNonceExpired ()
95
+ {
96
+ $ time = microtime (true ) + 10 ;
97
+ $ key = 'ThisIsAKey ' ;
98
+ $ nonce = base64_encode ($ time . ': ' . md5 ($ time . ': ' . $ key ));
99
+
100
+ $ digestAuth = new DigestData (
101
+ 'username="user", realm="Welcome, robot!", nonce=" ' . $ nonce . '", ' .
102
+ 'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", ' .
103
+ 'response="b52938fc9e6d7c01be7702ece9031b42" '
104
+ );
105
+
106
+ $ digestAuth ->validateAndDecode ($ key , 'Welcome, robot! ' );
107
+
108
+ $ this ->assertFalse ($ digestAuth ->isNonceExpired ());
109
+ }
110
+ }
0 commit comments