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 external storage login credentials #26397

Merged
merged 1 commit into from
Jun 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
37 changes: 34 additions & 3 deletions apps/files_external/lib/Lib/Auth/Password/LoginCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserBackend;
use OCP\LDAP\ILDAPProviderFactory;
use OCP\Security\ICredentialsManager;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserLoggedInEvent;
Expand All @@ -55,10 +57,21 @@ class LoginCredentials extends AuthMechanism {
/** @var CredentialsStore */
private $credentialsStore;

public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) {
/** @var ILDAPProviderFactory */
private $ldapFactory;
icewind1991 marked this conversation as resolved.
Show resolved Hide resolved

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 +99,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 +117,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 IUserBackend && $backend->getBackendName() === 'LDAP') {
Copy link
Member

Choose a reason for hiding this comment

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

$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);
}
}