/**
  * @param User $user
  * @param int $lifetime
  */
 private function generateToken(User $user, $lifetime = 86400)
 {
     $token = sha1(random_bytes(40));
     $expireAt = new \DateTime();
     $lifetime = $lifetime - $expireAt->getOffset();
     // GMT
     $expireAt->add(new \DateInterval('PT' . (int) $lifetime . 'S'));
     $user->setApiToken($token);
     $user->setApiTokenExpireAt($expireAt);
 }