Exemplo n.º 1
0
 /**
  * @covers OAuth2\Client\AccessToken::__construct()
  */
 public function testConstructorBuildsAccessToken()
 {
     // assigns client and token
     $this->assertEquals($this->client, $this->accessToken->getClient());
     $this->assertEquals($this->token, $this->accessToken->getToken());
     // assigns extra params
     $target = new \OAuth2\Client\AccessToken($this->client, $this->token, array('foo' => 'bar'));
     $this->assertArrayHasKey('foo', $target->getParams());
     $this->assertEquals('bar', $target->getParam('foo'));
     // initialize with a Hash
     $hash = array('access_token' => $this->token, 'expires_in' => time() + 200, 'foo' => 'bar');
     $target = \OAuth2\Client\AccessToken::fromHash($this->client, $hash);
     $this->assertInitializeToken($target);
     // initalizes with a form-urlencoded key/value string
     $kvform = "access_token={$this->token}&expires_in={time() + 200}&foo=bar";
     $target = \OAuth2\Client\AccessToken::fromKvform($this->client, $kvform);
     $this->assertInitializeToken($target);
     // sets options
     $target = new \OAuth2\Client\AccessToken($this->client, $this->token, array('param_name' => 'foo', 'header_format' => 'Bearer %', 'mode' => 'body'));
     $this->assertEquals('foo', $target->options['param_name']);
     $this->assertEquals('Bearer %', $target->options['header_format']);
     $this->assertEquals('body', $target->options['mode']);
 }
Exemplo n.º 2
0
 /**
  * Initializes an AccessToken from a key/value application/x-www-form-urlencoded string
  *
  * @param  \OAuth2\Client\Client $client The OAuth2::Client instance
  * @param  string         $kvform The application/x-www-form-urlencoded string
  * @return \OAuth2\Client\AccessToken
  */
 public static function fromKvform($client, $kvform)
 {
     // Parse key/value application/x-www-form-urlencoded string into a hash
     parse_str($kvform, $hash);
     return \OAuth2\Client\AccessToken::fromHash($client, $hash);
 }
Exemplo n.º 3
0
 /**
  * Initializes an AccessToken by making a request to the token endpoint
  *
  * @param  array $params An array of params for the token endpoint
  * @param  array $access Token options, to pass to the AccessToken object
  * @return \OAuth2\Client\AccessToken
  */
 public function getToken($params = array(), $tokenOpts = array())
 {
     // Get parse mode for the response
     $parseMode = isset($params['parse']) ? $params['parse'] : 'automatic';
     unset($params['parse']);
     if ($this->options['token_method'] === 'POST') {
         $opts['headers'] = array('Content-Type' => 'x-www-form-urlencoded');
         $opts['body'] = $params;
     } else {
         $opts['query'] = $params;
     }
     // Create request
     $request = $this->createRequest($this->options['token_method'], $this->tokenUrl(), $opts);
     // Set auth
     if (isset($this->options['client_auth'])) {
         if ($this->options['client_auth'] === 'header') {
             $request->setHeader('Authorization', 'Basic ' . base64_encode("{$this->id}:{$this->secret}"));
         } else {
             if ($this->options['client_auth'] === 'query') {
                 $request->getQuery()->merge(['client_id' => $this->id, 'client_secret' => $this->secret]);
             } else {
                 if ($this->options['client_auth'] === 'body') {
                     // Retrieve current body as a \Guzzle\Query object since we'll have to add client auth
                     $body = \GuzzleHttp\Query::fromString((string) $request->getBody());
                     // Add client auth
                     $body->merge(['client_id' => $this->id, 'client_secret' => $this->secret]);
                     // Replace body
                     $request->setBody(\GuzzleHttp\Stream\Stream::factory((string) $body));
                 } else {
                     throw new \Exception("Unknown client authentication method.");
                 }
             }
         }
     } else {
         throw new \Exception("Missing client authentication method.");
     }
     // Get response
     $response = $this->getResponse($request, $parseMode);
     // Handle response
     $parsedResponse = $response->parse();
     if (!is_array($parsedResponse) && !isset($parsedResponse['access_token'])) {
         throw new \OAuth2\Client\Error($response);
     }
     // Return access token
     return \OAuth2\Client\AccessToken::fromHash($this, array_merge($parsedResponse, $tokenOpts));
 }