Ejemplo n.º 1
0
 /**
  * @param array|string $formats
  * @param string $blob
  * @return Message
  * @throws InvalidMessageException
  */
 public function decode($formats, $blob)
 {
     $formats = (array) $formats;
     $prefixLen = 0;
     foreach ($formats as $format) {
         $prefixLen = max($prefixLen, strlen($format));
     }
     list($prefix) = explode(Constants::PROTOCOL_DELIM, substr($blob, 0, $prefixLen + 1));
     if (!in_array($prefix, $formats)) {
         if (in_array(GarbledMessage::NAME, $formats)) {
             return GarbledMessage::decode($blob);
         } else {
             throw new InvalidMessageException("Unexpected message type.");
         }
     }
     switch ($prefix) {
         case StdMessage::NAME:
             return StdMessage::decode($this->cxnStore, $blob);
         case InsecureMessage::NAME:
             return InsecureMessage::decode($blob);
         case RegistrationMessage::NAME:
             return RegistrationMessage::decode($this->appStore, $blob);
         case AppMetasMessage::NAME:
             return AppMetasMessage::decode($this->certValidator, $blob);
         default:
             throw new InvalidMessageException("Unrecognized message type.");
     }
 }
 /**
  * @param Message $invalidInput
  * @throws Exception\InvalidMessageException
  * @dataProvider invalidInputExamples
  */
 public function testInvalidInput($appKeyPair, $invalidInput)
 {
     $caKeyPair = KeyPair::create();
     $this->assertNotEmpty($caKeyPair['privatekey']);
     $this->assertNotEmpty($caKeyPair['publickey']);
     $caCert = CA::create($caKeyPair, '/O=test');
     $this->assertNotEmpty($caCert);
     $appMeta = array('title' => 'My App', 'appId' => self::APP_ID, 'appCert' => CA::signCSR($caKeyPair, $caCert, CA::createAppCSR($appKeyPair, '/O=Application Provider')), 'appUrl' => 'http://app-a.com/cxn', 'perm' => array('api' => array(), 'grant' => array('view all contacts')));
     $appCxnStore = new ArrayCxnStore();
     $regServer = new RegistrationServer($appMeta, $appKeyPair, $appCxnStore);
     list($headers, $blob, $code) = $regServer->handle($invalidInput->encode())->toHttp();
     $this->assertEquals(400, $code);
     $message = InsecureMessage::decode($blob);
     $data = $message->getData();
     $this->assertEquals(1, $data['is_error']);
     $this->assertEquals('Invalid message coding', $data['error_message']);
 }
Ejemplo n.º 3
0
 public function decode($formats, $message)
 {
     $prefixLen = 0;
     foreach ($formats as $format) {
         $prefixLen = max($prefixLen, strlen($format));
     }
     list($prefix) = explode(Constants::PROTOCOL_DELIM, substr($message, 0, $prefixLen + 1));
     if (!in_array($prefix, $formats)) {
         throw new InvalidMessageException("Unexpected message type.");
     }
     switch ($prefix) {
         case StdMessage::NAME:
             return StdMessage::decode($this->cxnStore, $message);
         case InsecureMessage::NAME:
             return InsecureMessage::decode($message);
         case RegistrationMessage::NAME:
             return RegistrationMessage::decode($this->appId, $this->appPrivKey, $message);
         default:
             throw new InvalidMessageException("Unrecognized message type");
     }
 }