/** * Create a new token (and generate the token) * * @param TokenOwnerInterface $owner * @param Client $client * @param string|string[]|Scope[] $scopes * @return AccessToken * @throws OAuth2Exception */ public function createToken($owner, $client, $scopes) : AccessToken { if (empty($scopes)) { $scopes = $this->scopeService->getDefaultScopes(); } else { $this->validateTokenScopes($scopes); } do { $token = AccessToken::createNewAccessToken($this->serverOptions->getAccessTokenTtl(), $owner, $client, $scopes); } while ($this->tokenRepository->tokenExists($token->getToken())); return $this->tokenRepository->save($token); }
public function testIsValid() { $accessToken = AccessToken::createNewAccessToken(60, null, null, 'read write'); $this->assertTrue($accessToken->isValid('read')); $accessToken = AccessToken::createNewAccessToken(-60, null, null, 'read write'); $this->assertFalse($accessToken->isValid('read')); $accessToken = AccessToken::createNewAccessToken(60, null, null, 'read write'); $this->assertFalse($accessToken->isValid('delete')); }
/** * @dataProvider requestProvider */ public function testCanValidateAccessToResource($expiredToken, $tokenScope, $desiredScope, $match) { $request = $this->createMock(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')); if ($expiredToken) { $accessToken = AccessToken::createNewAccessToken(-3600, null, null, $tokenScope); } else { $accessToken = AccessToken::createNewAccessToken(3600, null, null, $tokenScope); } $this->tokenService->expects($this->once())->method('getToken')->with('token')->will($this->returnValue($accessToken)); if (!$match || $expiredToken) { $this->expectException(InvalidAccessTokenException::class); } $tokenResult = $this->resourceServer->getAccessToken($request, $desiredScope); $this->assertInstanceOf(AccessToken::class, $tokenResult); }