Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option for digest authentication for webdav. #227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ Add the following to your `config.php`:

'user_backends' => array(
array(
'class' => '\OCA\UserExternal\WebDAVAuth',
'arguments' => array('https://example.com/webdav'),
'class' => '\OCA\UserExternal\WebDavAuth',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: I also fixed the typo in the capitalization of WebDavAuth here.

'arguments' => array(
'https://example.com/webdav',
'basic', // alternative: 'digest'
),
),
),

Expand Down
74 changes: 68 additions & 6 deletions lib/WebDavAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
class WebDavAuth extends Base {
private $webDavAuthUrl;

public function __construct($webDavAuthUrl) {
parent::__construct($webDavAuthUrl);
public function __construct($webDavAuthUrl, $authType = 'basic') {
$this->webDavAuthUrl = $webDavAuthUrl;
$this->authType = $authType;
}

/**
Expand All @@ -31,12 +31,74 @@ public function checkPassword($uid, $password) {
return false;
}
list($protocol, $path) = $arr;
$url = $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path;
$headers = get_headers($url);
if ($headers === false) {
\OC::$server->getLogger()->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']);
$url = $protocol.'://'.$path;

switch ($this->authType) {
case 'digest':
// Initial unauthenticated request
@file_get_contents($url);
if (!isset($http_response_header)) {
\OC::$server->getLogger()->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']);
return false;
}

// Find the WWW-Authenticate header
foreach ($http_response_header as $header) {
if (strpos($header, 'WWW-Authenticate: Digest') === 0) {
$auth_header = substr($header, strlen('WWW-Authenticate: Digest '));
break;
}
}

// Parse the header to get the parameters
$auth_params = array();
foreach (explode(',', $auth_header) as $param) {
list($key, $value) = explode('=', $param, 2);
$auth_params[trim($key)] = trim($value, ' "');
}

// Generate a cnonce (client nonce) value
$cnonce = bin2hex(openssl_random_pseudo_bytes(8));

// Generate the response value
$A1 = md5($uid . ':' . $auth_params['realm'] . ':' . $password);
$A2 = md5('GET:' . $url);
$response = md5($A1 . ':' . $auth_params['nonce'] . ':00000001:' . $cnonce . ':auth:' . $A2);

// Construct the Authorization header
$auth_header = 'Authorization: Digest username="' . $uid . '", realm="' . $auth_params['realm'] .
'", nonce="' . $auth_params['nonce'] . '", uri="' . $url . '", cnonce="' . $cnonce .
'", nc=00000001, qop=auth, response="' . $response . '", opaque="' . $auth_params['opaque'] . '"';

// Make the authenticated request
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => $auth_header
)
));
break;
case 'basic':
$url = $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path;
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => 'Authorization: Basic ' . base64_encode($uid . ':' . $password)
)
));
break;
default:
\OC::$server->getLogger()->error('ERROR: Invalid authentication type: "'.$this->authType.'". Expected "basic" or "digest".', ['app' => 'user_external']);
return false;
}

$result = @file_get_contents($url, false, $context);
if ($result === false) {
\OC::$server->getLogger()->error('ERROR: Not possible to connect to WebDAV Url: "'.$url.'" ', ['app' => 'user_external']);
return false;
}

$headers = $http_response_header;
$returnCode = substr($headers[0], 9, 3);

if (substr($returnCode, 0, 1) === '2') {
Expand Down