-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateNewSocialRecordExample.php
More file actions
79 lines (62 loc) · 3.16 KB
/
Copy pathCreateNewSocialRecordExample.php
File metadata and controls
79 lines (62 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php namespace sgoendoer\Sonic\examples;
require_once(__DIR__ . '/../vendor/autoload.php');
use sgoendoer\Sonic\Crypt\KeyPair;
use sgoendoer\Sonic\Crypt\Random;
use sgoendoer\Sonic\Identity\EntityAuthData;
use sgoendoer\Sonic\Identity\SocialRecord;
use sgoendoer\Sonic\Identity\SocialRecordBuilder;
use sgoendoer\Sonic\Identity\SocialRecordManager;
try
{
//////////////////////////////////////////////////////////////////////////////
// create a new SocialRecord for a platform
//////////////////////////////////////////////////////////////////////////////
// create new keypairs
$personalKeyPairPlatform = new KeyPair();
$accountKeyPairPlatform = new KeyPair();
// use SocialRecordBuilder to create new SocialRecord object
$socialRecordPlatform = (new SocialRecordBuilder())
->type(SocialRecord::TYPE_PLATFORM)
->salt(Random::getRandom())
->accountPublicKey($accountKeyPairPlatform->getPublicKey())
->personalPublicKey($personalKeyPairPlatform->getPublicKey())
->displayName('Platform A')
->profileLocation('http://sonic-project.net/sonic/')
->build();
echo "Your new platform SocialRecord is:\n----------\n" . $socialRecordPlatform . "\n----------\n";
//////////////////////////////////////////////////////////////////////////////
// create a new SocialRecord for a user Alice
//////////////////////////////////////////////////////////////////////////////
// create new keypairs
$personalKeyPair = new KeyPair();
$accountKeyPair = new KeyPair();
// use SocialRecordBuilder to create new SocialRecord object
$socialRecord = (new SocialRecordBuilder())
->type(SocialRecord::TYPE_USER)
->salt(Random::getRandom())
->platformGID($socialRecordPlatform->getGlobalID()) // using the platform SocialRecord's GlobalID
->accountPublicKey($accountKeyPair->getPublicKey())
->personalPublicKey($personalKeyPair->getPublicKey())
->displayName('Alice')
->profileLocation('http://sonic-project.net/sonic/alice/')
->build();
echo "Your new user SocialRecord is:\n----------\n" . $socialRecord . "\n----------\n";
// export the Social Record instance with keys to a JSONObject
$exportedFull = SocialRecordManager::exportSocialRecord($socialRecord, $accountKeyPair, $personalKeyPair);
// export only the public part (no keys)
$exportedPublic = SocialRecordManager::exportSocialRecord($socialRecord);
// in order to upload the SocialRecord to the GSLS, it needs to be passed as a EntityAuthData object. EntityAuthData objects are containers for data and keypairs.
$entityAuthData = new EntityAuthData($socialRecord, $accountKeyPair, $personalKeyPair);
// the data stored in the GSLS is the SocialRecord formatted as a signed JWT. In case you need to access it, you can do it via
$rawJWT = $entityAuthData->getJWT();
echo "The JWT for your user SocialRecord is:\n----------\n" . $rawJWT . "\n----------\n";
// upload to GSLS
SocialRecordManager::pushToGSLS($entityAuthData);
// retrieve SocialRecord from GSLS while ignoring local caches
$retrievedSocialRecord = SocialRecordManager::retrieveSocialRecord($socialRecord->getGlobalID(), true);
}
catch (\Exception $e)
{
echo "There has been an error: " . $e->getMessage() . "\n\n" . $e->getTraceAsString();
}
?>