示例#1
0
 /**
  * Do authenticate using xAuth with username and password.
  *
  * @param string $username
  * @param string $password
  * @param string $method
  *        	HTTP method
  * @param array $params
  *        	Request body if applicable (an associative array will
  *        	automatically be converted into a urlencoded body)
  * @param array $extraHeaders
  *        	Extra headers if applicable. These will override service-specific
  *        	any defaults.
  *
  * @return string
  */
 public function auth($username, $password, $login = true, $method = 'GET', $params = null, array $extraHeaders = [])
 {
     $body = [];
     // Check username/password arguments
     if (empty($username)) {
         throw new \InvalidArgumentException('x_auth_username is required');
     }
     if (empty($password)) {
         throw new \InvalidArgumentException('x_auth_password is required');
     }
     $body = array_merge($body, is_array($params) ? $params : array());
     if (!isset($body['x_auth_mode'])) {
         $body['x_auth_mode'] = 'client_auth';
     }
     // Unset duplicate
     unset($body['x_auth_username']);
     unset($body['x_auth_password']);
     $body['x_auth_username'] = $username;
     $body['x_auth_password'] = $password;
     // Set up web device
     $body['device_name'] = $_SERVER['SERVER_NAME'] . ' Website';
     $body['kind'] = 'website|' . $_SERVER['SERVER_NAME'];
     $platformName = 'platform_name=' . php_uname('s') . ' PHP ' . phpversion();
     $platformVersion = 'platform_version=' . php_uname('v');
     $productName = 'product_name=' . 'PHP ' . phpversion();
     $productModel = 'product_model=' . $_SERVER['SERVER_SOFTWARE'];
     $body['info'] = implode('|', [$platformName, $platformVersion, $productName, $productModel]);
     // Info
     $body['info'] = base64_encode($body['info']);
     // Using app.key as fingerprint
     $body['fingerprint'] = base64_encode(sha1($body['info'], true));
     try {
         $response = $this->requestZeroLeg($this->getAuthUri(), 'POST', $body);
         $token = self::parseAccessTokenResponse($response->getContent());
         // Login
         if ($login) {
             $this->storage->storeAccessToken($this->getServiceName(), $token);
         }
         return $response;
     } catch (ResponseException $re) {
         $e = new TokenResponseException(sprintf('Error in retrieving token. %s', $re->getMessage()));
         $e->setResponse($re->getResponse());
         throw $e;
     }
 }
示例#2
0
 /**
  *
  * @param unknown $token
  * @param unknown $verifier
  * @param string $tokenSecret
  * @throws \Gponster\OAuth\Exception\TokenResponseException
  * @return \Gponster\OAuth\TokenInterface
  */
 public function requestAccessToken($token, $verifier, $tokenSecret = null)
 {
     if (is_null($tokenSecret)) {
         $storedRequestToken = $this->storage->retrieveAccessToken($this->getServiceName());
         $tokenSecret = $storedRequestToken->getRequestTokenSecret();
     }
     $this->signature->setTokenSecret($tokenSecret);
     $extraAuthenticationHeaders = ['oauth_token' => $token];
     $bodyParams = ['oauth_verifier' => $verifier];
     $authorizationHeader = ['Authorization' => $this->buildAuthorizationHeaderForApiRequest('POST', $this->getAccessTokenUri(), $this->storage->retrieveAccessToken($this->getServiceName()), $bodyParams)];
     $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders());
     try {
         $response = $this->httpClient->retrieveResponse($this->getAccessTokenUri(), $bodyParams, $headers);
         $token = $this->parseAccessTokenResponse($response->getContent());
         $this->storage->storeAccessToken($this->getServiceName(), $token);
         return $token;
     } catch (ResponseException $re) {
         $e = new TokenResponseException(sprintf('Error in retrieving token. The HTTP status code %s', $code));
         $e->setResponse($re->getResponse());
         throw $e;
     }
 }