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

allow using any ldap property as login name when using login credentials #24955

Merged
merged 1 commit into from
Jan 7, 2021
Merged
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
40 changes: 38 additions & 2 deletions apps/files_external/lib/Lib/Auth/Password/LoginCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -81,7 +82,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 @@ -99,7 +100,42 @@ 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($backend, $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(IUserLDAP $ldap, IUser $user, string $property): ?string {
$access = $ldap->getLDAPAccess($user->getUID());
$connection = $access->getConnection();
$key = "external_login::" . $user->getUID() . "::" . $property;
$cached = $connection->getFromCache($key);

if ($cached !== null) {
return $cached;
}

$value = $access->readAttribute($access->username2dn($user->getUID()), $property);
if (count($value) > 0) {
$value = current($value);
} else {
return null;
}
$connection->writeToCache($key, $value);
Comment on lines +122 to +137
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer it would use \OCP\ILDAPProvider (via its factory) instead of using private user_ldap classes. https://github.com/nextcloud/server/tree/master/lib/public/LDAP


return $value;
}
}