public function testCanSetScopesFromInstances() { $scope = new Scope(); $scope->setName('bar'); $accessToken = new AccessToken(); $accessToken->setScopes([$scope]); $this->assertCount(1, $accessToken->getScopes()); }
/** * @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); }
/** * @dataProvider scopeProvider */ public function testCanSaveToken($registeredScopes, $tokenScope, $throwException) { if ($throwException) { $this->setExpectedException(OAuth2Exception::class, null, 'invalid_scope'); } $token = new AccessToken(); if (empty($tokenScope)) { $scope = new Scope(); $scope->setName('read'); $this->scopeService->expects($this->once())->method('getDefaultScopes')->will($this->returnValue([$scope])); } else { $token->setScopes($tokenScope); } if (!$throwException) { $this->tokenRepository->expects($this->once())->method('save')->with($this->isInstanceOf(AbstractToken::class)); } $scopes = []; foreach ($registeredScopes as $registeredScope) { $scope = new Scope(); $scope->setName($registeredScope); $scopes[] = $scope; } $this->scopeService->expects($this->any())->method('getAll')->will($this->returnValue($scopes)); $this->tokenService->createToken($token); $this->assertEquals(40, strlen($token->getToken())); if (empty($tokenScope)) { $this->assertCount(1, $token->getScopes()); } else { $this->assertEquals(explode(' ', $tokenScope), $token->getScopes()); } }