示例#1
0
 /**
  * Creates new secrets.
  *
  * @return array the secrets
  */
 private function createSecrets()
 {
     $n = $this->numberOfSecrets;
     $res = array();
     while ($n-- > 0) {
         $res[] = $this->hashFunction->calculateHash(mt_rand());
     }
     return $res;
 }
 public function authenticate($gid, $data)
 {
     assert(isset($data['password']));
     $password = $data['password'];
     $hash = $this->storage->fetch($gid);
     if ($hash === null) {
         throw new AuthenticationException('No authentication found for user');
     }
     return $this->hashFunction->checkPasswordHash($password, $hash, $this->hmacKey);
 }
示例#3
0
 public function deleteToken($gid, $token)
 {
     if (!file_exists($this->path)) {
         return false;
     }
     $handle = fopen($this->path, 'r+');
     flock($handle, LOCK_EX);
     while (!flock($handle, LOCK_EX)) {
         // Wait for file lock
         usleep(100);
     }
     clearstatcache();
     $size = filesize($this->path);
     $deletions = 0;
     if ($size > 0) {
         $json = fread($handle, $size);
         $content = json_decode($json, true);
         if (is_array($content)) {
             foreach ($content as $key => $row) {
                 if ($row['gid'] === $gid) {
                     if ($this->hashFunction->checkPasswordHash($token, $row['token'], $this->hmacKey)) {
                         unset($content[$key]);
                         $deletions++;
                     }
                 }
             }
             if ($deletions > 0) {
                 $content = json_encode($content);
                 if (is_string($content)) {
                     ftruncate($handle, 0);
                     fseek($handle, 0, SEEK_SET);
                     fwrite($handle, $content);
                 }
             }
         }
     }
     flock($handle, LOCK_UN);
     fclose($handle);
     return $deletions > 0 ? true : false;
 }
 /**
  * Creates the HMAC for the cookie.
  *
  * The HMAC is used to ensure the cookie cannot be tampered or replayed.
  *
  * @param string $userGID the user GID
  * @param string $data the serialized data
  * @param int $expires the expiration date
  * @param string $key the key generated by <code>createKey()</code>
  * @return string the HMAC
  */
 private function createHMAC($userGID, $data, $expires, $key)
 {
     return $this->hmacFunction->calculateHMAC($userGID . $expires . $data . $this->sessionIdentifier, $key);
 }