Exemplo n.º 1
0
 public function testCanCheckIfATokenIsExpired()
 {
     $expiresAt = new DateTime();
     $expiresAt->sub(new DateInterval('PT60S'));
     $accessToken = new AccessToken();
     $accessToken->setExpiresAt($expiresAt);
     $this->assertTrue($accessToken->isExpired());
 }
 public function generateAccessToken(TokenOwnerInterface $user)
 {
     $token = new AccessToken();
     $token->setOwner($user);
     $token->setToken(substr(md5(rand()), 0, 7));
     $expiresAt = new DateTime();
     $token->setExpiresAt($expiresAt->modify('+1 hour'));
     $this->entityManager->persist($token);
     $this->entityManager->flush();
     return $token->getToken();
 }
 public function testCanCreateTokenResponse()
 {
     $request = $this->getMock(ServerRequestInterface::class);
     $client = new Client();
     $owner = $this->getMock(TokenOwnerInterface::class);
     $owner->expects($this->once())->method('getTokenOwnerId')->will($this->returnValue(1));
     $token = new AccessToken();
     $token->setToken('azerty');
     $token->setOwner($owner);
     $token->setExpiresAt((new DateTime())->add(new DateInterval('PT1H')));
     $this->tokenService->expects($this->once())->method('createToken')->will($this->returnValue($token));
     $response = $this->grant->createTokenResponse($request, $client, $owner);
     $body = json_decode($response->getBody(), true);
     $this->assertEquals('azerty', $body['access_token']);
     $this->assertEquals('Bearer', $body['token_type']);
     $this->assertEquals(3600, $body['expires_in']);
     $this->assertEquals(1, $body['owner_id']);
 }
 /**
  * @return AccessToken
  */
 private function getValidAccessToken()
 {
     $accessToken = new AccessToken();
     $accessToken->setToken('azerty_access');
     $accessToken->setScopes('read');
     $validDate = new DateTime();
     $validDate->add(new DateInterval('PT1H'));
     $accessToken->setExpiresAt($validDate);
     return $accessToken;
 }
 /**
  * @dataProvider requestProvider
  */
 public function testCanValidateAccessToResource($expiredToken, $tokenScope, $desiredScope, $match)
 {
     $request = $this->getMock(ServerRequestInterface::class);
     $request->expects($this->once())->method('hasHeader')->with('Authorization')->will($this->returnValue(true));
     $request->expects($this->once())->method('getHeaderLine')->will($this->returnValue('Bearer token'));
     $accessToken = new AccessToken();
     $date = new DateTime();
     if ($expiredToken) {
         $date->sub(new DateInterval('P1D'));
     } else {
         $date->add(new DateInterval('P1D'));
     }
     $accessToken->setExpiresAt($date);
     $accessToken->setScopes($tokenScope);
     $this->tokenService->expects($this->once())->method('getToken')->with('token')->will($this->returnValue($accessToken));
     if (!$match || $expiredToken) {
         $this->setExpectedException(InvalidAccessTokenException::class);
     }
     $tokenResult = $this->resourceServer->getAccessToken($request, $desiredScope);
     $this->assertInstanceOf(AccessToken::class, $tokenResult);
 }