Created
October 18, 2018 22:00
-
-
Save vzool/68fad0d0d116aa9968f2865ece371c12 to your computer and use it in GitHub Desktop.
Publish Client to chronicle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $root = \dirname(__DIR__); | |
| /** @psalm-suppress UnresolvableInclude */ | |
| require_once $root . '/cli-autoload.php'; | |
| use GuzzleHttp\Client; | |
| use ParagonIE\Sapient\Sapient; | |
| use ParagonIE\Chronicle\Chronicle; | |
| use ParagonIE\ConstantTime\Base64UrlSafe; | |
| use ParagonIE\Sapient\Adapter\Guzzle as GuzzleAdapter; | |
| use ParagonIE\Sapient\CryptographyKeys\SigningPublicKey; | |
| use ParagonIE\Sapient\CryptographyKeys\SigningSecretKey; | |
| use ParagonIE\Sapient\Exception\InvalidMessageException; | |
| $http = new Client([ | |
| 'base_uri' => 'http://localhost:8080' | |
| ]); | |
| $sapient = new Sapient(new GuzzleAdapter($http)); | |
| // Keys | |
| $clientSigningKey = new SigningSecretKey( | |
| Base64UrlSafe::decode( | |
| "Rjeuq_q-ZeiiLmq-vDaqsVUagCs8KFrRpuPCzQJXv_Yp2Qe9fznnHZKamsriq443ajWfg2InfSGZBV99x9GKpA==" | |
| ) | |
| ); | |
| $serverPublicKey = new SigningPublicKey( | |
| Base64UrlSafe::decode( | |
| "KdkHvX855x2SmprK4quON2o1n4NiJ30hmQVffcfRiqQ=" | |
| ) | |
| ); | |
| // We use an array to define our message | |
| $myMessage = [ | |
| 'date' => (new DateTime)->format(DateTime::ATOM), | |
| 'body' => [ | |
| uniqid('test_') => 'hello ' . microtime(1) . ' world!' | |
| ] | |
| ]; | |
| // Create the signed request: | |
| $request = $sapient->createSignedJsonRequest( | |
| 'POST', | |
| '/chronicle/publish', | |
| $myMessage, | |
| $clientSigningKey, | |
| [ | |
| Chronicle::CLIENT_IDENTIFIER_HEADER => 'mvCPXgNb81PyucAiOGXN7EeuFepwfZPP' | |
| ] | |
| ); | |
| print_r($request); | |
| $response = $http->send($request); | |
| print_r($response); | |
| try { | |
| /** @var array $verifiedResponse */ | |
| $verifiedResponse = $sapient->decodeSignedJsonResponse( | |
| $response, | |
| $serverPublicKey | |
| ); | |
| echo "DONE"; | |
| } catch (InvalidMessageException $ex) { | |
| // echo $ex->getMessage(); | |
| \http_response_code(500); | |
| exit; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment