示例#1
0
 /**
  * Creates  a signed JWT to be pushed to the GSLS. If the personalKeyPair is not set, an exception is thrown
  * 
  * @return String The JWT
  */
 public function getJWT()
 {
     if ($this->personalKeyPair === NULL) {
         throw new \Exception("JWT cannot be built without the personalKey");
     }
     // create and sign JWT
     $signer = new Sha512();
     $personalPrivateKey = PrivateKey::formatPEM($this->personalKeyPair->getPrivateKey());
     $token = (new Builder())->set('socialRecord', base64_encode($this->socialRecord->getJSONString()))->sign($signer, $personalPrivateKey)->getToken();
     return $token->__toString();
 }
示例#2
0
 /**
  * Exports a SocialRecord object to a serialized JSONObject
  * 
  * @param SocialRecord The SocialRecord to export
  * @param KeyPair account key pair to export
  * @param KeyPair personal key pair to export
  * @return string The exported SocialRecord
  */
 public static function exportSocialRecord(SocialRecord $socialRecord, KeyPair $accountKeyPair = NULL, KeyPair $personalKeyPair = NULL)
 {
     $json = new JSONObject();
     $json->put('socialRecord', $socialRecord->getJSONObject());
     if ($accountKeyPair != NULL) {
         $json->put('accountPrivateKey', PrivateKey::exportKey($accountKeyPair->getPrivateKey()));
     }
     if ($personalKeyPair != NULL) {
         $json->put('personalPrivateKey', PrivateKey::exportKey($personalKeyPair->getPrivateKey()));
     }
     return $json->write();
 }
示例#3
0
文件: GSLS.php 项目: sgoendoer/sonic
 /**
  * Pushes an update for a SocialRecord to the GSLS. The SocialRecord will be transformed into a signed JWT, which is then stored in the GSLS
  * 
  * @param $sr The SocialRecord
  * @param $personalPrivateKey The private key to sign the JWT
  * 
  * @throws Exception
  * 
  * @return result json string
  */
 public static function putSocialRecord(SocialRecord $sr, $personalPrivateKey)
 {
     if (!$sr->verify()) {
         throw new \Excetion("Error: Invalid Social Record");
     }
     // create and sign JWT
     $signer = new Sha512();
     $personalPrivateKey = PrivateKey::formatPEM($personalPrivateKey);
     $token = (new Builder())->set('socialRecord', base64_encode($sr->getJSONString()))->sign($signer, $personalPrivateKey)->getToken();
     $ch = curl_init(Configuration::getPrimaryGSLSNode());
     if (Configuration::getCurlVerbose() >= 2) {
         curl_setopt($ch, CURLOPT_VERBOSE, 1);
     }
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, Configuration::getGSLSTimeout());
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-Length: ' . strlen((string) $token)));
     curl_setopt($ch, CURLOPT_POSTFIELDS, (string) $token);
     $result = curl_exec($ch);
     if (curl_errno($ch) != CURLE_OK) {
         $ch = curl_init(Configuration::getSecondaryGSLSNode());
         if (Configuration::getCurlVerbose() >= 2) {
             curl_setopt($ch, CURLOPT_VERBOSE, 1);
         }
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HTTPGET, 1);
         $result = curl_exec($ch);
         if (curl_errno($ch) != CURLE_OK) {
             throw new \Exception('Connection error: ' . curl_error($ch));
         }
     }
     $result = json_decode($result);
     curl_close($ch);
     if ($result->responseCode != 200) {
         throw new \Exception("Error: " . $result->message);
     } else {
         return $result;
     }
 }