Added Auth Bearer to SnappyMail\HTTP\Request

This commit is contained in:
the-djmaze 2024-02-18 16:29:25 +01:00
parent 785bde153e
commit 86f084e5a9
3 changed files with 21 additions and 10 deletions

View file

@ -9,7 +9,8 @@ abstract class Request
* Authentication
*/
AUTH_BASIC = 1,
AUTH_DIGEST = 2;
AUTH_DIGEST = 2,
AUTH_BEARER = 3;
public
$timeout = 5, // timeout in seconds.

View file

@ -51,15 +51,20 @@ class CURL extends \SnappyMail\HTTP\Request
\curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
}
if ($this->auth['user'] && $this->auth['type']) {
$auth = 0;
if ($this->auth['type'] & self::AUTH_BASIC) {
$auth |= CURLAUTH_BASIC;
if ($this->auth['type'] & self::AUTH_BEARER ) {
\curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
\curl_setopt($c, CURLOPT_XOAUTH2_BEARER, $this->auth['pass']);
} else {
$auth = 0;
if ($this->auth['type'] & self::AUTH_BASIC) {
$auth |= CURLAUTH_BASIC;
}
if ($this->auth['type'] & self::AUTH_DIGEST) {
$auth |= CURLAUTH_DIGEST;
}
\curl_setopt($c, CURLOPT_HTTPAUTH, $auth);
\curl_setopt($c, CURLOPT_USERPWD, $this->auth['user'] . ':' . $this->auth['pass']);
}
if ($this->auth['type'] & self::AUTH_DIGEST) {
$auth |= CURLAUTH_DIGEST;
}
\curl_setopt($c, CURLOPT_HTTPAUTH, $auth);
\curl_setopt($c, CURLOPT_USERPWD, $this->auth['user'] . ':' . $this->auth['pass']);
}
if ($this->proxy) {
\curl_setopt($c, CURLOPT_PROXY, $this->proxy);

View file

@ -105,7 +105,7 @@ class Socket extends \SnappyMail\HTTP\Request
return $this->__doRequest($method, $request_url, $body, $extra_headers);
}
// Digest authentication
else if ($this->auth['type'] & self::AUTH_DIGEST && \preg_match("/WWW-Authenticate:\\s+Digest\\s+([^\\r\\n]*)/i", $data, $match)) {
if ($this->auth['type'] & self::AUTH_DIGEST && \preg_match("/WWW-Authenticate:\\s+Digest\\s+([^\\r\\n]*)/i", $data, $match)) {
$challenge = [];
foreach (\split(',', $match[1]) as $i) {
$ii = \split('=', \trim($i), 2);
@ -137,6 +137,11 @@ class Socket extends \SnappyMail\HTTP\Request
\fclose($sock);
return $this->__doRequest($method, $request_url, $body, $extra_headers);
}
if ($this->auth['type'] & self::AUTH_BEARER) {
$extra_headers['Authorization'] = "Authorization: Bearer {$this->auth['pass']}";
\fclose($sock);
return $this->__doRequest($method, $request_url, $body, $extra_headers);
}
}
$data = \rtrim(\fgets($sock, 1024)); # read next line