* @package generis */ class AuthAdapter extends Configurable implements LoginAdapter { const OPTION_PATTERN = 'pattern'; const OPTION_USERFACTORY = 'user_factory'; /** * Returns the hashing algorithm defined in generis configuration * use core_kernel_users_Service::getPasswordHash() instead * * @return \helpers_PasswordHash * @deprecated */ public static function getPasswordHash() { return core_kernel_users_Service::getPasswordHash(); } /** * Username to verify * * @var string */ protected $username; /** * Password to verify * * @var $password */ protected $password; /** * (non-PHPdoc) * @see \oat\oatbox\user\auth\LoginAdapter::setCredentials() */ public function setCredentials($login, $password) { $this->username = $login; $this->password = $password; } /** * (non-PHPdoc) * @see \common_user_auth_Adapter::authenticate() * @throws \Exception */ public function authenticate() { if ($this->hasOption(self::OPTION_PATTERN)) { if (preg_match($this->getOption(self::OPTION_PATTERN), $this->username) === 0) { throw new core_kernel_users_InvalidLoginException("Invalid pattern for user '" . $this->username . "'."); } } $userClass = new core_kernel_classes_Class(GenerisRdf::CLASS_GENERIS_USER); $filters = [GenerisRdf::PROPERTY_USER_LOGIN => $this->username]; $options = ['like' => false, 'recursive' => true]; $users = $userClass->searchInstances($filters, $options); if (count($users) > 1) { // Multiple users matching throw new common_exception_InconsistentData("Multiple Users found with the same login '" . $this->username . "'."); } if (empty($users)) { // fake code execution to prevent timing attacks $label = new core_kernel_classes_Property(OntologyRdfs::RDFS_LABEL); $hash = $label->getUniquePropertyValue($label); if (!core_kernel_users_Service::getPasswordHash()->verify($this->password, $hash)) { throw new core_kernel_users_InvalidLoginException('Unknown user "' . $this->username . '"'); } // should never happen, added for integrity throw new core_kernel_users_InvalidLoginException('Inexisting user did not fail password check, this should not happen'); } $userResource = current($users); $hash = $userResource->getUniquePropertyValue(new core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_PASSWORD)); if (!core_kernel_users_Service::getPasswordHash()->verify($this->password, $hash)) { throw new core_kernel_users_InvalidLoginException('Invalid password for user "' . $this->username . '"'); } if ($this->hasOption(self::OPTION_USERFACTORY)) { $userFactory = ServiceManager::getServiceManager()->get($this->getOption(self::OPTION_USERFACTORY)) ; if ($userFactory instanceof UserFactoryServiceInterface) { return $userFactory->createUser($userResource, UserHashForEncryption::hash($this->password)); } } return (new UserFactoryService())->createUser($userResource); } }