Example #1
0
 /**
  * Parse digest authentication header and return parsed key / value pairs.
  *
  * @param string $digest
  * @param HttpRequest $request
  * @return array<string, string>
  */
 protected function parseDigest($digest, HttpRequest $request)
 {
     $required = ['nonce' => true, 'username' => true, 'response' => true, 'cnonce' => true, 'nc' => true];
     $tmp = [];
     $data = [];
     @preg_match_all("'(\\w+)\\s*=\\s*(?:(?:\"((?:(?<!\\\\)[^\"])+)\")|([^,]+),)'U", $digest, $tmp, PREG_SET_ORDER);
     foreach ($tmp as $m) {
         $data[$m[1]] = array_key_exists(3, $m) ? trim(trim($m[3]), '"') : trim(trim($m[2]), '"');
         unset($required[$m[1]]);
     }
     if (!empty($data['username'])) {
         if (false !== strpos($data['username'], '\\\\')) {
             $tmp = explode('\\\\', $data['username']);
             $username = trim(array_pop($tmp), '"');
             $domain = trim(implode('\\\\', $tmp), '"');
             if ($domain === $request->getHost()) {
                 $data['username'] = $username;
                 $data['msdomain'] = $domain;
             }
         }
     }
     return count($required) > 0 ? [] : $data;
 }