public function testXYZ() { $client = new Client(); $mock = new MockPlugin(); $mock->addResponse(new Response(200, null, json_encode(array("access_token" => "my_access_token", "token_type" => "BeArEr", "refresh_token" => "why_not_a_refresh_token")))); $client->addSubscriber($mock); $state = new State(array("state" => "my_state", "client_config_id" => "foo", "issue_time" => time() - 100, "user_id" => "my_user_id", "scope" => Scope::fromString("foo bar"))); $this->storage->storeState($state); $callback = new Callback("foo", $this->clientConfig[0], $this->storage, $client); $tokenResponse = $callback->handleCallback(array("state" => "my_state", "code" => "my_code")); $this->assertEquals("my_access_token", $tokenResponse->getAccessToken()); }
public function testXYZ() { $client = new Client(); $mock = new MockPlugin(); $mock->addResponse(new Response(200, null, json_encode(array('access_token' => 'my_access_token', 'token_type' => 'BeArEr', 'refresh_token' => 'why_not_a_refresh_token')))); $client->addSubscriber($mock); $state = new State(array('state' => 'my_state', 'client_config_id' => 'foo', 'issue_time' => time() - 100, 'user_id' => 'my_user_id', 'scope' => Scope::fromString('foo bar'))); $this->storage->storeState($state); $guzzle3Client = new Guzzle3Client($client); $callback = new Callback('foo', $this->clientConfig[0], $this->storage, $guzzle3Client); $tokenResponse = $callback->handleCallback(array('state' => 'my_state', 'code' => 'my_code')); $this->assertEquals('my_access_token', $tokenResponse->getAccessToken()); }
/** * Tries to authenticate a user * @param Request $request The request * @return \Exception|RedirectResponse Returns an exception when authentication fails, or a redirect response when a redirect is required * @throws \fkooman\OAuth\Client\Exception\ApiException */ public function tryAuthentication(Request $request) { $this->clientConfig->setRedirectUri($request->getUri()); if ($request->query->has('code') || $request->query->has('error')) { try { $this->callback->handleCallback($request->query->all()); } catch (AuthorizeException $ex) { return $ex; } catch (CallbackException $ex) { return $ex; } } if ($request->query->has('code')) { $request->query->remove('code'); $request->query->remove('state'); $request->server->set('QUERY_STRING', http_build_query($request->query->all())); return new RedirectResponse($request->getUri()); } if (!$this->getAccessToken()) { return new RedirectResponse($this->api->getAuthorizeUri($this->context)); } }
<?php require_once 'vendor/autoload.php'; use fkooman\OAuth\Client\Guzzle3Client; use fkooman\OAuth\Client\ClientConfig; use fkooman\OAuth\Client\SessionStorage; use fkooman\OAuth\Client\Callback; $clientConfig = new ClientConfig(array('authorize_endpoint' => 'http://localhost/php-oauth-as/authorize.php', 'client_id' => 'php-oauth-client-example', 'client_secret' => 'f00b4r', 'token_endpoint' => 'http://localhost/php-oauth-as/token.php')); try { $tokenStorage = new SessionStorage(); $httpClient = new Guzzle3Client(); $cb = new Callback('foo', $clientConfig, $tokenStorage, $httpClient); $cb->handleCallback($_GET); header('HTTP/1.1 302 Found'); header('Location: http://localhost/fkooman/php-oauth-client/example/simple/index.php'); exit; } catch (fkooman\OAuth\Client\Exception\AuthorizeException $e) { // this exception is thrown by Callback when the OAuth server returns a // specific error message for the client, e.g.: the user did not authorize // the request die(sprintf('ERROR: %s, DESCRIPTION: %s', $e->getMessage(), $e->getDescription())); } catch (Exception $e) { // other error, these should never occur in the normal flow die(sprintf('ERROR: %s', $e->getMessage())); }
/** * @throws \fkooman\OAuth\Client\Exception\AuthorizeException * @throws \fkooman\OAuth\Client\Exception\CallbackException */ public function callback() { $cb = new Callback(self::$CLIENT_ID, $this->getClientConfig(), $this->getTokenStorage(), $this->getConnection()->getClient()); $cb->handleCallback($_GET); }