public function testCanCreateTokenResponse() { $request = $this->createMock(ServerRequestInterface::class); $client = Client::createNewClient('name', 'http://www.example.com'); $owner = $this->createMock(TokenOwnerInterface::class); $owner->expects($this->once())->method('getTokenOwnerId')->will($this->returnValue(1)); $token = AccessToken::reconstitute(['token' => 'azerty', 'owner' => $owner, 'client' => null, 'expiresAt' => (new \DateTimeImmutable())->add(new DateInterval('PT1H')), 'scopes' => []]); $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']); }
/** * @dataProvider providerReconstitute */ public function testReconstitute($data) { /** @var AccessToken $accessToken */ $accessToken = AccessToken::reconstitute($data); $this->assertEquals($data['token'], $accessToken->getToken()); $this->assertSame($data['owner'], $accessToken->getOwner()); $this->assertSame($data['client'], $accessToken->getClient()); if ($data['expiresAt'] instanceof DateTimeInterface) { /** @var DateTimeInterface $expiresAt */ $expiresAt = $data['expiresAt']; $this->assertSame($expiresAt->getTimeStamp(), $accessToken->getExpiresAt()->getTimestamp()); } else { $this->assertNull($accessToken->getExpiresAt()); } $this->assertSame($data['scopes'], $accessToken->getScopes()); }
/** * @return AccessToken */ private function getValidAccessToken(TokenOwnerInterface $owner = null, array $scopes = null) { $validDate = (new \DateTimeImmutable())->add(new DateInterval('PT1H')); $token = AccessToken::reconstitute(['token' => 'azerty_access', 'owner' => $owner, 'client' => null, 'scopes' => $scopes ?? ['read'], 'expiresAt' => $validDate]); return $token; }
public function testDoesCaseSensitiveTest() { $token = AccessToken::reconstitute(['token' => 'Token', 'owner' => $this->createMock(TokenOwnerInterface::class), 'client' => $this->createMock(Client::class), 'expiresAt' => new \DateTimeImmutable(), 'scopes' => []]); $this->tokenRepository->expects($this->once())->method('findByToken')->with('token')->will($this->returnValue($token)); $this->assertNull($this->tokenService->getToken('token')); }
/** * @dataProvider revocationProvider */ public function testReturn503IfCannotRevoke($tokenType) { $request = $this->createMock(ServerRequestInterface::class); $request->expects($this->once())->method('getParsedBody')->willReturn(['token' => 'abc', 'token_type_hint' => $tokenType]); $clientService = $this->createMock(ClientService::class); $grant = $this->createMock(GrantInterface::class); $accessTokenService = $this->createMock(AccessTokenService::class); $refreshTokenService = $this->createMock(RefreshTokenService::class); $authorizationServer = new AuthorizationServer($clientService, [$grant], $accessTokenService, $refreshTokenService); if ($tokenType === 'access_token') { $token = AccessToken::reconstitute(['token' => 'abc', 'owner' => null, 'client' => null, 'scopes' => [], 'expiresAt' => new \DateTimeImmutable()]); $accessTokenService->expects($this->once())->method('getToken')->with('abc')->will($this->returnValue($token)); $accessTokenService->expects($this->once())->method('deleteToken')->with($token)->will($this->throwException(new \RuntimeException())); } elseif ($tokenType === 'refresh_token') { $token = RefreshToken::reconstitute(['token' => 'abc', 'owner' => null, 'client' => null, 'scopes' => [], 'expiresAt' => new \DateTimeImmutable()]); $refreshTokenService->expects($this->once())->method('getToken')->with('abc')->will($this->returnValue($token)); $refreshTokenService->expects($this->once())->method('deleteToken')->with($token)->will($this->throwException(new \RuntimeException())); } $response = $authorizationServer->handleRevocationRequest($request); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(503, $response->getStatusCode()); }