forked from HubSpot/haPiHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.hubspotmarketplace.php
196 lines (170 loc) · 5.8 KB
/
class.hubspotmarketplace.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
class HubSpotMarketplace{
protected $appSecret;
protected $marketplaceSignature;
protected $marketplaceCaller;
protected $marketplaceUserId;
protected $marketplaceUserEmail;
protected $marketplaceUserFirstName;
protected $marketplaceUserLastName;
protected $marketplaceUserGlobal;
protected $marketplacePortalId;
protected $marketplaceAppName;
protected $marketplaceAppCallbackURL;
protected $marketplaceAppPageURL;
protected $marketplaceAppCanvasURL;
protected $marketplaceAccessToken;
protected $marketplaceAccessExpires;
protected $marketplaceRefreshToken;
/**
* Constructor.
*
* @param $request: The $_REQUEST array from the HTTP request
* @param $appSecret: Your app secret key
**/
function __construct($request, $appSecret) {
$this->appSecret = $appSecret;
$this->marketplaceSignature = $request['hubspot_marketplace_signature'];
$this->marketplaceCaller = $request['hubspot_marketplace_caller'];
$this->marketplaceUserId = $request['hubspot_marketplace_user_id'];
$this->marketplaceUserEmail = $request['hubspot_marketplace_user_email'];
$this->marketplaceUserFirstName = $request['hubspot_marketplace_user_first_name'];
$this->marketplaceUserLastName = $request['hubspot_marketplace_user_last_name'];
$this->marketplaceUserGlobal = $request['hubspot_marketplace_user_global'];
$this->marketplacePortalId = $request['hubspot_marketplace_portal_id'];
$this->marketplaceAppName = $request['hubspot_marketplace_app_name'];
$this->marketplaceAppCallbackURL = $request['hubspot_marketplace_app_callbackUrl'];
$this->marketplaceAppPageURL = $request['hubspot_marketplace_app_pageUrl'];
$this->marketplaceAppCanvasURL = $request['hubspot_marketplace_canvasUrl'];
$this->marketplaceAccessToken = $request['hubspot_marketplace_accessToken'];
$this->marketplaceAccessExpires = $request['hubspot_marketplace_accessExpires'];
$this->marketplaceRefreshToken = $request['hubspot_marketplace_refreshToken'];
}
/**
* Verifies that request is from HubSpot Marketplace
*
* @returns boolean true if request is verfied, false if not verified
**/
public function verifyRequest() {
return $this->parseSignedRequest($this->marketplaceSignature);
}
/**
* @returns String value of hubspot_marketplace_caller
**/
public function getCaller() {
return $this->marketplaceCaller;
}
/**
* @returns String value of hubspot_marketplace_user_id
**/
public function getUserId() {
return $this->marketplaceUserId;
}
/**
* @returns String value of hubspot_marketplace_user_email
**/
public function getUserEmail() {
return $this->marketplaceUserEmail;
}
/**
* @returns String value of hubspot_marketplace_user_first_name
**/
public function getUserFirstName() {
return $this->marketplaceUserFirstName;
}
/**
* @returns String value of hubspot_marketplace_user_last_name
**/
public function getUserLastName() {
return $this->marketplaceUserLastName;
}
/**
* @returns Boolean value of hubspot_marketplace_user_global
**/
public function getUserGlobal() {
if ($this->marketplaceUserGlobal == 'true') {
return true;
} else {
return false;
}
}
/**
* @returns String value of hubspot_marketplace_portal_id
**/
public function getPortalId() {
return $this->marketplacePortalId;
}
/**
* @returns String value of hubspot_marketplace_app_name
**/
public function getAppName() {
return $this->marketplaceAppName;
}
/**
* @returns String value of hubspot_marketplace_app_callbackUrl
**/
public function getAppCallbackURL() {
return $this->marketplaceAppCallbackURL;
}
/**
* @returns String value of hubspot_marketplace_app_pageUrl
**/
public function getAppPageURL() {
return $this->marketplaceAppPageURL;
}
/**
* @returns String value of hubspot_marketplace_app_canvasUrl
**/
public function getAppCanvasURL() {
return $this->marketplaceAppCanvasURL;
}
/**
* @returns String value of hubspot_marketplace_accessToken
**/
public function getAccessToken() {
return $this->marketplaceAccessToken;
}
/**
* @returns String value of hubspot_marketplace_accessExpires
**/
public function getAccessExpires() {
return $this->marketplaceAccessExpires;
}
/**
* @returns String value of hubspot_marketplace_refreshToken
**/
public function getRefreshToken() {
return $this->marketplaceRefreshToken;
}
/**
* Parses and decodes hubspot_marketplace_signature to verify
* that request is from HubSpot
*
* @param $marketplaceSignature: The encoded signature from $_REQUEST['hubspot_marketplace_signature']
*
* @returns boolean true if request is verified, false if not verified
**/
protected function parseSignedRequest($marketplaceSignature) {
list($encoded_sig, $payload) = explode('.', $marketplaceSignature, 2);
// decode the data
$sig = $this->base64UrlDecode($encoded_sig);
$data = $this->base64UrlDecode($payload);
// check sig
$expected_sig = hash_hmac('sha1', $data,
$this->appSecret, $raw = true);
if ($sig != $expected_sig) {
return false;
}
return true;
}
/**
* Decodes base64 encoded data
*
* @param $input: base 64 encoded string
*
* @returns decoded string
**/
protected function base64UrlDecode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
}