/** * Creates and returns access token for a user * @param AdvancedUserInterface $user [description] * @return [type] [description] */ public function generateAccessToken(AdvancedUserInterface $user) { if (is_null($user->getOAuthClient()->getId())) { throw new \Exception('User must have an OAuth Client', 500); } // Search valid token $oauth_access_token = $this->oauth_manipulator->getValidTokenForClient($user->getOAuthClient()); if (!is_null($oauth_access_token)) { return $oauth_access_token->getToken(); } // Or else, creates a new one // Forge request to satisfy OAuth2 server $request = new Request(); $request->query->add(['client_id' => $user->getOAuthClient()->getPublicId(), 'response_type' => OAuth2::RESPONSE_TYPE_ACCESS_TOKEN, 'redirect_uri' => $user->getOAuthClient()->getRedirectUris()[0]]); $response = $this->oauth2->finishClientAuthorization(true, $user, $request, null); if ($response instanceof Response) { $location = str_replace('#', '?', $response->headers->get('location')); $query_string = parse_url($location, PHP_URL_QUERY); parse_str($query_string, $queries); if (isset($queries['access_token'])) { $access_token = $queries['access_token']; return $access_token; } } else { throw new Exception("Token creation ; unknown response type : " . get_class($response), 500); } }