verify() public static method

Verifies that a password matches a hash.
public static verify ( $password, $hash ) : boolean
return boolean
 /**
  * @param array $credentials
  * @return Identity
  * @throws AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($email, $password) = $credentials;
     $user = $this->orm->users->getByEmail($email);
     if (!$user) {
         throw new AuthenticationException('auth.flash.wrongUsername', self::IDENTITY_NOT_FOUND);
     }
     if (!$user->password) {
         throw new AuthenticationException('auth.flash.notSet', self::PASSWORD_NOT_SET);
     }
     $plainHash = $this->aes->decrypt($user->password);
     if (strpos($user->password, 'old-password;') === 0) {
         $this->authOldPassword($password, $user);
     } else {
         if (!Passwords::verify($password, $plainHash)) {
             throw new AuthenticationException('auth.flash.wrongPassword', self::INVALID_CREDENTIAL);
         }
     }
     if (Passwords::needsRehash($plainHash)) {
         $plainHash = Passwords::hash($password);
         $user->password = $this->aes->encrypt($plainHash);
         $this->orm->flush();
     }
     return new Identity($user->id);
 }
Ejemplo n.º 2
0
 /**
  * Performs an authentication.
  *
  * @param array $credentials
  *
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->userRepository->findOneBy(["userName" => $username]);
     $authExt = false;
     foreach ($this->authDrivers as $authDriver) {
         $authDriver->setUp($this->userRepository, $this->roleRepository, $this->userActivityRepository);
         if ($authExt = $authDriver->authenticate($username, $password, $user)) {
             if ($user) {
                 $user->authMethod = $authDriver->getName();
                 break;
             } else {
                 $authExt = false;
             }
         }
     }
     if (!$authExt) {
         if (!$user) {
             throw new Security\AuthenticationException('Wrong username.', self::IDENTITY_NOT_FOUND);
         } elseif (!Security\Passwords::verify($password, $this->userRepository->getPassword($user))) {
             throw new Security\AuthenticationException('Wrong password.', self::INVALID_CREDENTIAL);
         }
     }
     // update lastLogged
     $user->lastLogged = new DateTime();
     $this->userRepository->save($user);
     $userActivity = new UserActivity();
     $userActivity->userId = $user->id;
     $userActivity->type = "login";
     $this->userActivityRepository->save($userActivity);
     return new Security\Identity($user->id, $user->roles, $user);
 }
Ejemplo n.º 3
0
 /**
  * Performs an authentication against e.g. database.
  * and returns IIdentity on success or throws AuthenticationException
  * @var array $credentials
  * @return Nette\Security\IIdentity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     if (is_null($password)) {
         $user = $this->manager->getItem($username);
     } else {
         $user = $this->manager->getUserByUsername($username);
         if (is_null($user->password)) {
             throw new Trejjam\Authorization\User\AuthenticatorException('User has not have defined password.', Trejjam\Authorization\User\AuthenticatorException::NOT_DEFINED_PASSWORD);
         } else {
             if (!Nette\Security\Passwords::verify($password, $user->password)) {
                 throw new Trejjam\Authorization\User\AuthenticatorException('The password is incorrect.', Trejjam\Authorization\User\AuthenticatorException::INVALID_CREDENTIAL);
             } else {
                 if (Nette\Security\Passwords::needsRehash($user->password)) {
                     $this->manager->changePassword($user, $password);
                 }
             }
         }
     }
     if ($this->manager->isEnableLogin($user)) {
         $baseUserArr = (array) $this->manager->getUserInfo($user);
         $baseUserArr["login_time"] = new Nette\Utils\DateTime();
         $role = $this->acl->getUserRoles($user);
         return new Identity($user->id, $role, $baseUserArr);
     } else {
         //this may not happen
         throw new Trejjam\Authorization\User\AuthenticatorException();
     }
 }
Ejemplo n.º 4
0
 /**
  * Performs an authentication.
  * @param array $credentials (string $username, string $password)
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $row = $this->database->table('user')->where('username', $username)->fetch();
     if (!$row) {
         throw new Security\AuthenticationException('Uživatel s tímto jménem neexistuje.', self::IDENTITY_NOT_FOUND);
     } elseif (!Security\Passwords::verify($password, $row->password)) {
         throw new Security\AuthenticationException('Nesprávné heslo.', self::INVALID_CREDENTIAL);
     } elseif (!$row->active) {
         throw new Security\AuthenticationException('Účet není aktivovaný.', self::NOT_APPROVED);
     } elseif (Security\Passwords::needsRehash($row->password)) {
         $row->update(array('password' => Security\Passwords::hash($password)));
     }
     $arr = $row->toArray();
     unset($arr['password']);
     $roles = $row->related('privilege')->fetch()->toArray();
     unset($roles['user_id']);
     //adds privileges
     array_walk($roles, function (&$value, $key) use(&$roles) {
         if ($value != NULL) {
             $value = $key . ' - ' . $value;
         }
     });
     return new Security\Identity($row->id, $roles, $arr);
 }
Ejemplo n.º 5
0
 public function editUser($values, $user_id)
 {
     //        $temp = $this->database->table('user')->where('email = ?', $values->email)->fetch();
     $row = $this->database->table('user')->where('id', $user_id)->fetch();
     if (!NS\Passwords::verify($values->oldPassword, $row->password)) {
         //            throw new NS\AuthenticationException('Špatné heslo.');
         $check = 0;
     } else {
         if ($values->newPassword != NULL) {
             $this->database->table('user')->where('id', $user_id)->update(['password' => Passwords::hash($values->newPassword)]);
         }
         if ($values->username != NULL) {
             $this->database->table('user')->where('id', $user_id)->update(['username' => $values->username]);
         }
         $check = 1;
     }
     //        $check = 0;
     //        if ((!$temp)) $check = 1;
     //        if ($check) {
     //            $this->database->table('user')->where('id', $user_id)->update([
     //                'username' => $values->username,
     //                'password' => Passwords::hash($values->newPassword),
     //            ]);
     //
     //            /*$mail = new Message;
     //            $mail->setFrom('BrNOC bot <*****@*****.**>')
     //                ->addTo($values->email)
     //                ->setSubject('Potvrzení příhlášení')
     //                ->setBody("Byl jsi přihlášen jako účastník BrNOCi 2015. \n \nBrNOC tým");*/
     //        }
     return $check;
 }
Ejemplo n.º 6
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     if (!Passwords::verify($credentials[1], $this->database->table('option')->get('password')->value)) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
     }
     return new Nette\Security\Identity(NULL, NULL, NULL);
 }
 public function onBeforeChange(Form $form, User $user)
 {
     $values = $form->getValues();
     if (!Passwords::verify($values['currentPassword'], $user->password)) {
         $this->flashMessage('Heslo nelze změnit, protože nesouhlasí
              Vaše aktuální heslo.', 'warning');
         $this->redirect('this');
     }
 }
Ejemplo n.º 8
0
 public function updateUserPasswd($curentPasswd, $newPasswd)
 {
     $row = $this->database->table(self::USER_TABLE_NAME)->where(self::USER_COLUMN_ID, $this->user->identity->id)->fetch();
     if (Passwords::verify($curentPasswd, $row[self::USER_COLUMN_PASSWORD])) {
         $this->database->table(self::USER_TABLE_NAME)->where(self::USER_COLUMN_ID, $this->user->identity->id)->update(array(self::USER_COLUMN_PASSWORD => Passwords::hash($newPasswd)));
         return True;
     } else {
         return False;
     }
 }
Ejemplo n.º 9
0
 /**
  * @param array $credentials
  * @throws AuthenticationException
  * @return Identity
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     /** @var User $user */
     $user = $this->userDao->findOneBy(['username' => $username]);
     if (!$user || !Passwords::verify($password, $user->getPassword())) {
         throw new AuthenticationException('Invalid username or password.', self::IDENTITY_NOT_FOUND);
     }
     return new Identity($user->getUserId(), NULL, $user->getLoginData());
 }
Ejemplo n.º 10
0
 /**
  * Compare $password to origin
  * @param $password
  * @return bool
  */
 public function verifyPassword($password)
 {
     if (Passwords::verify($password, $this->password)) {
         if (Passwords::needsRehash($this->password)) {
             $this->setPassword($password);
         }
         return TRUE;
     }
     return FALSE;
 }
 /**
  * Funkce kontrolující, zda zadané heslo odpovídá hodnotě uložené v configu
  * @param string $password
  * @return bool
  */
 public function checkInstallationPassword($password)
 {
     $passwordHash = $this->data['parameters']['install']['password'];
     if (Passwords::verify($password, $passwordHash)) {
         if (Passwords::needsRehash($password)) {
             $this->saveInstallationPassword($password);
         }
         return true;
     }
     return false;
 }
Ejemplo n.º 12
0
 /**
  * @param array $credentials
  * @return \Nette\Security\Identity
  * @throws \Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->repository->findUserByName($username);
     if (!is_null($user) && \Nette\Security\Passwords::verify($password, $user->getPassword())) {
         $group = $this->repository->findGroupById($user->getGroupId());
         return new \Nette\Security\Identity($user->id, $group->getName(), $user->getIdentity());
     } else {
         throw new \Nette\Security\AuthenticationException($this->t("forms.sign.errors.wrong-credentials"), self::IDENTITY_NOT_FOUND);
     }
 }
Ejemplo n.º 13
0
 public function changePassword($userId, $oldPassword, $newPassword)
 {
     $user = $this->get($userId);
     if (Nette\Security\Passwords::verify($oldPassword, $user->passwordHash)) {
         $user->passwordHash = Nette\Security\Passwords::hash($newPassword);
         $this->em->flush();
         return TRUE;
     } else {
         return FALSE;
     }
 }
 function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->user_service->getByLogin($username);
     if (!$user) {
         throw new AuthenticationException('Přihlášení se nezdařilo.');
     }
     if (!Passwords::verify($password, $user->password)) {
         throw new AuthenticationException('Přihlášení se nezdařilo.');
     }
     return new Identity($user->id_user);
 }
Ejemplo n.º 15
0
 public function passwordChangeFormSucceeded(Form $form, $values)
 {
     $user = $this->userFacade->findOneById($this->user->getId());
     if (!Passwords::verify($values->actualPassword, $user->hash)) {
         $form->addError("Zadal si špatné aktuální heslo. Zkus to znovu!");
     } else {
         $user->changePassword($values->password);
         $this->userFacade->save($user);
         $form->getPresenter()->flashMessage("Tvoje heslo bylo úspěšně změněno", "alert-success");
         $form->getPresenter()->redirect("this");
     }
 }
Ejemplo n.º 16
0
 function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $row = $this->database->table('users')->where('username', $username)->fetch();
     if (!$row) {
         throw new NS\AuthenticationException('User not found.');
     }
     if (!NS\Passwords::verify($password, $row->password)) {
         throw new NS\AuthenticationException('Invalid password.');
     }
     return new NS\Identity($row->id, $row->role, array('username' => $row->username));
 }
Ejemplo n.º 17
0
 /**
  * Processing of change user password form
  *
  * @param \Nette\Application\UI\Form $form
  *
  * @Privilege("default")
  */
 public function changePasswordSucceded(\Nette\Application\UI\Form $form)
 {
     $values = $form->getValues(TRUE);
     $row = $this->users->get($this->user->id);
     if (!\Nette\Security\Passwords::verify($values['oldpassword'], $row->password)) {
         $form->addError('Nesprávné heslo.');
     } else {
         $this->users->updatePassword($row->id, $values['password']);
         $this->flashMessage('Heslo bylo změněno');
     }
     $this->redirect('this');
 }
Ejemplo n.º 18
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->userRepository->getUserByLogin($username);
     if (!$user) {
         throw new Nette\Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($password, $user->getPassword())) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
     }
     $oParams = ['login' => $user->getLogin()];
     return new Nette\Security\Identity($user->getId(), $user->getRole(), $oParams);
 }
Ejemplo n.º 19
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $row = $this->em->getRepository(UserEntity::getClassName())->findOneBy(array('login' => $username));
     if (!$row) {
         throw new Nette\Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($password, $row->password)) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
     }
     $arr = array('id' => $row->id, 'role' => '');
     return new Nette\Security\Identity($arr[self::COLUMN_ID], $arr['role'], $arr);
 }
Ejemplo n.º 20
0
 public function authenticate($username, $password)
 {
     /** @var \stdClass $user */
     $user = $this->userModel->getByUsername($username);
     if (!$user) {
         throw new Nette\Security\AuthenticationException('Username does not exist.');
     } elseif (!Nette\Security\Passwords::verify($password, $user->password)) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.');
     }
     unset($user->password);
     return new Nette\Security\Identity($user->id, $user->access, $user);
 }
Ejemplo n.º 21
0
 public function validate()
 {
     if ($this->createdAt < DateTime::from('- 3 days')) {
         throw new TokenExpiredException();
     }
     if ($this->used) {
         throw new TokenAlreadyUsedException();
     }
     if (!Passwords::verify($this->unsafe, $this->hash)) {
         throw new TokenNotValidException();
     }
 }
Ejemplo n.º 22
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $row = $this->database->table('users')->where('username', $username)->fetch();
     if (!$row) {
         throw new Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
     } elseif (!Security\Passwords::verify($password, $row->password)) {
         throw new Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
     }
     $arr = $row->toArray();
     unset($arr['password']);
     return new Security\Identity($row->id, NULL, $arr);
 }
Ejemplo n.º 23
0
 /**
  * Performs an authentication.
  * @param array $credentials
  * @return Identity
  * @throws AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->uf->findByEmail($username);
     if (!$user) {
         throw new AuthenticationException('Neznámý uživatel.', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($password, $user->password)) {
         throw new AuthenticationException('Chybné heslo.', self::INVALID_CREDENTIAL);
     }
     $user->last_seen = new DateTime();
     $this->uf->persistAndFlush($user);
     $r = array("email" => $user->email, "username" => $user->login_name);
     return new Identity($user->id, $user->role, $r);
 }
Ejemplo n.º 24
0
 /**
  * @param array $credentials
  * @return null|User
  * @throws AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($email, $password) = $credentials;
     $user = $this->users->findOneBy(['email' => $email]);
     if (!$user) {
         throw new AuthenticationException('Invalid credentials.', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($password, $user->password)) {
         throw new AuthenticationException('Invalid credentials.', self::INVALID_CREDENTIAL);
     } elseif (Passwords::needsRehash($user->password)) {
         $user->setPassword($password);
         $this->em->flush();
     }
     return $user;
 }
Ejemplo n.º 25
0
 public function verifyToken($username, $token)
 {
     $now = new \Nette\DateTime();
     //$hashed = \Nette\Security\Passwords::hash($token);
     $row = $this->localLoginModel->query("SELECT * FROM `login_local` JOIN `user` ON `user`.`id` = `login_local`.`user_id` WHERE `user`.`email` = \"{$username}\"")->fetch();
     if (!$row) {
         throw new Nette\Security\AuthenticationException('Neznámé uživatelské jméno.', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($token, $row["token"])) {
         throw new Nette\Security\AuthenticationException('Neodpovídající kód.', self::INVALID_CREDENTIAL);
     } elseif ($now > new \Nette\DateTime($row["token_expiration"])) {
         throw new Nette\Security\AuthenticationException('Platnost kódu již vypršela.', self::INVALID_CREDENTIAL);
     }
     return true;
 }
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($email, $password) = $credentials;
     $row = $this->database->table(self::USER_TABLE_NAME)->where(self::USER_COLUMN_EMAIL, $email)->fetch();
     if (!$row) {
         throw new Nette\Security\AuthenticationException('Uživatel pod tímto emailem není zaregistrován!', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($password, $row[self::USER_COLUMN_PASSWORD])) {
         throw new Nette\Security\AuthenticationException('Nesprávné heslo!', self::INVALID_CREDENTIAL);
     }
     $arr = $row->toArray();
     $arr['color'] = $row->color[self::COLOR_COLUMN_COLOR];
     unset($arr[self::USER_COLUMN_ID], $arr[self::USER_COLUMN_PASSWORD], $arr[self::USER_COLUMN_COLOR]);
     return new Nette\Security\Identity($row[self::USER_COLUMN_ID], 'Uživatel', $arr);
 }
Ejemplo n.º 27
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($email, $password) = $credentials;
     $row = $this->getOne([self::COLUMN_EMAIL => $email]);
     if (!$row) {
         throw new Nette\Security\AuthenticationException("User not found.");
     }
     if (!Passwords::verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
         throw new Nette\Security\AuthenticationException("Bad password.");
     }
     $data = $row->toArray();
     unset($data[self::COLUMN_PASSWORD_HASH]);
     return new Nette\Security\Identity($data[self::COLUMN_ID], ['root' => $data[self::COLUMN_ROOT]], $data);
 }
Ejemplo n.º 28
0
 /**
  * @inheritdoc
  */
 public function authenticate(array $credentials)
 {
     list($email, $password) = $credentials;
     $email = UserTools::sanitizeUserEmail($email);
     /** @var User|null $user */
     $user = $this->registry->getRepository(User::class)->findOneBy(['email' => $email]);
     if ($user === null) {
         throw new AuthenticationException("User '{$email}' not found.");
     }
     if (!Passwords::verify($password, $user->getPassword())) {
         throw new AuthenticationException('Invalid password.');
     }
     return new Identity($user->getId(), $user->getUserRoles(), ['username' => $user->getEmail()]);
 }
Ejemplo n.º 29
0
 /**
  * Performs an authentication
  *
  * Partly taken from Nette Sandbox tutorial
  *
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($email, $password) = $credentials;
     $user = $this->database->table('user')->where('email', $email)->fetch();
     if (!$user) {
         throw new Nette\Security\AuthenticationException('User not found.', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($password, $user['password'])) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
     } elseif (Passwords::needsRehash($user['password'])) {
         // from Nette Sandbox
         $user->update(array('password' => Passwords::hash($password)));
     }
     return new Nette\Security\Identity($user['id'], 'admin', array('name' => $user->name, 'email' => $user->email));
 }
Ejemplo n.º 30
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->usersFacade->findByName($username);
     if ($user == null) {
         throw new Nette\Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
     } elseif (!Passwords::verify($password, $user->getPassword())) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
     } elseif (Passwords::needsRehash($user->getPassword())) {
         $this->usersFacade->update(array('password' => Passwords::hash($password)));
     }
     $arr = array($user->getUsername(), $user->getEmail(), $user->getDate());
     return new Nette\Security\Identity($user->getId(), $user->getRole(), $arr);
 }