]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/HTTP/DigestAuthTest.php
Initial Release of the calendar plugin
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / HTTP / DigestAuthTest.php
1 <?php
2
3 require_once 'Sabre/HTTP/ResponseMock.php';
4
5 class Sabre_HTTP_DigestAuthTest extends PHPUnit_Framework_TestCase {
6
7     /**
8      * @var Sabre_HTTP_ResponseMock
9      */
10     private $response;
11     /**
12      * @var Sabre_HTTP_DigestAuth
13      */
14     private $auth;
15
16     const REALM = 'SabreDAV unittest';
17
18     public function setUp() {
19
20         $this->response = new Sabre_HTTP_ResponseMock();
21         $this->auth = new Sabre_HTTP_DigestAuth();
22         $this->auth->setRealm(self::REALM);
23         $this->auth->setHTTPResponse($this->response);
24
25     }
26
27     public function testDigest() {
28
29         list($nonce,$opaque) = $this->getServerTokens();
30
31         $username = 'admin';
32         $password = 12345;
33         $nc = '00002';
34         $cnonce = uniqid();
35
36         $digestHash = md5(
37             md5($username . ':' . self::REALM . ':' . $password) . ':' .
38             $nonce . ':' .
39             $nc . ':' .
40             $cnonce . ':' .
41             'auth:' .
42             md5('GET' . ':' . '/')
43         );
44
45         $request = new Sabre_HTTP_Request(array(
46             'REQUEST_METHOD' => 'GET',
47             'PHP_AUTH_DIGEST' => 'username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
48         ));
49
50         $this->auth->setHTTPRequest($request);
51         $this->auth->init();
52
53         $this->assertEquals($username,$this->auth->getUserName());
54         $this->assertEquals(self::REALM,$this->auth->getRealm());
55         $this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
56         $this->assertTrue($this->auth->validatePassword($password),'Authentication is deemed invalid through validatePassword');
57
58     }
59
60     public function testDigestCGIFormat() {
61
62         list($nonce,$opaque) = $this->getServerTokens();
63
64         $username = 'admin';
65         $password = 12345;
66         $nc = '00002';
67         $cnonce = uniqid();
68
69         $digestHash = md5(
70             md5($username . ':' . self::REALM . ':' . $password) . ':' .
71             $nonce . ':' .
72             $nc . ':' .
73             $cnonce . ':' .
74             'auth:' .
75             md5('GET' . ':' . '/')
76         );
77
78         $request = new Sabre_HTTP_Request(array(
79             'REQUEST_METHOD' => 'GET',
80             'HTTP_AUTHORIZATION' => 'Digest username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
81         ));
82
83         $this->auth->setHTTPRequest($request);
84         $this->auth->init();
85
86         $this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
87         $this->assertTrue($this->auth->validatePassword($password),'Authentication is deemed invalid through validatePassword');
88
89     }
90
91     public function testDigestApacheEdgeCase() {
92
93         list($nonce,$opaque) = $this->getServerTokens();
94
95         $username = 'admin';
96         $password = 12345;
97         $nc = '00002';
98         $cnonce = uniqid();
99
100         $digestHash = md5(
101             md5($username . ':' . self::REALM . ':' . $password) . ':' .
102             $nonce . ':' .
103             $nc . ':' .
104             $cnonce . ':' .
105             'auth:' .
106             md5('GET' . ':' . '/')
107         );
108
109         $request = new Sabre_HTTP_Request(array(
110             'REQUEST_METHOD' => 'GET',
111             'REDIRECT_HTTP_AUTHORIZATION' => 'Digest username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
112         ));
113
114         $this->auth->setHTTPRequest($request);
115         $this->auth->init();
116
117         $this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
118         $this->assertTrue($this->auth->validatePassword($password),'Authentication is deemed invalid through validatePassword');
119
120     }
121
122     public function testInvalidDigest() {
123
124         list($nonce,$opaque) = $this->getServerTokens();
125
126         $username = 'admin';
127         $password = 12345;
128         $nc = '00002';
129         $cnonce = uniqid();
130
131         $digestHash = md5(
132             md5($username . ':' . self::REALM . ':' . $password) . ':' .
133             $nonce . ':' .
134             $nc . ':' .
135             $cnonce . ':' .
136             'auth:' .
137             md5('GET' . ':' . '/')
138         );
139
140         $request = new Sabre_HTTP_Request(array(
141             'REQUEST_METHOD' => 'GET',
142             'PHP_AUTH_DIGEST' => 'username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
143         ));
144
145         $this->auth->setHTTPRequest($request);
146         $this->auth->init();
147
148         $this->assertFalse($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . ($password . 'randomness'))),'Authentication is deemed invalid through validateA1');
149
150     }
151
152     public function testInvalidDigest2() {
153
154         $request = new Sabre_HTTP_Request(array(
155             'REQUEST_METHOD' => 'GET',
156             'HTTP_AUTHORIZATION' => 'basic blablabla',
157         ));
158
159         $this->auth->setHTTPRequest($request);
160         $this->auth->init();
161
162         $this->assertFalse($this->auth->validateA1(md5('user:realm:password')));
163
164     }
165
166
167     public function testDigestAuthInt() {
168
169         $this->auth->setQOP(Sabre_HTTP_DigestAuth::QOP_AUTHINT | Sabre_HTTP_DigestAuth::QOP_AUTH);
170         list($nonce,$opaque) = $this->getServerTokens(Sabre_HTTP_DigestAuth::QOP_AUTHINT| Sabre_HTTP_DigestAuth::QOP_AUTH);
171
172         $username = 'admin';
173         $password = 12345;
174         $nc = '00003';
175         $cnonce = uniqid();
176
177         $digestHash = md5(
178             md5($username . ':' . self::REALM . ':' . $password) . ':' .
179             $nonce . ':' .
180             $nc . ':' .
181             $cnonce . ':' .
182             'auth-int:' .
183             md5('POST' . ':' . '/' . ':' . md5('body'))
184         );
185
186         $request = new Sabre_HTTP_Request(array(
187             'REQUEST_METHOD' => 'POST',
188             'PHP_AUTH_DIGEST' => 'username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth-int,nc='.$nc.',cnonce="' . $cnonce . '"',
189         ));
190         $request->setBody('body');
191
192         $this->auth->setHTTPRequest($request);
193
194         $this->auth->init();
195
196         $this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
197
198     }
199
200     private function getServerTokens($qop = Sabre_HTTP_DigestAuth::QOP_AUTH) {
201
202         $this->auth->requireLogin();
203
204         switch($qop) {
205             case Sabre_HTTP_DigestAuth::QOP_AUTH    : $qopstr='auth'; break;
206             case Sabre_HTTP_DigestAuth::QOP_AUTHINT : $qopstr='auth-int'; break;
207             default                                 : $qopstr='auth,auth-int'; break;
208         }
209
210         $test = preg_match('/Digest realm="'.self::REALM.'",qop="'.$qopstr.'",nonce="([0-9a-f]*)",opaque="([0-9a-f]*)"/',
211             $this->response->headers['WWW-Authenticate'],$matches);
212
213         $this->assertTrue($test==true,'The WWW-Authenticate response didn\'t match our pattern. We received: ' . $this->response->headers['WWW-Authenticate']);
214
215         $nonce = $matches[1];
216         $opaque = $matches[2];
217
218         // Reset our environment
219         $this->setUp();
220         $this->auth->setQOP($qop);
221
222         return array($nonce,$opaque);
223
224     }
225
226 }