From 86f084e5a9bfacc2bcb6ededb1d504833189f7a5 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Sun, 18 Feb 2024 16:29:25 +0100 Subject: [PATCH] Added Auth Bearer to SnappyMail\HTTP\Request --- .../app/libraries/snappymail/http/request.php | 3 ++- .../snappymail/http/request/curl.php | 21 ++++++++++++------- .../snappymail/http/request/socket.php | 7 ++++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php b/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php index ffbddeea3..8b4a7e684 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php @@ -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. diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/http/request/curl.php b/snappymail/v/0.0.0/app/libraries/snappymail/http/request/curl.php index 37dad6cfa..2185d0c8f 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/http/request/curl.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/http/request/curl.php @@ -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); diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php b/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php index 80629023d..8b6ffc975 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php @@ -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