hash() public static method

Computes salted password hash.
public static hash ( $password, array $options = [] ) : string
$options array
return string 60 chars long
Esempio n. 1
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @return void
  */
 public function add($username, $password)
 {
     $user = new Entities\UserEntity();
     $user->setUsername($username);
     $user->setPassword(Passwords::hash($password));
     $this->userRepository->save($user);
 }
 public function registerFormSucceeded($form, $values)
 {
     $hash = \Nette\Security\Passwords::hash($values['password']);
     $reguser = $this->database->table('users')->insert(array('username' => $values->username, 'password' => $hash, 'email' => $values->email));
     $this->flashMessage("Gratulujeme. Boli ste úspešne zaregistrovaný. Môžte sa prihlásiť do aplikácie.", 'success');
     $this->redirect('Sign:in');
 }
Esempio n. 3
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;
 }
 /**
  * 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);
 }
 public function onSuccess()
 {
     $v = $this->values;
     $user = $this->orm->users->getByEmail($v->email);
     if ($user && $user->registered) {
         $this->addError('duplicate');
         return;
     }
     if (!$user) {
         $user = new User();
         $user->email = $v->email;
         $this->orm->users->attach($user);
     }
     $user->gender = $v->gender;
     $user->setNames($v->name);
     $user->registered = TRUE;
     $plainHash = Passwords::hash($v->password);
     $user->password = $this->aes->encrypt($plainHash);
     $this->orm->flush();
     /** @var Auth $presenter */
     $presenter = $this->presenter;
     $presenter->user->login(new Identity($user->id));
     $this->iLog('auth.registration.password', ['entropy' => $this->entropy->compute($v->password, $user)]);
     $presenter->onLogin($user, TRUE);
 }
Esempio n. 6
0
 public function hashPassword(ArrayHash $values)
 {
     if ($values->password) {
         $values->password = Passwords::hash($values->password);
     } else {
         unset($values->password);
     }
 }
Esempio n. 7
0
 /**
  * @param \stdClass $user
  * @throws DuplicateNameException
  */
 public function add(\stdClass $user)
 {
     try {
         $this->userModel->add(['username' => $user->username, 'email' => $user->email, 'password' => Passwords::hash($user->password), 'first_name' => $user->firstName, 'last_name' => $user->lastName]);
     } catch (UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 8
0
 public function update($userID, $login, $password, $email)
 {
     try {
         $this->database->table(self::TABLE_NAME)->get($userID)->update(array(self::COLUMN_NAME => $login, self::COLUMN_PASSWORD_HASH => Passwords::hash($password), self::COLUMN_EMAIL => $email));
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
 /**
  * @param string|null $password
  */
 public function renderDefault($password = NULL)
 {
     if (!empty($password)) {
         $this->template->hash = \Nette\Security\Passwords::hash($password);
     }
     $this->setLayout(FALSE);
     $this->template->setFile(__DIR__ . '/template.latte');
 }
Esempio n. 10
0
 /**
  * Adds new user.
  *
  * @param  string
  * @param  string
  */
 public function add($username, $password)
 {
     try {
         $this->connection->query('INSERT INTO [' . table(self::TABLE_NAME) . ']', array(self::COLUMN_NAME => $username, self::COLUMN_PASSWORD_HASH => Passwords::hash($password)));
     } catch (Exception $e) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 11
0
 /**
  * Upraví
  * @param int
  * @param string
  * @param string
  * @param string
  * @return int
  */
 public function uprav($id, $jmeno, $heslo, $role)
 {
     $u = array(self::COLUMN_NAME => $jmeno, self::COLUMN_ROLE => $role);
     if ($heslo) {
         $u[self::COLUMN_PASSWORD_HASH] = Passwords::hash($heslo);
     }
     return $this->database->table(self::TABLE_NAME)->where(self::COLUMN_ID, (int) $id)->update($u);
 }
Esempio n. 12
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @return void
  * @throws DuplicateNameException
  */
 public function add($username, $password, $role = 'guest')
 {
     try {
         $this->db->insert(self::TABLE_NAME, [self::COLUMN_NAME => $username, self::COLUMN_PASSWORD_HASH => Passwords::hash($password), self::COLUMN_ROLE => $role])->execute();
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 13
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @param  int
  * @return void
  */
 public function add($sUserName, $sPassword, $sEmail, $iRole)
 {
     try {
         $this->oDatabase->table(self::TABLE_NAME)->insert(array(self::COLUMN_NAME => $sUserName, self::COLUMN_PASSWORD_HASH => Passwords::hash($sPassword), self::COLUMN_MAIL => $sEmail, self::COLUMN_TYP => $iRole));
     } catch (Nette\Database\UniqueConstraintViolationException $oException) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 14
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @return void
  * @throws DuplicateNameException
  */
 public function add($username, $email, $password)
 {
     try {
         $this->database->table(self::TABLE_NAME)->insert([self::COLUMN_NAME => $username, self::COLUMN_PASSWORD_HASH => Passwords::hash($password), self::COLUMN_EMAIL => $email]);
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 15
0
 /**
  * @param string $username
  * @param string $password
  * @param string $fullname
  */
 public function add($username, $password, $fullname)
 {
     $user = new User();
     $user->setUsername($username);
     $user->setPassword(Passwords::hash($password));
     $user->setFullname($fullname);
     $this->userDao->safePersist($user);
 }
Esempio n. 16
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @param  DateTime
  * @return void
  */
 public function add($username, $password, $email, $date)
 {
     try {
         $data = array('username' => $username, 'password' => Passwords::hash($password), 'email' => $email, 'date' => $date, 'role' => 'user');
         $this->usersFacade->update($data);
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 17
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @return void
  */
 public function add($username, $email, $password)
 {
     Passwords::validateNew($password);
     try {
         $this->db->table(static::TABLE_NAME)->insert(array(static::COLUMN_NAME => $username, static::COLUMN_EMAIL => $email, static::COLUMN_PASSWORD_HASH => Security\Passwords::hash($password)));
     } catch (Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 18
0
 /**
  * @param User $user
  * @return static
  */
 public static function createFromUser(User $user)
 {
     $token = new static();
     $ref = new \ReflectionClass($token);
     $token->type = $ref->getShortName();
     $token->user = $user;
     $token->unsafe = $token->computeUnsafeHash();
     $token->hash = Passwords::hash($token->unsafe);
     return $token;
 }
Esempio n. 19
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;
     }
 }
 /**
  * @deprecated
  * @param string $password
  * @param $user
  * @throws AuthenticationException
  */
 private function authOldPassword($password, $user)
 {
     list($_, $hash, $salt) = explode(';', $user->password);
     if ($this->calculateHash($password, $salt) !== $hash) {
         throw new AuthenticationException('auth.flash.wrongPassword', self::INVALID_CREDENTIAL);
     }
     $plainHash = Passwords::hash($password);
     $user->password = $this->aes->encrypt($plainHash);
     $this->orm->flush();
 }
Esempio n. 21
0
 function signUpFormSucceeded(\Nette\Forms\BootstrapUIForm $form)
 {
     $activationCode = \Nette\Utils\Random::generate(12, "987654321zyxwvutsrqponmlkjihgfedcba");
     $password = \Nette\Security\Passwords::hash($form->values->pwd);
     $arr = array("email" => $form->values->email, "username" => $form->values->username, "password" => $password, "activation" => $activationCode, "newsletter" => (bool) $form->values->newsletter, "state" => 0, "users_roles_id" => 4, "date_created" => date("Y-m-d H:i:s"));
     if ($this->presenter->template->settings['members:groups:enabled']) {
         $arr["categories_id"] = $form->values->group;
     }
     $userId = $this->database->table("users")->insert($arr);
     $this->database->table("users")->where(array("id" => $userId->id))->update(array("uid" => \Nette\Utils\Strings::padLeft($userId->id, 6, '0')));
     if ($this->template->settings['members:signup:contactEnabled']) {
         $arrContacts = array("categories_id" => 44, "users_id" => $userId, "name" => $form->values->name, "street" => $form->values->street, "city" => $form->values->city, "zip" => $form->values->zip, "countries_id" => 1);
         if ($this->presenter->template->settings['members:signup:companyEnabled']) {
             $arrContacts["company"] = $form->values->company;
             $arrContacts["vatin"] = $form->values->vatin;
             $arrContacts["vatid"] = $form->values->vatid;
         }
         $contactId = $this->database->table("contacts")->insert($arrContacts);
         $this->database->table("contacts")->get($contactId)->update(array("order" => $contactId));
     }
     if ($form->values->vatin) {
         $ares = new \h4kuna\Ares\Ares();
         $aresArr = $ares->loadData('')->toArray();
     }
     $latte = new \Latte\Engine();
     $latte->setLoader(new \Latte\Loaders\StringLoader());
     $params = array('username' => $form->values->username, 'activationCode' => $activationCode, 'settings' => $this->presenter->template->settings, 'form' => $form, 'aresArr' => $aresArr);
     $helpdesk = $this->database->table("helpdesk")->get(3);
     $helpdesk_signup_member = $helpdesk->related("helpdesk_emails", "helpdesk_id")->get(5);
     $helpdesk_signup_confirmbyadmin = $helpdesk->related("helpdesk_emails", "helpdesk_id")->get(6);
     $helpdesk_signup_adminconfirm = $helpdesk->related("helpdesk_emails", "helpdesk_id")->get(7);
     try {
         if ($this->presenter->template->settings['members:signup:confirmByAdmin']) {
             $email_signup_confirmbyamin = $latte->renderToString($helpdesk_signup_confirmbyadmin->body, $params);
             $email_signup_adminconfirm = $latte->renderToString($helpdesk_signup_adminconfirm->body, $params);
             $mail = new \Nette\Mail\Message();
             $mail->setFrom($this->presenter->template->settings['contacts:email:hq'])->addTo($form->values->email)->setHTMLBody($email_signup_confirmbyamin);
             $this->presenter->mailer->send($mail);
             $mailA = new \Nette\Mail\Message();
             $mailA->setFrom($this->presenter->template->settings['contacts:email:hq'])->addTo($this->presenter->template->settings['contacts:email:hq'])->setHTMLBody($email_signup_adminconfirm);
             $this->presenter->mailer->send($mailA);
             $this->flashMessage('Registrace byla dokončena. Po ověření Vám bude zaslán e-mail, po kterém se můžete přihlásit', 'note');
         } else {
             $email_signup_member = $latte->renderToString($helpdesk_signup_member->body, $params);
             $mail = new \Nette\Mail\Message();
             $mail->setFrom($this->presenter->template->settings['contacts:email:hq'])->addTo($form->values->email)->setHTMLBody($email_signup_member);
             $this->presenter->mailer->send($mail);
             $this->presenter->flashMessage('Vaše registrace proběhla úspěšně. Po ověření se můžete přihlásit.', 'note');
         }
         $this->presenter->redirect(":Front:Sign:ed");
     } catch (\Nette\Mail\SmtpException $e) {
         $this->presenter->flashMessage('E-mail nebyl odeslán' . $e->getMessage(), 'error');
         $this->presenter->redirect(":Front:Sign:up");
     }
 }
Esempio n. 22
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;
     }
 }
Esempio n. 23
0
 public function changePassword($oldPassword, $newPassword)
 {
     $this->auth->checkCredentials([$this->user->identity->name, $oldPassword]);
     $user = $this->userService->get((int) $this->user->identity->id);
     $login = $user->login;
     $login->password = Nette\Security\Passwords::hash($newPassword);
     $login->passwordLastChangedAt = new Nette\Utils\DateTime();
     $this->userLoginService->persist($login);
     $this->userLoginService->flush();
     $this->aclFactory->invalidateCache();
 }
Esempio n. 24
0
 /**
  * Vytvoří nového uživatele a uloží jej do databáze.
  * @param \Nette\Utils\ArrayHash $values
  * @param int $defaultGroupId
  * @param int $status
  * @return \Model\Core\User\User
  */
 public function createNewUser(\Nette\Utils\ArrayHash $values, $defaultGroupId = BasicGroupId::USER, $status = \Model\Common\RecordStatus::VALID)
 {
     $user = new User();
     $user->setUsername($values->username);
     $user->setPassword(\Nette\Security\Passwords::hash($values->password));
     $user->setEmail($values->email);
     $user->setGroupId($defaultGroupId);
     $user->setStatus($status);
     $this->saveUser($user);
     return $user;
 }
Esempio n. 25
0
 /**
  * @param string $email
  * @param string $password
  * @param string $roleStringIdentifier
  * @return User
  * @throws RoleDoesNotExistException
  * @throws UserAlreadyExistsException
  */
 public function register(string $email, string $password, string $roleStringIdentifier)
 {
     $entityManager = $this->registry->getManager();
     $email = UserTools::sanitizeUserEmail($email);
     $this->validateUserDoesNotExists($email);
     $passwordHash = Passwords::hash($password);
     $user = new User($email, $passwordHash);
     $entityManager->persist($user);
     $this->roleToUserAssigner->assignByString($roleStringIdentifier, $user);
     $entityManager->flush($user);
     return $user;
 }
Esempio n. 26
0
 public function generateHash($userId, $type, $timeout = NULL)
 {
     if (!$this->isTypeValid($type)) {
         throw new Trejjam\Authorization\User\RequestException("Type '{$type}' is not valid or registered");
     }
     $hash = Nette\Utils\Random::generate($this->tables['userRequest']['hash']['length'], '0-9A-Z');
     if (is_null($timeout)) {
         $timeout = $this->tables['userRequest']['timeout']['default'];
     }
     $insertion = $this->getTable()->insert([$this->tables['userRequest']['userId'] => isset($userId->{static::ROW}) ? $userId->id : $userId, $this->tables['userRequest']['hash']['name'] => Nette\Security\Passwords::hash($hash), $this->tables['userRequest']['type']['name'] => $type, $this->tables['userRequest']['timeout']['name'] => $timeout === FALSE ? NULL : new Nette\Database\SqlLiteral('NOW() + INTERVAL ' . $timeout)]);
     return [$insertion->id, $hash];
 }
 function changePasswordFormSucceeded(\Nette\Forms\BootstrapUIForm $form)
 {
     $ppwd = $form->values->password1;
     $ppwd2 = $form->values->password2;
     $passwordEncrypted = \Nette\Security\Passwords::hash($ppwd);
     if (strcasecmp($ppwd, $ppwd2) != 0) {
         $this->presenter->flashMessage('Hesla se neshodují');
     }
     $this->database->table("users")->where(array("id" => $this->presenter->user->getId()))->update(array("password" => $passwordEncrypted));
     setcookie("calpwd", $passwordEncrypted, time() + time() + 60 * 60 * 24 * 30, "/");
     $this->presenter->redirect(this);
 }
Esempio n. 28
0
 public function __construct($email, $username, $name, $surname, $password)
 {
     $this->email = $email;
     $this->username = empty($surname) ? null : $username;
     $this->name = empty($name) ? null : $name;
     $this->surname = empty($surname) ? null : $surname;
     $this->password = Passwords::hash($password);
     $this->code = Random::generate();
     $this->frozen = true;
     $this->createdAt = new DateTime();
     $this->roles = new ArrayCollection();
 }
Esempio n. 29
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @return void
  */
 public function add($fullname, $role, $email, $password)
 {
     try {
         if (!$this->isUserRegistred($email)) {
             $user = $this->database->table(self::TABLE_NAME)->insert(array(self::COLUMN_NAME => $email, 'fullname' => $fullname, 'role' => $role, self::COLUMN_PASSWORD_HASH => Passwords::hash($password), 'ins_dt' => new \DateTime()));
             return $user;
         } else {
             return FALSE;
         }
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Esempio n. 30
0
 public function userFormSucceeded($button)
 {
     $values = $button->getForm()->getValues();
     $id = (int) $this->getParameter('id');
     if ($id) {
         $this->database->table('users')->get($id)->update(array('username' => $values->username, 'password' => Passwords::hash($values->password), 'email' => $values->email));
         $this->flashMessage('User has been updated.');
     } else {
         $this->userManager->add($values->username, $values->password, $values->email);
         $this->flashMessage('User has been added.');
     }
     $this->redirect('Homepage:');
 }