From d5c60ac815473217265f19c8124ba79b8865f956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dagef=C3=B6rde?= Date: Wed, 16 Aug 2017 21:04:36 +0200 Subject: [PATCH] MDL-59844 webdav: Add bearer auth method to WebDAV requests --- lib/webdavlib.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/webdavlib.php b/lib/webdavlib.php index 12441f254a8..f71c61ab762 100644 --- a/lib/webdavlib.php +++ b/lib/webdavlib.php @@ -16,6 +16,7 @@ // along with Moodle. If not, see . /** + * A Moodle-modified WebDAV client, based on * webdav_client v0.1.5, a php based webdav client class. * class webdav client. a php based nearly RFC 2518 conforming client. * @@ -26,11 +27,13 @@ * Please notice that all Filenames coming from or going to the webdav server should be UTF-8 encoded (see RFC 2518). * This class tries to convert all you filenames into utf-8 when it's needed. * + * Moodle modifications: + * * Moodle 3.4: Add support for OAuth 2 bearer token-based authentication + * * @package moodlecore * @author Christian Juerges , Xwave GmbH, Josefstr. 92, 8005 Zuerich - Switzerland * @copyright (C) 2003/2004, Christian Juerges * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License - * @version 0.1.5 */ class webdav_client { @@ -79,12 +82,24 @@ class webdav_client { private $_cnonce = ''; private $_nc = 0; + /** + * OAuth token used for bearer auth. + * @var string + */ + private $oauthtoken; + /**#@-*/ /** * Constructor - Initialise class variables + * @param string $server Hostname of the server to connect to + * @param string $user Username (for basic/digest auth, see $auth) + * @param string $pass Password (for basic/digest auth, see $auth) + * @param bool $auth Authentication type; one of ['basic', 'digest', 'bearer'] + * @param string $socket Used protocol for fsockopen, usually: '' (empty) or 'ssl://' + * @param string $oauthtoken OAuth 2 bearer token (for bearer auth, see $auth) */ - function __construct($server = '', $user = '', $pass = '', $auth = false, $socket = '') { + public function __construct($server = '', $user = '', $pass = '', $auth = false, $socket = '', $oauthtoken = '') { if (!empty($server)) { $this->_server = $server; } @@ -94,6 +109,9 @@ class webdav_client { } $this->_auth = $auth; $this->_socket = $socket; + if ($auth == 'bearer') { + $this->oauthtoken = $oauthtoken; + } } public function __set($key, $value) { $property = '_' . $key; @@ -1323,6 +1341,8 @@ EOD; if ($signature = $this->digest_signature($method)){ $this->header_add($signature); } + } else if ($this->_auth == 'bearer') { + $this->header_add(sprintf('Authorization: Bearer %s', $this->oauthtoken)); } }