Skip to content

Commit

Permalink
allow using any ldap property as login name when using external stora…
Browse files Browse the repository at this point in the history
…ge login credentials

Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Mar 31, 2021
1 parent 91ae7f2 commit 0ca0287
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions apps/files_external/lib/Lib/Auth/Password/LoginCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Listener\StorePasswordListener;
use OCA\User_LDAP\IUserLDAP;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
use OCP\LDAP\ILDAPProviderFactory;
use OCP\Security\ICredentialsManager;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserLoggedInEvent;
Expand All @@ -55,10 +57,20 @@ class LoginCredentials extends AuthMechanism {
/** @var CredentialsStore */
private $credentialsStore;

public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) {
private $ldapFactory;

public function __construct(
IL10N $l,
ISession $session,
ICredentialsManager $credentialsManager,
CredentialsStore $credentialsStore,
IEventDispatcher $eventDispatcher,
ILDAPProviderFactory $ldapFactory
) {
$this->session = $session;
$this->credentialsManager = $credentialsManager;
$this->credentialsStore = $credentialsStore;
$this->ldapFactory = $ldapFactory;

$this
->setIdentifier('password::logincredentials')
Expand Down Expand Up @@ -86,7 +98,7 @@ private function getCredentials(IUser $user): array {

$credentials = [
'user' => $sessionCredentials->getLoginName(),
'password' => $sessionCredentials->getPassword()
'password' => $sessionCredentials->getPassword(),
];

$this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
Expand All @@ -104,7 +116,25 @@ public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = n
}
$credentials = $this->getCredentials($user);

$storage->setBackendOption('user', $credentials['user']);
$loginKey = $storage->getBackendOption("login_ldap_attr");
if ($loginKey) {
$backend = $user->getBackend();
if ($backend instanceof IUserLDAP) {
$value = $this->getLdapPropertyForUser($user, $loginKey);
if ($value === null) {
throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user ' . $user->getUID());
}
$storage->setBackendOption('user', $value);
} else {
throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user ' . $user->getUID() . ' is not an ldap user');
}
} else {
$storage->setBackendOption('user', $credentials['user']);
}
$storage->setBackendOption('password', $credentials['password']);
}

private function getLdapPropertyForUser(IUser $user, string $property): ?string {
return $this->ldapFactory->getLDAPProvider()->getUserAttribute($user->getUID(), $property);
}
}

0 comments on commit 0ca0287

Please sign in to comment.