Exemplo n.º 1
0
 /**
  * @param string $binding
  * @param MethodJwt $jwt
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return MethodJwt
  */
 public function send($binding, MethodJwt $jwt)
 {
     $this->checkBinding($binding);
     $token = $this->encoder->encode($jwt, $this->key, $this->getAlgorithm());
     if ($binding == JwtBindingTypes::HTTP_POST) {
         $response = $this->httpClient->post($this->targetUrl, array(), array('jwt' => $token));
     } else {
         if ($binding == JwtBindingTypes::HTTP_REDIRECT) {
             $response = $this->httpClient->get($this->getRedirectUrl(), array('jwt' => $token));
         } else {
             throw new \InvalidArgumentException('Unsupported or invalid binding ' . $binding);
         }
     }
     $statusCode = $this->httpClient->getStatusCode();
     if ($statusCode != HttpStatusCode::OK) {
         throw new \RuntimeException(sprintf('API error: %s %s', $statusCode, $response));
     }
     $resultJwt = null;
     if ($response) {
         try {
             $result = $this->encoder->decode($response, $this->key);
             $resultJwt = new MethodJwt($result->getHeader(), $result->getPayload());
         } catch (\Exception $ex) {
         }
     }
     if (!$resultJwt) {
         $resultJwt = new MethodJwt();
     }
     if ($ex = $resultJwt->getException()) {
         throw new RemoteMethodException($ex);
     }
     return $resultJwt;
 }
Exemplo n.º 2
0
 /**
  * @param string $code
  * @throws \Symfony\Component\HttpKernel\Exception\HttpException
  * @return GithubAccessResponse
  */
 public function getAccessTokenFromCode($code)
 {
     $json = $this->httpClient->post('https://github.com/login/oauth/access_token', array(), array('client_id' => $this->clientId, 'client_secret' => $this->secret, 'code' => $code), null, array('Accept: application/json'));
     if ($this->httpClient->getStatusCode() != HttpStatusCode::OK) {
         throw new HttpException($this->httpClient->getStatusCode(), $this->httpClient->getErrorText() . ' : ' . $json);
     }
     /** @var GithubAccessResponse $result */
     $result = GithubAccessResponse::deserialize($json);
     if (!$result) {
         new \LogicException('Unable to parse github response: ' . $json);
     }
     if ($result->error) {
         throw new HttpException(HttpStatusCode::INTERNAL_SERVER_ERROR, $result->error . ': ' . $result->error_description);
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  * @param string $url
  * @param string $method
  * @param array $queryData
  * @param array $postData
  * @param null $contentType
  * @param array $arrHeaders
  * @return string
  * @throws \LogicException
  * @throws HttpException
  */
 protected function makeRequest($url, $method = null, array $queryData = array(), array $postData = array(), $contentType = null, array $arrHeaders = array())
 {
     if (null == $method) {
         if ($this->authType == self::AUTH_TYPE_REQUEST) {
             $method = 'post';
         } else {
             $method = 'get';
         }
     }
     $this->prepareRequest($arrHeaders, $postData);
     switch ($method) {
         case 'post':
             $json = $this->httpClient->post($url, $queryData, $postData, $contentType, $arrHeaders);
             break;
         case 'get':
             $json = $this->httpClient->get($url, $queryData, $arrHeaders);
             break;
         case 'delete':
             $json = $this->httpClient->delete($url, $queryData, $arrHeaders);
             break;
         default:
             throw new \LogicException(sprintf("Unsupported HTTP method '%s'", $method));
     }
     if ($this->httpClient->getStatusCode() != HttpStatusCode::OK) {
         throw new HttpException($this->httpClient->getStatusCode(), $json);
     }
     return $json;
 }