Пример #1
0
 /**
  * Retrieves a SocialRecord for a given GlobalID from the GSLS. The signed JWT stored in the GSLS will be retrieved, the payloads verified, and the enclosed SocialRecord object will be returned.
  * 
  * @param $gid The GlobalID to resolve
  * @param $raw If set to true, the signed JWT will be returned instead of the SocialRecrod
  * 
  * @throws SocialRecordNotFoundException
  * @throws SocialRecordIntegrityException
  * @throws Exception
  * 
  * @return SocialRecord object
  */
 public static function getSocialRecord($gid, $raw = false)
 {
     $ch = curl_init(Configuration::getPrimaryGSLSNode() . '/' . $gid);
     if (Configuration::getCurlVerbose() >= 2) {
         curl_setopt($ch, CURLOPT_VERBOSE, 1);
     }
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_HTTPGET, 1);
     curl_setopt($ch, CURLOPT_TIMEOUT, Configuration::getGSLSTimeout());
     $result = curl_exec($ch);
     if (curl_errno($ch) != CURLE_OK) {
         $ch = curl_init(Configuration::getSecondaryGSLSNode() . '/' . $gid);
         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) {
         if ($result->responseCode == 404) {
             throw new SocialRecordNotFoundException($result->message);
         } else {
             throw new \Exception($result->message);
         }
     } else {
         // verify JWT and extract SocialRecord
         $signer = new Sha512();
         $token = (new Parser())->parse((string) $result->socialRecord);
         $socialRecord = json_decode(base64_decode($token->getClaim('socialRecord')));
         $personalPublicKey = PublicKey::formatPEM($socialRecord->personalPublicKey);
         try {
             $token->verify($signer, $personalPublicKey);
         } catch (\Exception $e) {
             throw new SocialRecordIntegrityException('SocialRecord integrity compromised: ' . $e->getMessage());
         }
         if ($raw) {
             return $token;
         } else {
             return SocialRecordBuilder::buildFromJSON(json_encode($socialRecord, JSON_UNESCAPED_SLASHES));
         }
     }
 }
Пример #2
0
 /**
  * Imports a SocialRecord from a string resource
  * 
  * @param $source The string to parse
  * @return array Array containting the SocialRecord object and optinally the KeyPair(s)
  */
 public static function importSocialRecord($source)
 {
     $json = new JSONObject($source);
     $socialRecord = SocialRecordBuilder::buildFromJSON($json->get('socialRecord'));
     $result = array('socialRecord' => $socialRecord);
     if ($json->has('accountPrivateKey')) {
         $result['accountKeyPair'] = new KeyPair($json->get('accountPrivateKey'), $socialRecord->getAccountPublicKey());
     }
     if ($json->has('personalPrivateKey')) {
         $result['personalKeyPair'] = new KeyPair($json->get('personalPrivateKey'), $socialRecord->getPersonalPublicKey());
     }
     return $result;
 }