Skip to content

Commit

Permalink
Merge 3f3c06f into e33744f
Browse files Browse the repository at this point in the history
  • Loading branch information
Standfestgit committed Dec 17, 2013
2 parents e33744f + 3f3c06f commit 2a25fac
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
11 changes: 11 additions & 0 deletions library/Requests.php
Expand Up @@ -266,6 +266,9 @@ public static function patch($url, $headers, $data = array(), $options = array()
* (string|boolean, default: false)
* - `auth`: Authentication handler or array of user/password details to use
* for Basic authentication
* (Requests_Auth|array|boolean, default: false)*
* - `auth_digest`: Authentication handler or array of user/password details to use
* for Digest authentication*
* (Requests_Auth|array|boolean, default: false)
* - `proxy`: Proxy details to use for proxy by-passing and authentication
* (Requests_Proxy|array|boolean, default: false)
Expand Down Expand Up @@ -451,6 +454,7 @@ protected static function get_default_options($multirequest = false) {
'type' => self::GET,
'filename' => false,
'auth' => false,
'auth_digest' => false,
'proxy' => false,
'cookies' => false,
'idn' => true,
Expand Down Expand Up @@ -491,6 +495,13 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio
$options['auth']->register($options['hooks']);
}

if (is_array($options['auth_digest'])) {
$options['auth_digest'] = new Requests_Auth_Digest($options['auth_digest']);
}
if ($options['auth_digest'] !== false) {
$options['auth_digest']->register($options['hooks']);
}

if (!empty($options['proxy'])) {
$options['proxy'] = new Requests_Proxy_HTTP($options['proxy']);
}
Expand Down
82 changes: 82 additions & 0 deletions library/Requests/Auth/Digest.php
@@ -0,0 +1,82 @@
<?php

/**
* Digest Authentication provider
*
* Provides a handler for Basic HTTP authentication via the Authorization
* header.
*
* @package Requests
* @subpackage Authentication
*/
class Requests_Auth_Digest implements Requests_Auth {
/**
* Username
*
* @var string
*/
public $user;

/**
* Password
*
* @var string
*/
public $pass;

/**
* Constructor
*
* @throws Requests_Exception On incorrect number of arguments (`authdigestbadargs`)
* @param array|null $args Array of user and password. Must have exactly two elements
*/
public function __construct($args = null) {
if (is_array($args)) {
if (count($args) !== 2) {
throw new Requests_Exception('Invalid number of arguments', 'authdigestbadargs');
}

list($this->user, $this->pass) = $args;
}
}

/**
* Register the necessary callbacks
*
* @see curl_before_send
* @see fsockopen_header
* @param Requests_Hooks $hooks Hook system
*/
public function register(Requests_Hooks &$hooks) {
$hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
$hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
}

/**
* Set cURL parameters before the data is sent
*
* @param resource $handle cURL resource
*/
public function curl_before_send(&$handle) {
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString());
}

/**
* Add extra headers to the request before sending
*
* @param string $out HTTP header string
*/
public function fsockopen_header(&$out) {
$out .= "Authorization: Digest " . base64_encode($this->getAuthString()) . "\r\n";
}

/**
* Get the authentication string (user:pass)
*
* @return string
*/
public function getAuthString() {
return $this->user . ':' . $this->pass;
}
}

0 comments on commit 2a25fac

Please sign in to comment.