/** * Parses and returns the request's `Authorization` header. * * This method extracts the request's `Authorization` header and returns an * array with the following elements: * * - `#scheme` - the authentication scheme, e.g. Basic, Bearer * - `#credentials` - the credentials following the scheme * * If `$parse_credentials` is true, the method will also attempt to parse * the credential information. For the `Basic` scheme, the user name and * password will be returned in the array as `#username` and `#password` * respectively. For other schemes with delimited name-value parameters, * those name-value pairs will be returned. * * @param bool $parse_credentials whether to parse the credential information * @return array the parsed `Authorization` header, or `null` if none * exists */ public function getAuthorizationHeader($parse_credentials = false) { if (!$this->hasHeader('Authorization')) { return null; } $results = array(); $header = $this->getHeader('Authorization'); list($scheme, $credentials) = preg_split('/\\s+/', $header, 2); $results['#scheme'] = HTTPResponse::httpCase($scheme); $results['#credentials'] = $credentials; if ($parse_credentials) { if ($results['#scheme'] == 'Basic') { list($username, $password) = explode(':', base64_decode($credentials)); $results['#username'] = $username; $results['#password'] = $password; } else { $matches = array(); preg_match_all('/([-a-zA-Z]+)=\\"([^\\"]+)\\"/', $credentials, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $results[$match[1]] = $match[2]; } } } return $results; }