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); }
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; }