Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ
2022-04-15 08:15:32 +02:00
parent 98768cfb57
commit 751dc7ea2a
15 changed files with 185 additions and 194 deletions

View File

@@ -8,19 +8,15 @@ use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Notification\IManager;
use OCP\User\Events;
class Application extends App implements IBootstrap {
public function __construct() {
parent::__construct('user_external');
}
public function __construct() {
parent::__construct('user_external');
}
public function register(IRegistrationContext $context): void {
}
public function register(IRegistrationContext $context): void {
}
public function boot(IBootContext $context): void {
}
}
public function boot(IBootContext $context): void {
}
}

View File

@@ -21,7 +21,7 @@ namespace OCA\UserExternal;
* @license http://www.gnu.org/licenses/agpl AGPL
* @link http://github.com/owncloud/apps
*/
abstract class Base extends \OC\User\Backend{
abstract class Base extends \OC\User\Backend {
protected $backend = '';
/**
@@ -80,7 +80,6 @@ abstract class Base extends \OC\User\Backend{
* @return array with all displayNames (value) and the corresponding uids (key)
*/
public function getDisplayNames($search = '', $limit = null, $offset = null) {
$connection = \OC::$server->getDatabaseConnection();
$query = $connection->getQueryBuilder();
$query->select('uid', 'displayname')
@@ -106,10 +105,10 @@ abstract class Base extends \OC\User\Backend{
}
/**
* Get a list of all users
*
* @return array with all uids
*/
* Get a list of all users
*
* @return array with all uids
*/
public function getUsers($search = '', $limit = null, $offset = null) {
$connection = \OC::$server->getDatabaseConnection();
$query = $connection->getQueryBuilder();
@@ -231,5 +230,4 @@ abstract class Base extends \OC\User\Backend{
return $users > 0;
}
}

View File

@@ -9,12 +9,11 @@
namespace OCA\UserExternal;
class BasicAuth extends Base {
private $authUrl;
public function __construct($authUrl) {
parent::__construct($authUrl);
$this->authUrl =$authUrl;
$this->authUrl = $authUrl;
}
/**
@@ -31,13 +30,13 @@ class BasicAuth extends Base {
* URL is indeed authenticating or not...
*/
$context = stream_context_create(array(
'http' => array(
'method' => "GET",
'follow_location' => 0
))
'http' => array(
'method' => "GET",
'follow_location' => 0
))
);
$canary = get_headers($this->authUrl, 1, $context);
if(!$canary) {
if (!$canary) {
\OC::$server->getLogger()->error(
'ERROR: Not possible to connect to BasicAuth Url: '.$this->authUrl,
['app' => 'user_external']
@@ -53,17 +52,17 @@ class BasicAuth extends Base {
}
$context = stream_context_create(array(
'http' => array(
'method' => "GET",
'header' => "authorization: Basic " . base64_encode("$uid:$password"),
'follow_location' => 0
))
'http' => array(
'method' => "GET",
'header' => "authorization: Basic " . base64_encode("$uid:$password"),
'follow_location' => 0
))
);
$headers = get_headers($this->authUrl, 1, $context);
if(!$headers) {
if (!$headers) {
\OC::$server->getLogger()->error(
'ERROR: Not possible to connect to BasicAuth Url: '.$this->authUrl,
'ERROR: Not possible to connect to BasicAuth Url: '.$this->authUrl,
['app' => 'user_external']
);
return false;
@@ -84,7 +83,7 @@ class BasicAuth extends Base {
return $uid;
case "3":
\OC::$server->getLogger()->error(
'ERROR: Too many redirects from BasicAuth Url: '.$this->authUrl,
'ERROR: Too many redirects from BasicAuth Url: '.$this->authUrl,
['app' => 'user_external']
);
return false;

View File

@@ -17,7 +17,7 @@ namespace OCA\UserExternal;
* @license http://www.gnu.org/licenses/agpl AGPL
* @link http://github.com/owncloud/apps
*/
class FTP extends Base{
class FTP extends Base {
private $host;
private $secure;
private $protocol;
@@ -28,12 +28,12 @@ class FTP extends Base{
* @param string $host Hostname or IP of FTP server
* @param boolean $secure TRUE to enable SSL
*/
public function __construct($host,$secure=false) {
$this->host=$host;
$this->secure=$secure;
$this->protocol='ftp';
if($this->secure) {
$this->protocol.='s';
public function __construct($host, $secure = false) {
$this->host = $host;
$this->secure = $secure;
$this->protocol = 'ftp';
if ($this->secure) {
$this->protocol .= 's';
}
parent::__construct($this->protocol . '://' . $this->host);
}
@@ -56,11 +56,11 @@ class FTP extends Base{
}
// opendir handles the as %-encoded string, but this is not true for usernames and passwords, encode them before passing them
$url = sprintf('%s://%s:%s@%s/', $this->protocol, urlencode($uid), urlencode($password), $this->host);
$result=@opendir($url);
if(is_resource($result)) {
$result = @opendir($url);
if (is_resource($result)) {
$this->storeUser($uid);
return $uid;
}else{
} else {
return false;
}
}

View File

@@ -9,7 +9,6 @@
*/
namespace OCA\UserExternal;
/**
* User authentication against an IMAP mail server
*
@@ -59,14 +58,14 @@ class IMAP extends Base {
// Replace escaped @ symbol in uid (which is a mail address)
// but only if there is no @ symbol and if there is a %40 inside the uid
if (!(strpos($uid, '@') !== false) && (strpos($uid, '%40') !== false)) {
$uid = str_replace("%40","@",$uid);
$uid = str_replace("%40", "@", $uid);
}
$pieces = explode('@', $uid);
if ($this->domain !== '') {
if (count($pieces) === 1) {
$username = $uid . '@' . $this->domain;
} else if(count($pieces) === 2 && $pieces[1] === $this->domain) {
} elseif (count($pieces) === 2 && $pieces[1] === $this->domain) {
$username = $uid;
if ($this->stripeDomain) {
$uid = $pieces[0];
@@ -80,11 +79,11 @@ class IMAP extends Base {
}
} else {
$username = $uid;
}
}
$groups = [];
if ($this->groupDomain && $pieces[1]) {
$groups[] = $pieces[1];
$groups[] = $pieces[1];
}
$protocol = ($this->sslmode === "ssl") ? "imaps" : "imap";
@@ -101,7 +100,7 @@ class IMAP extends Base {
$canconnect = curl_exec($ch);
if($canconnect) {
if ($canconnect) {
curl_close($ch);
$uid = mb_strtolower($uid);
$this->storeUser($uid, $groups);

View File

@@ -16,11 +16,11 @@ namespace OCA\UserExternal;
* @license http://www.gnu.org/licenses/agpl AGPL
* @link http://github.com/owncloud/apps
*/
class SMB extends Base{
class SMB extends Base {
private $host;
const SMBCLIENT = 'smbclient -L';
const LOGINERROR = 'NT_STATUS_LOGON_FAILURE';
public const SMBCLIENT = 'smbclient -L';
public const LOGINERROR = 'NT_STATUS_LOGON_FAILURE';
/**
* Create new samba authentication provider
@@ -29,7 +29,7 @@ class SMB extends Base{
*/
public function __construct($host) {
parent::__construct($host);
$this->host=$host;
$this->host = $host;
}
/**
@@ -48,13 +48,13 @@ class SMB extends Base{
['app' => 'user_external']
);
return false;
} else if (strpos($lastline, self::LOGINERROR) !== false) {
} elseif (strpos($lastline, self::LOGINERROR) !== false) {
//normal login error
return false;
} else if (strpos($lastline, 'NT_STATUS_BAD_NETWORK_NAME') !== false) {
} elseif (strpos($lastline, 'NT_STATUS_BAD_NETWORK_NAME') !== false) {
//login on minor error
goto login;
} else if ($retval !== 0) {
} elseif ($retval !== 0) {
//some other error
\OC::$server->getLogger()->error(
'ERROR: smbclient error: ' . trim($lastline),
@@ -78,13 +78,13 @@ class SMB extends Base{
public function checkPassword($uid, $password) {
// Check with an invalid password, if the user authenticates then fail
$attemptWithInvalidPassword = $this->tryAuthentication($uid, base64_encode($password));
if(is_string($attemptWithInvalidPassword)) {
if (is_string($attemptWithInvalidPassword)) {
return false;
}
// Check with valid password
$attemptWithValidPassword = $this->tryAuthentication($uid, $password);
if(is_string($attemptWithValidPassword)) {
if (is_string($attemptWithValidPassword)) {
$this->storeUser($uid);
return $uid;
}

View File

@@ -23,10 +23,10 @@ class SSH extends Base {
private $port;
/**
* Create a new SSH authentication provider
*
* @param string $host Hostname or IP address of SSH servr
*/
* Create a new SSH authentication provider
*
* @param string $host Hostname or IP address of SSH servr
*/
public function __construct($host, $port = 22) {
parent::__construct($host);
$this->host = $host;
@@ -34,14 +34,14 @@ class SSH extends Base {
}
/**
* Check if the password is correct without logging in
* Requires the php-ssh2 pecl extension
*
* @param string $uid The username
* @param string $password The password
*
* @return true/false
*/
* Check if the password is correct without logging in
* Requires the php-ssh2 pecl extension
*
* @param string $uid The username
* @param string $password The password
*
* @return true/false
*/
public function checkPassword($uid, $password) {
if (!extension_loaded('ssh2')) {
\OC::$server->getLogger()->error(

View File

@@ -9,12 +9,11 @@
namespace OCA\UserExternal;
class WebDavAuth extends Base {
private $webDavAuthUrl;
public function __construct($webDavAuthUrl) {
parent::__construct($webDavAuthUrl);
$this->webDavAuthUrl =$webDavAuthUrl;
$this->webDavAuthUrl = $webDavAuthUrl;
}
/**
@@ -27,21 +26,20 @@ class WebDavAuth extends Base {
*/
public function checkPassword($uid, $password) {
$arr = explode('://', $this->webDavAuthUrl, 2);
if( ! isset($arr) OR count($arr) !== 2) {
if (! isset($arr) or count($arr) !== 2) {
\OC::$server->getLogger()->error('ERROR: Invalid WebdavUrl: "'.$this->webDavAuthUrl.'" ', ['app' => 'user_external']);
return false;
}
list($protocol, $path) = $arr;
$url= $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path;
$url = $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path;
$headers = get_headers($url);
if($headers === false) {
if ($headers === false) {
\OC::$server->getLogger()->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']);
return false;
}
$returnCode= substr($headers[0], 9, 3);
$returnCode = substr($headers[0], 9, 3);
if(substr($returnCode, 0, 1) === '2') {
if (substr($returnCode, 0, 1) === '2') {
$this->storeUser($uid);
return $uid;
} else {

View File

@@ -34,89 +34,89 @@ class XMPP extends Base {
}
public function hmacSha1($key, $data) {
if (strlen($key) > 64) {
$key = str_pad(sha1($key, true), 64, chr(0));
if (strlen($key) > 64) {
$key = str_pad(sha1($key, true), 64, chr(0));
}
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
$oPad = str_repeat(chr(0x5C), 64);
$iPad = str_repeat(chr(0x36), 64);
$oPad = str_repeat(chr(0x5C), 64);
$iPad = str_repeat(chr(0x36), 64);
for ($i = 0; $i < strlen($key); $i++) {
$oPad[$i] = $oPad[$i] ^ $key[$i];
$iPad[$i] = $iPad[$i] ^ $key[$i];
}
return sha1($oPad.sha1($iPad.$data, true));
}
for ($i = 0; $i < strlen($key); $i++) {
$oPad[$i] = $oPad[$i] ^ $key[$i];
$iPad[$i] = $iPad[$i] ^ $key[$i];
}
return sha1($oPad.sha1($iPad.$data, true));
}
public function validateHashedPassword($user, $uid, $submittedPassword){
foreach ($user as $key){
if($key[3] === "salt") {
$internalSalt = $key['value'];
}
if($key[3] === "server_key") {
$internalServerKey = $key['value'];
}
if($key[3] === "stored_key") {
$internalStoredKey = $key['value'];
}
}
unset($user);
$internalIteration = '4096';
$newSaltedPassword = hash_pbkdf2('sha1', $submittedPassword, $internalSalt, $internalIteration, 0, true);
$newServerKey = $this->hmacSha1($newSaltedPassword, 'Server Key');
$newClientKey = $this->hmacSha1($newSaltedPassword, 'Client Key');
$newStoredKey = sha1(hex2bin($newClientKey));
public function validateHashedPassword($user, $uid, $submittedPassword) {
foreach ($user as $key) {
if ($key[3] === "salt") {
$internalSalt = $key['value'];
}
if ($key[3] === "server_key") {
$internalServerKey = $key['value'];
}
if ($key[3] === "stored_key") {
$internalStoredKey = $key['value'];
}
}
unset($user);
$internalIteration = '4096';
$newSaltedPassword = hash_pbkdf2('sha1', $submittedPassword, $internalSalt, $internalIteration, 0, true);
$newServerKey = $this->hmacSha1($newSaltedPassword, 'Server Key');
$newClientKey = $this->hmacSha1($newSaltedPassword, 'Client Key');
$newStoredKey = sha1(hex2bin($newClientKey));
if ($newServerKey === $internalServerKey
&& $newStoredKey === $internalStoredKey) {
$uid = mb_strtolower($uid);
$this->storeUser($uid);
return $uid;
} else {
return false;
}
if ($newServerKey === $internalServerKey
&& $newStoredKey === $internalStoredKey) {
$uid = mb_strtolower($uid);
$this->storeUser($uid);
return $uid;
} else {
return false;
}
}
public function validatePlainPassword($user, $uid, $submittedPassword) {
foreach ($user as $key) {
if($key[3] === "password") {
$internalPlainPassword = $key['value'];
}
}
foreach ($user as $key) {
if ($key[3] === "password") {
$internalPlainPassword = $key['value'];
}
}
unset($user);
if ($submittedPassword === $internalPlainPassword) {
$uid = mb_strtolower($uid);
$this->storeUser($uid);
return $uid;
} else {
return false;
}
if ($submittedPassword === $internalPlainPassword) {
$uid = mb_strtolower($uid);
$this->storeUser($uid);
return $uid;
} else {
return false;
}
}
public function checkPassword($uid, $password){
public function checkPassword($uid, $password) {
$pdo = new \PDO("mysql:host=$this->host;dbname=$this->xmppDb", $this->xmppDbUser, $this->xmppDbPassword);
if(isset($uid)
if (isset($uid)
&& isset($password)) {
if(!filter_var($uid, FILTER_VALIDATE_EMAIL)
|| !strpos($uid, $this->xmppDomain)
if (!filter_var($uid, FILTER_VALIDATE_EMAIL)
|| !strpos($uid, $this->xmppDomain)
|| substr($uid, -strlen($this->xmppDomain)) !== $this->xmppDomain
) {
return false;
}
$user = explode("@", $uid);
$userName = strtolower($user[0]);
$submittedPassword = $password;
$statement = $pdo->prepare("SELECT * FROM prosody WHERE user = :user AND host = :xmppDomain AND store = 'accounts'");
$result = $statement->execute(array(
'user' => $userName,
$userName = strtolower($user[0]);
$submittedPassword = $password;
$statement = $pdo->prepare("SELECT * FROM prosody WHERE user = :user AND host = :xmppDomain AND store = 'accounts'");
$result = $statement->execute(array(
'user' => $userName,
'xmppDomain' => $this->xmppDomain
));
$user = $statement->fetchAll();
if(empty($user)) {
return false;
$user = $statement->fetchAll();
if (empty($user)) {
return false;
}
if ($this->passwordHashed === true) {

View File

@@ -16,20 +16,20 @@ class Test_User_BasicAuth extends \Test\TestCase {
return include(__DIR__.'/config.php');
}
function skip() {
$config=$this->getConfig();
public function skip() {
$config = $this->getConfig();
$this->skipUnless($config['basic_auth']['run']);
}
protected function setUp() {
parent::setUp();
$config=$this->getConfig();
$this->instance=new OC_User_BasicAuth($config['basic_auth']['url']);
$config = $this->getConfig();
$this->instance = new OC_User_BasicAuth($config['basic_auth']['url']);
}
function testLogin() {
$config=$this->getConfig();
$this->assertEquals($config['basic_auth']['user'],$this->instance->checkPassword($config['basic_auth']['user'],$config['basic_auth']['password']));
$this->assertFalse($this->instance->checkPassword($config['basic_auth']['user'],$config['basic_auth']['password'].'foo'));
public function testLogin() {
$config = $this->getConfig();
$this->assertEquals($config['basic_auth']['user'], $this->instance->checkPassword($config['basic_auth']['user'], $config['basic_auth']['password']));
$this->assertFalse($this->instance->checkPassword($config['basic_auth']['user'], $config['basic_auth']['password'].'foo'));
}
}

View File

@@ -1,4 +1,5 @@
<?php
if (!defined('PHPUNIT_RUN')) {
define('PHPUNIT_RUN', 1);
}
@@ -9,7 +10,7 @@ if (!class_exists('\PHPUnit\Framework\TestCase')) {
\OC_App::loadApp('user_external');
$dummyClass = \OC::$SERVERROOT . '/tests/lib/Util/User/Dummy.php';
if(file_exists($dummyClass)) {
if (file_exists($dummyClass)) {
require_once($dummyClass);
}
OC_Hook::clear();

View File

@@ -8,28 +8,28 @@
OC_App::loadApp('user_external');
return array(
'imap'=>array(
'run'=>false,
'mailbox'=>'{imap.gmail.com:993/imap/ssl}INBOX', //see http://php.net/manual/en/function.imap-open.php
'user'=>'foo',//valid username/password combination
'password'=>'bar',
'imap' => array(
'run' => false,
'mailbox' => '{imap.gmail.com:993/imap/ssl}INBOX', //see http://php.net/manual/en/function.imap-open.php
'user' => 'foo',//valid username/password combination
'password' => 'bar',
),
'smb'=>array(
'run'=>false,
'host'=>'localhost',
'user'=>'test',//valid username/password combination
'password'=>'test',
'smb' => array(
'run' => false,
'host' => 'localhost',
'user' => 'test',//valid username/password combination
'password' => 'test',
),
'ftp'=>array(
'run'=>false,
'host'=>'localhost',
'user'=>'test',//valid username/password combination
'password'=>'test',
'ftp' => array(
'run' => false,
'host' => 'localhost',
'user' => 'test',//valid username/password combination
'password' => 'test',
),
'basic_auth'=>array(
'run'=>false,
'url'=>'localhost/basic_auth',
'user'=>'test',//valid username/password combination
'password'=>'test',
'basic_auth' => array(
'run' => false,
'url' => 'localhost/basic_auth',
'user' => 'test',//valid username/password combination
'password' => 'test',
),
);

View File

@@ -16,20 +16,20 @@ class Test_User_FTP extends \Test\TestCase {
return include(__DIR__.'/config.php');
}
function skip() {
$config=$this->getConfig();
public function skip() {
$config = $this->getConfig();
$this->skipUnless($config['ftp']['run']);
}
protected function setUp() {
parent::setUp();
$config=$this->getConfig();
$this->instance=new OC_User_FTP($config['ftp']['host']);
$config = $this->getConfig();
$this->instance = new OC_User_FTP($config['ftp']['host']);
}
function testLogin() {
$config=$this->getConfig();
$this->assertEquals($config['ftp']['user'],$this->instance->checkPassword($config['ftp']['user'],$config['ftp']['password']));
$this->assertFalse($this->instance->checkPassword($config['ftp']['user'],$config['ftp']['password'].'foo'));
public function testLogin() {
$config = $this->getConfig();
$this->assertEquals($config['ftp']['user'], $this->instance->checkPassword($config['ftp']['user'], $config['ftp']['password']));
$this->assertFalse($this->instance->checkPassword($config['ftp']['user'], $config['ftp']['password'].'foo'));
}
}

View File

@@ -16,21 +16,21 @@ class Test_User_Imap extends \Test\TestCase {
return include(__DIR__.'/config.php');
}
function skip() {
$config=$this->getConfig();
public function skip() {
$config = $this->getConfig();
$this->skipUnless($config['imap']['run']);
}
protected function setUp() {
parent::setUp();
$config=$this->getConfig();
$this->instance=new OC_User_IMAP($config['imap']['mailbox']);
$config = $this->getConfig();
$this->instance = new OC_User_IMAP($config['imap']['mailbox']);
}
function testLogin() {
$config=$this->getConfig();
$this->assertEquals($config['imap']['user'],$this->instance->checkPassword($config['imap']['user'],$config['imap']['password']));
$this->assertFalse($this->instance->checkPassword($config['imap']['user'],$config['imap']['password'].'foo'));
public function testLogin() {
$config = $this->getConfig();
$this->assertEquals($config['imap']['user'], $this->instance->checkPassword($config['imap']['user'], $config['imap']['password']));
$this->assertFalse($this->instance->checkPassword($config['imap']['user'], $config['imap']['password'].'foo'));
}
}

View File

@@ -16,21 +16,21 @@ class Test_User_SMB extends \Test\TestCase {
return include(__DIR__.'/config.php');
}
function skip() {
$config=$this->getConfig();
public function skip() {
$config = $this->getConfig();
$this->skipUnless($config['smb']['run']);
}
protected function setUp() {
parent::setUp();
$config=$this->getConfig();
$this->instance=new OC_User_SMB($config['smb']['host']);
$config = $this->getConfig();
$this->instance = new OC_User_SMB($config['smb']['host']);
}
function testLogin() {
$config=$this->getConfig();
$this->assertEquals($config['smb']['user'],$this->instance->checkPassword($config['smb']['user'],$config['smb']['password']));
$this->assertFalse($this->instance->checkPassword($config['smb']['user'],$config['smb']['password'].'foo'));
public function testLogin() {
$config = $this->getConfig();
$this->assertEquals($config['smb']['user'], $this->instance->checkPassword($config['smb']['user'], $config['smb']['password']));
$this->assertFalse($this->instance->checkPassword($config['smb']['user'], $config['smb']['password'].'foo'));
}
}