/**
  * @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;
 }
Esempio 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;
 }
Esempio n. 3
0
 /**
  * @param $arrHeaders
  * @param $postData
  * @throws \RuntimeException
  * @throws \LogicException
  */
 private function prepareRequest(&$arrHeaders, &$postData)
 {
     switch ($this->authType) {
         case self::AUTH_TYPE_ACCESS_TOKEN:
             if (false == $this->accessToken) {
                 throw new \RuntimeException('Access Token must be set');
             }
             $arrHeaders[] = 'Authorization: token ' . $this->accessToken;
             break;
         case self::AUTH_TYPE_BASIC_AUTH:
             if (false == $this->clientId || false == $this->clientSecret) {
                 throw new \RuntimeException('ClientId and ClientSecret Must be set');
             }
             $this->httpClient->setCredentials($this->getClientId(), $this->getClientSecret());
             break;
         case self::AUTH_TYPE_REQUEST:
             if (false == $this->clientId || false == $this->clientSecret) {
                 throw new \RuntimeException('ClientId and ClientSecret Must be set');
             }
             $postData['client_id'] = $this->getClientId();
             $postData['client_secret'] = $this->getClientSecret();
             break;
         default:
             throw new \LogicException(sprintf("Invalid authentication type '%s'", $this->authType));
     }
 }