/** * {@inheritDoc} */ public function createTokenResponse(Request $request, Client $client = null, TokenOwnerInterface $owner = null) { $token = $request->getPost('access_token'); $scope = $request->getPost('scope'); if (null === $token) { throw OAuth2Exception::invalidRequest('Missing parameter access_token'); } $owner = $this->getOwner($token); if (!$owner instanceof TokenOwnerInterface) { throw OAuth2Exception::accessDenied('Unable to load user from this token'); } /** * @var AccessToken $accessToken * @var null|RefreshToken $refreshToken * */ $accessToken = new AccessToken(); $refreshToken = null; // Generate token $this->populateToken($accessToken, $client, $owner, $scope); $accessToken = $this->accessTokenService->createToken($accessToken); // Before generating a refresh token, we must make sure the authorization server supports this grant if ($this->authorizationServer->hasGrant(RefreshTokenGrant::GRANT_TYPE)) { $refreshToken = new RefreshToken(); $this->populateToken($refreshToken, $client, $owner, $scope); $refreshToken = $this->refreshTokenService->createToken($refreshToken); } return $this->prepareTokenResponse($accessToken, $refreshToken); }
/** * {@inheritDoc} */ public function createTokenResponse(ServerRequestInterface $request, Client $client = null, TokenOwnerInterface $owner = null) : ResponseInterface { $postParams = $request->getParsedBody(); // Everything is okey, we can start tokens generation! $scope = $postParams['scope'] ?? null; $accessToken = new AccessToken(); $this->populateToken($accessToken, $client, $owner, $scope); $accessToken = $this->accessTokenService->createToken($accessToken); return $this->prepareTokenResponse($accessToken); }
/** * {@inheritDoc} * @throws OAuth2Exception */ public function createTokenResponse(ServerRequestInterface $request, Client $client = null, TokenOwnerInterface $owner = null) : ResponseInterface { $postParams = $request->getParsedBody(); $code = $postParams['code'] ?? null; if (null === $code) { throw OAuth2Exception::invalidRequest('Could not find the authorization code in the request'); } /* @var \ZfrOAuth2\Server\Entity\AuthorizationCode $authorizationCode */ $authorizationCode = $this->authorizationCodeService->getToken($code); if (null === $authorizationCode || $authorizationCode->isExpired()) { throw OAuth2Exception::invalidGrant('Authorization code cannot be found or is expired'); } $clientId = $postParams['client_id'] ?? null; if ($authorizationCode->getClient()->getId() !== $clientId) { throw OAuth2Exception::invalidRequest('Authorization code\'s client does not match with the one that created the authorization code'); } // If owner is null, we reuse the same as the authorization code $owner = $owner ?: $authorizationCode->getOwner(); // Everything is okey, let's start the token generation! $scopes = $authorizationCode->getScopes(); // reuse the scopes from the authorization code $accessToken = new AccessToken(); $this->populateToken($accessToken, $client, $owner, $scopes); $accessToken = $this->accessTokenService->createToken($accessToken); // Before generating a refresh token, we must make sure the authorization server supports this grant $refreshToken = null; if ($this->authorizationServer->hasGrant(RefreshTokenGrant::GRANT_TYPE)) { $refreshToken = new RefreshToken(); $this->populateToken($refreshToken, $client, $owner, $scopes); $refreshToken = $this->refreshTokenService->createToken($refreshToken); } return $this->prepareTokenResponse($accessToken, $refreshToken); }
/** * {@inheritDoc} */ public function createTokenResponse(ServerRequestInterface $request, Client $client = null, TokenOwnerInterface $owner = null) : ResponseInterface { $postParams = $request->getParsedBody(); $refreshToken = $postParams['refresh_token'] ?? null; if (null === $refreshToken) { throw OAuth2Exception::invalidRequest('Refresh token is missing'); } // We can fetch the actual token, and validate it /** @var RefreshToken $refreshToken */ $refreshToken = $this->refreshTokenService->getToken($refreshToken); if (null === $refreshToken || $refreshToken->isExpired()) { throw OAuth2Exception::invalidGrant('Refresh token is expired'); } // We can now create a new access token! First, we need to make some checks on the asked scopes, // because according to the spec, a refresh token can create an access token with an equal or lesser // scope, but not more $scopes = $postParams['scope'] ?? $refreshToken->getScopes(); if (!$refreshToken->matchScopes($scopes)) { throw OAuth2Exception::invalidScope('The scope of the new access token exceeds the scope(s) of the refresh token'); } $owner = $refreshToken->getOwner(); $accessToken = new AccessToken(); $this->populateToken($accessToken, $client, $owner, $scopes); /** @var AccessToken $accessToken */ $accessToken = $this->accessTokenService->createToken($accessToken); // We may want to revoke the old refresh token if ($this->rotateRefreshTokens) { $this->refreshTokenService->deleteToken($refreshToken); $refreshToken = new RefreshToken(); $this->populateToken($refreshToken, $client, $owner, $scopes); $refreshToken = $this->refreshTokenService->createToken($refreshToken); } // We can generate the response! return $this->prepareTokenResponse($accessToken, $refreshToken, true); }
/** * {@inheritDoc} * @throws OAuth2Exception */ public function createTokenResponse(ServerRequestInterface $request, Client $client = null, TokenOwnerInterface $owner = null) : ResponseInterface { $postParams = $request->getParsedBody(); // Validate the user using its username and password $username = $postParams['username'] ?? null; $password = $postParams['password'] ?? null; $scope = $postParams['scope'] ?? null; if (null === $username || null == $password) { throw OAuth2Exception::invalidRequest('Username and/or password is missing'); } $callback = $this->callback; $owner = $callback($username, $password); if (!$owner instanceof TokenOwnerInterface) { throw OAuth2Exception::accessDenied('Either username or password are incorrect'); } // Everything is okay, we can start tokens generation! $accessToken = new AccessToken(); $this->populateToken($accessToken, $client, $owner, $scope); $accessToken = $this->accessTokenService->createToken($accessToken); // Before generating a refresh token, we must make sure the authorization server supports this grant $refreshToken = null; if ($this->authorizationServer->hasGrant(RefreshTokenGrant::GRANT_TYPE)) { $refreshToken = new RefreshToken(); $this->populateToken($refreshToken, $client, $owner, $scope); $refreshToken = $this->refreshTokenService->createToken($refreshToken); } return $this->prepareTokenResponse($accessToken, $refreshToken); }
public function testCreateNewTokenUntilOneDoesNotExist() { $token = new AccessToken(); $this->scopeService->expects($this->once())->method('getDefaultScopes')->will($this->returnValue(['read'])); $this->tokenRepository->expects($this->at(0))->method('findByToken')->with($this->isType('string'))->will($this->returnValue(new AccessToken())); $this->tokenRepository->expects($this->at(1))->method('findByToken')->with($this->isType('string'))->will($this->returnValue(null)); $this->tokenService->createToken($token); $this->assertEquals(40, strlen($token->getToken())); }