/** * @test */ public function checkReturnsFalseWhenKeyIsExpired() { $key = new ApiKey(); $key->setExpirationDate((new \DateTime())->sub(new \DateInterval('P1D'))); $repo = $this->prophesize(EntityRepository::class); $repo->findOneBy(['key' => '12345'])->willReturn($key)->shouldBeCalledTimes(1); $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); $this->assertFalse($this->service->check('12345')); }
/** * Creates a new ApiKey with provided expiration date * * @param \DateTime $expirationDate * @return ApiKey */ public function create(\DateTime $expirationDate = null) { $key = new ApiKey(); if (isset($expirationDate)) { $key->setExpirationDate($expirationDate); } $this->em->persist($key); $this->em->flush(); return $key; }
/** * @param ApiKey $apiKey * @return string */ protected function getEnabledSymbol(ApiKey $apiKey) { return !$apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++'; }
/** * Creates a new JSON web token por provided API key * * @param ApiKey $apiKey * @param int $lifetime * @return string */ public function create(ApiKey $apiKey, $lifetime = self::DEFAULT_LIFETIME) { $currentTimestamp = time(); return $this->encode(['iss' => $this->appOptions->__toString(), 'iat' => $currentTimestamp, 'exp' => $currentTimestamp + $lifetime, 'sub' => 'auth', 'key' => $apiKey->getId()]); }