/**
  * @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;
 }
 /**
  * @param \Exception $exception
  * @param JwtContext $context
  * @return void
  */
 public function handle(\Exception $exception, JwtContext $context)
 {
     $requestJwt = $context->getRequestJwt();
     if (!$requestJwt || $requestJwt->getDirection() == Directions::RESPONSE) {
         return;
     }
     $responseJwt = MethodJwt::create(Directions::RESPONSE, $context->getMyIssuerId(), $requestJwt->getMethod(), $requestJwt->getInstance(), null, $requestJwt->getJwtId());
     $responseJwt->setException($exception->getMessage());
     $context->setResponseJwt($responseJwt);
 }
 /**
  * @test
  */
 public function shouldCreate()
 {
     $methodJwt = MethodJwt::create($direction = Directions::REQUEST, $issuer = 'issuer', $method = 'method', $instance = 'instance', $data = array(1, 2, 3), $inResponseTo = 'foo');
     $this->assertEquals($direction, $methodJwt->getDirection());
     $this->assertEquals($issuer, $methodJwt->getIssuer());
     $this->assertEquals($method, $methodJwt->getMethod());
     $this->assertEquals($instance, $methodJwt->getInstance());
     $this->assertEquals($data, $methodJwt->getData());
     $this->assertEquals($inResponseTo, $methodJwt->getInResponseTo());
 }
 /**
  * @param JwtContext $context
  * @throws \BWC\Component\JwtApiBundle\Error\JwtException
  */
 public function handleContext(JwtContext $context)
 {
     if ($context->getResponseJwt() || $context->optionGet(ContextOptions::HANDLED)) {
         return;
     }
     if ($this->logger) {
         $this->logger->debug('UnhandledContextHandler', array('context' => $context));
     }
     $message = sprintf("Unhandled request for direction '%s' method '%s' of issuer '%s'", $context->getRequestJwt()->getDirection(), $context->getRequestJwt()->getMethod(), $context->getRequestJwt()->getIssuer());
     $requestJwt = $context->getRequestJwt();
     if ($requestJwt->getDirection() == Directions::RESPONSE) {
         throw new JwtException($message);
     }
     $responseJwt = MethodJwt::create(Directions::RESPONSE, $context->getMyIssuerId(), $requestJwt->getMethod(), $requestJwt->getInstance(), null, $requestJwt->getJwtId());
     $responseJwt->setException($message);
     $context->setResponseJwt($responseJwt);
 }