-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlockstackCommon.php
More file actions
110 lines (84 loc) · 2.88 KB
/
BlockstackCommon.php
File metadata and controls
110 lines (84 loc) · 2.88 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* This class intergrates blockstack with php.
* Author: Saul Boyd (avikar.io)
* License: GPL (http://www.gnu.org/copyleft/gpl.html)
*/
class BlockstackCommon {
public function __construct() {}
// this function is to be called to verify and obtain the blockstack data
public function auth() {
$user = file_get_contents( 'php://input' );
if ( !isset( $user ) || $user === "" ) {
return $this->respond(true, "invalid post parameters");
}
$userData = json_decode( $user, true );
if ( json_last_error() != JSON_ERROR_NONE ) {
return $this->respond(true, "invalid json");
}
if ( !isset( $userData["appPrivateKey"]) || strlen( $userData["appPrivateKey"]) < 32 ) {
return $this->respond( true, "invalid key" );
}
if ( !isset( $userData["did"] ) ) {
return $this->respond( true, "missing did" );
}
// check to see if we have failed to get the name and try for the hosted profile data
// this is to fix a bug in the browser version not returning any profile data
if ( !isset( $userData["profile"]["name"] ) ) {
$profileData = $this->getProfileFromDid( $userData["did"] );
if ( $profileData ) {
$userData["profile"] = $profileData;
}
}
if ( !isset( $userData["profile"]["image"][0]["contentUrl"] ) ) {
$userData["avatarUrl"] = "https://s3.amazonaws.com/onename/avatar-placeholder.png";
}
else {
$userData["avatarUrl"] = $userData["profile"]["image"][0]["contentUrl"];
}
if ( !isset($userData["profile"]["name"] ) ) {
$userData["profile"]["name"] = "Anonymous";
}
if ( !isset($userData["profile"]["description"] ) ) {
$userData["profile"]["description"] = "";
}
$userData["password"] = hash_hmac( "sha256", $userData["appPrivateKey"], $userData["appPrivateKey"] );
$userData["id"] = substr( hash( "sha256", $userData["appPrivateKey"] ), 0, 30 );
return $this->respond( false, $userData );
}
private function respond( $error, $data ) {
return json_encode(
array(
"error" => $error,
"data" => $data
)
);
}
// Decodes the token and returns it in an array
private function decodeToken($token){
$authParts = explode( '.', $token );
if ( count($authParts) != 3 ) {
return false;
}
$authParts[0] = json_decode( base64_decode( $authParts[0] ), true );
$authParts[1] = json_decode( base64_decode( $authParts[1] ), true );
if ( json_last_error() != JSON_ERROR_NONE ) {
return false;
}
return $authParts;
}
private function getProfileFromDid( $did ) {
$profileDataPage = file_get_contents("https://gaia.blockstack.org/hub/" . $did . "/profile.json");
if ( !isset( $profileDataPage ) ) {
return false;
}
$profileData = json_decode( $profileDataPage, true );
if ( json_last_error() != JSON_ERROR_NONE ) {
return false;
}
if ( !isset( $profileData[0]["decodedToken"]["payload"]["claim"] ) ) {
return false;
}
return $profileData[0]["decodedToken"]["payload"]["claim"];
}
}