Browse Source

Add more logging to aid debugging

master
Chris Smith 13 years ago
parent
commit
0a0b480845
3 changed files with 23 additions and 0 deletions
  1. 14
    0
      keymanager.inc.php
  2. 7
    0
      processor.php
  3. 2
    0
      urlbuilder.inc.php

+ 14
- 0
keymanager.inc.php View File

43
    */
43
    */
44
   private static function loadData() {
44
   private static function loadData() {
45
    if (self::$data == null) {
45
    if (self::$data == null) {
46
+    Logger::log('Loading data from %s/keycache.php', dirname(__FILE__));
46
     $data = file(dirname(__FILE__) . '/keycache.php');
47
     $data = file(dirname(__FILE__) . '/keycache.php');
47
     self::$header = array_shift($data);
48
     self::$header = array_shift($data);
48
     self::$data = unserialize(implode("\n", $data));
49
     self::$data = unserialize(implode("\n", $data));
53
    * Saves the KeyManager's data array to disk.
54
    * Saves the KeyManager's data array to disk.
54
    */
55
    */
55
   private static function saveData() {
56
   private static function saveData() {
57
+   Logger::log('Saving data to %s/keycache.php', dirname(__FILE__));
56
    file_put_contents(dirname(__FILE__) . '/keycache.php', self::$header . serialize(self::$data));
58
    file_put_contents(dirname(__FILE__) . '/keycache.php', self::$header . serialize(self::$data));
57
   }
59
   }
58
 
60
 
164
    self::loadData();
166
    self::loadData();
165
 
167
 
166
    if (!isset(self::$data[$server])) {
168
    if (!isset(self::$data[$server])) {
169
+    Logger::log('No data found for %s', $server);
167
     return null;
170
     return null;
168
    }
171
    }
169
 
172
 
171
     if ($handle == '__private') { continue; }
174
     if ($handle == '__private') { continue; }
172
 
175
 
173
     if ($data['expires_at'] < time()) {
176
     if ($data['expires_at'] < time()) {
177
+     Logger::log('Handle for %s expired at %s, unsetting', $server, $data['expires_at']);
174
      unset(self::$data[$server][$handle]);
178
      unset(self::$data[$server][$handle]);
175
     } else {
179
     } else {
176
      return $handle;
180
      return $handle;
224
    * @return True if the message was authenticated, false if it's a fake
228
    * @return True if the message was authenticated, false if it's a fake
225
    */
229
    */
226
   public static function authenticate($server, $args) {
230
   public static function authenticate($server, $args) {
231
+   Logger::log('Authenticating message from %s', $server);
232
+
227
    $data = self::getData($server, $args['openid_assoc_handle']);
233
    $data = self::getData($server, $args['openid_assoc_handle']);
228
 
234
 
229
    if ($data === null) {
235
    if ($data === null) {
236
+    Logger::log('Can\'t authenticate, no key available');
230
     throw new Exception('No key available for that server/handle');
237
     throw new Exception('No key available for that server/handle');
231
    }
238
    }
232
 
239
 
244
      $algo = 'sha256';
251
      $algo = 'sha256';
245
      break;
252
      break;
246
     default:
253
     default:
254
+     Logger::log('Can\'t authenticate, unknown assoc type %s', $data['assoc_type']);
247
      throw new Exception('Unable to handle association type ' . $data['assoc_type']);
255
      throw new Exception('Unable to handle association type ' . $data['assoc_type']);
248
    }
256
    }
249
 
257
 
250
    $sig = base64_encode(hash_hmac($algo, $contents, base64_decode($data['mac_key']), true));
258
    $sig = base64_encode(hash_hmac($algo, $contents, base64_decode($data['mac_key']), true));
259
+   Logger::log('Expected signature %s, received signature %s', $sig, $args['openid_sig']);
251
 
260
 
252
    // Manually compare characters to prevent timing attacks
261
    // Manually compare characters to prevent timing attacks
253
    $res = strlen($sig) == strlen($args['openid_sig']);
262
    $res = strlen($sig) == strlen($args['openid_sig']);
254
    for ($i = 0; $i < strlen($sig); $i++) {
263
    for ($i = 0; $i < strlen($sig); $i++) {
255
     $res &= $sig[$i] == $args['openid_sig'][$i];
264
     $res &= $sig[$i] == $args['openid_sig'][$i];
256
    }
265
    }
266
+
267
+   Logger::log('Authentication result: %s', $res ? 'good' : 'bad');
257
    return $res;
268
    return $res;
258
   }
269
   }
259
 
270
 
264
    * @return True if the request has been authenticated, false otherwise.
275
    * @return True if the request has been authenticated, false otherwise.
265
    */
276
    */
266
   public static function dumbAuthenticate() {
277
   public static function dumbAuthenticate() {
278
+   Logger::log('Performing dumb authentication');
279
+
267
    $url = URLBuilder::buildAuth($_REQUEST, $_SESSION['openid']['version']);
280
    $url = URLBuilder::buildAuth($_REQUEST, $_SESSION['openid']['version']);
268
 
281
 
269
    try {
282
    try {
279
     }
292
     }
280
    }
293
    }
281
 
294
 
295
+   Logger::log('Authentication result: %s', $valid ? 'good' : 'bad');
282
    return $valid;
296
    return $valid;
283
   }
297
   }
284
 
298
 

+ 7
- 0
processor.php View File

173
     error('notvalid', 'Claimed identity is not a valid identifier');
173
     error('notvalid', 'Claimed identity is not a valid identifier');
174
    }
174
    }
175
 
175
 
176
+   Logger::log('Finished discovery on %s', $url);
177
+
176
    return $disc;
178
    return $disc;
177
   } catch (Exception $e) {
179
   } catch (Exception $e) {
178
    Logger::log('Error during discovery on %s: %s', $url, $e->getMessage());
180
    Logger::log('Error during discovery on %s: %s', $url, $e->getMessage());
190
   * @return The association handle of the server or null on failure
192
   * @return The association handle of the server or null on failure
191
   */
193
   */
192
  function getHandle($server) {
194
  function getHandle($server) {
195
+  Logger::log('Attempting to find a handle for %s', $server);
196
+
193
   if (KEYMANAGER) {
197
   if (KEYMANAGER) {
198
+   Logger::log('Using the key manager...');
194
    if (!KeyManager::hasHandle($server)) {
199
    if (!KeyManager::hasHandle($server)) {
200
+    Logger::log('Key manager has no handle, trying to associate with %s', $server);
195
     KeyManager::associate($server);
201
     KeyManager::associate($server);
196
    }
202
    }
197
 
203
 
198
    return KeyManager::getHandle($server);
204
    return KeyManager::getHandle($server);
199
   } else {
205
   } else {
206
+   Logger::log('Not using key manager...');
200
    return null;
207
    return null;
201
   }
208
   }
202
  }
209
  }

+ 2
- 0
urlbuilder.inc.php View File

22
  * SOFTWARE.
22
  * SOFTWARE.
23
  */
23
  */
24
 
24
 
25
+ require_once(dirname(__FILE__) . '/logging.inc.php');
25
  require_once(dirname(__FILE__) . '/keymanager.inc.php');
26
  require_once(dirname(__FILE__) . '/keymanager.inc.php');
26
 
27
 
27
  class URLBuilder {
28
  class URLBuilder {
246
    * @param $url The URL to redirect the user to
247
    * @param $url The URL to redirect the user to
247
    */
248
    */
248
   public static function doRedirect($url) {
249
   public static function doRedirect($url) {
250
+   Logger::log('Redirecting to %s', $url);
249
    header('Location: ' . $url);
251
    header('Location: ' . $url);
250
    echo '<html><head><title>Redirecting</title></head><body>';
252
    echo '<html><head><title>Redirecting</title></head><body>';
251
    echo '<p>Redirecting to <a href="', htmlentities($url), '">';
253
    echo '<p>Redirecting to <a href="', htmlentities($url), '">';

Loading…
Cancel
Save