Exemplo n.º 1
0
 public function resetProcess($code, $newPassword)
 {
     $this->trigger('resetprocess.pre');
     if (!$this->verifyRequestCode($code)) {
         throw new \Exception('Password reset code verify failed');
     }
     $codeItem = $this->getItem('User\\Item\\Code');
     $userId = $codeItem->user_id;
     $this->setItem(array('id' => $userId));
     $item = $this->getItem();
     $item->self(array('*'));
     $salt = $item->salt;
     $oldPassword = $item->password;
     $bcrypt = new \Zend\Crypt\Password\Bcrypt();
     $bcrypt->setSalt($salt);
     $item->password = $bcrypt->create($newPassword);
     $item->oldPassword = $oldPassword;
     $item->lastPasswordChangeTime = \Eva\Date\Date::getNow();
     $this->trigger('resetprocess');
     $item->save();
     $codeItem->clear();
     $codeItem->getDataClass()->where(array('code' => $code))->save(array('codeStatus' => 'used', 'used_by_id' => $userId, 'usedTime' => \Eva\Date\Date::getNow()));
     //One code used will expire all other active codes
     $codeItem->getDataClass()->where(array('codeType' => 'resetPassword', 'codeStatus' => 'active', 'user_id' => $userId))->save(array('codeStatus' => 'expired', 'expiredTime' => \Eva\Date\Date::getNow()));
     $this->trigger('resetprocess.post');
 }
Exemplo n.º 2
0
 public function getPassword()
 {
     if (!$this->password) {
         return null;
     }
     $salt = $this->getSalt();
     $bcrypt = new \Zend\Crypt\Password\Bcrypt();
     $bcrypt->setSalt($salt);
     return $this->password = $bcrypt->create($this->password);
 }
Exemplo n.º 3
0
 public static function verifyPassword($password, $data)
 {
     $userModel = \Eva\Api::_()->getModel('User\\Model\\User');
     $user = $userModel->getUser($data['id']);
     $salt = $user->salt;
     $bcrypt = new \Zend\Crypt\Password\Bcrypt();
     $bcrypt->setSalt($salt);
     $verifyPassword = $bcrypt->create($password);
     if ($verifyPassword === $user->password) {
         return true;
     }
     return false;
 }
 public static function encryptPassword($password)
 {
     $bcrypt = new \Zend\Crypt\Password\Bcrypt(array('cost' => 10));
     return $bcrypt->create($password);
 }
Exemplo n.º 5
0
 public function saveUserAction()
 {
     $em = $this->getEntityManager();
     $request = $this->getRequest();
     // print_r($request->getPost());
     $user = new User();
     $user->setEmail($request->getPost('email'));
     $bcrypt = new \Zend\Crypt\Password\Bcrypt();
     $bcrypt->setSalt('m3s3Cr3tS4lty34h');
     $user->setPassword($bcrypt->create($request->getPost('password')));
     $user->setIsActive(1);
     $user->setUsersalt($bcrypt->create($user->getEmail()));
     $em->persist($user);
     $em->flush();
     return new JsonModel(array(array('user' => $user)));
 }
Exemplo n.º 6
0
/**
 * Bcrypt utility
 *
 * Generates the bcrypt hash value of a string
 */
$autoload = realpath(__DIR__ . '/../vendor/autoload.php');
if (!$autoload) {
    // Attempt to locate it relative to the application root
    $autoload = realpath(__DIR__ . '/../../../autoload.php');
}
$zf2Env = "ZF2_PATH";
if (file_exists($autoload)) {
    include $autoload;
} elseif (getenv($zf2Env)) {
    include getenv($zf2Env) . '/Zend/Loader/AutoloaderFactory.php';
    Zend\Loader\AutoloaderFactory::factory(array('Zend\\Loader\\StandardAutoloader' => array('autoregister_zf' => true)));
}
if (!class_exists('Zend\\Loader\\AutoloaderFactory')) {
    throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}
$bcrypt = new Zend\Crypt\Password\Bcrypt();
if ($argc < 2) {
    printf("Usage: php bcrypt.php <password> [cost]\n");
    printf("where <password> is the user's password and [cost] is the value\nof the cost parameter of bcrypt (default is %d).\n", $bcrypt->getCost());
    exit(1);
}
if (isset($argv[2])) {
    $bcrypt->setCost($argv[2]);
}
printf("%s\n", $bcrypt->create($argv[1]));
Exemplo n.º 7
0
 public function loginByPassword($loginIdentity, $password)
 {
     $identityType = 'userName';
     if (is_numeric($loginIdentity)) {
         $identityType = 'mobile';
     } else {
         $validator = new \Zend\Validator\EmailAddress();
         if ($validator->isValid($loginIdentity)) {
             $identityType = 'email';
         }
     }
     switch ($identityType) {
         case 'email':
             $dbWhere = array('email' => $loginIdentity);
             $identityColumn = 'email';
             break;
         case 'mobile':
             $dbWhere = array('mobile' => $loginIdentity);
             $identityColumn = 'mobile';
             break;
         default:
             $dbWhere = array('userName' => $loginIdentity);
             $identityColumn = 'userName';
     }
     $auth = Auth::factory();
     $user = $this->getItem()->getDataClass()->columns(array('id', 'salt', 'userName'))->where($dbWhere)->find('one');
     if (!$user || !$user['id']) {
         return $this->loginResult = new Result(Result::FAILURE_IDENTITY_NOT_FOUND, $loginIdentity, array(Result::FAILURE_IDENTITY_NOT_FOUND => 'A record with the supplied identity could not be found.'));
     }
     if (!$user['salt']) {
         throw new \Exception(sprintf('User authention salt not found'));
     }
     $bcrypt = new \Zend\Crypt\Password\Bcrypt();
     $bcrypt->setSalt($user['salt']);
     $password = $bcrypt->create($password);
     $this->loginResult = $loginResult = $auth->getAuthService(array('tableName' => 'user_users', 'identityColumn' => $identityColumn, 'credentialColumn' => 'password'))->getAdapter()->setIdentity($loginIdentity)->setCredential($password)->authenticate();
     if ($loginResult->isValid()) {
         return $this->loginById($user['id']);
     }
     return $loginResult;
 }
Exemplo n.º 8
0
 /**
  * Create Secured Password
  *
  * @return string
  */
 public function createSecuredPassword($password)
 {
     return self::$bcrypt->create($password);
 }