-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSheerID.php
More file actions
282 lines (235 loc) · 7.26 KB
/
SheerID.php
File metadata and controls
282 lines (235 loc) · 7.26 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/*
* Copyright 2012 SheerID, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at:
*
* http://www.apache.org/licenses/LICENSE-2.0.html
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for
* the specific language governing permissions and limitations under the
* License.
*
* For more information, visit:
*
* http://developer.sheerid.com
*
*/
define('SHEERID_API_VERSION', 0.5);
define('SHEERID_ENDPOINT_SANDBOX', 'https://services-sandbox.sheerid.com');
define('SHEERID_ENDPOINT_PRODUCTION', 'https://services.sheerid.com');
class SheerID {
var $accessToken;
var $baseUrl;
var $verbose;
function SheerID($accessToken, $baseUrl=null, $verbose=false){
$this->accessToken = $accessToken;
$this->baseUrl = $baseUrl ? $baseUrl : SHEERID_ENDPOINT_SANDBOX;
$this->verbose = $verbose;
}
function listFields() {
return $this->getJson("/field");
}
function inquire($requestId) {
return $this->getJson("/verification/$requestId");
}
function verify($data, $org_id=null, $config=array()) {
$config_prefix = "_";
$post_data = array();
// add config data w/ prefix to request
foreach ($config as $k => $v) {
$post_data["${config_prefix}${k}"] = $v;
}
// add personal info fields
foreach ($data as $k => $v) {
$post_data[$k] = $v;
}
// add organization ID
if ($org_id) {
$post_data["organizationId"] = $org_id;
}
$resp = $this->post("/verification", $post_data);
return json_decode($resp["responseText"]);
}
function updateVerification($requestId, $data) {
$post_data = array();
foreach ($data as $k => $v) {
$post_data[$k] = $v;
}
$resp = $this->post("/verification/$requestId", $post_data);
return json_decode($resp["responseText"]);
}
function listOrganizations($type=null, $name=null) {
$params = array();
if ($type) {
$params["type"] = $type;
}
if ($name) {
$params["name"] = $name;
}
return $this->getJson("/organization", $params);
}
function listAffiliationTypes() {
return $this->getJson("/affiliationType");
}
function listAssets($request_id) {
try {
$resp = $this->get("/verification/${request_id}/assets");
return json_decode($resp["responseText"]);
} catch (Exception $e) {
return null;
}
}
function getAssetToken($request_id) {
try {
$resp = $this->post("/asset/token", array("requestId" => $request_id));
$json = json_decode($resp["responseText"]);
return $json->token;
} catch (Exception $e) {
return null;
}
}
function revokeToken($token) {
try {
$resp = $this->delete("/token/$token");
return $resp["status"] == 204;
} catch (Exception $e) {
return false;
}
}
/**
* Send Asset
*
* @param string $token
* @param string $file
* @return array|null
*/
public function send_asset($token, $file) {
try {
$resp = $this->post("/asset", array("token" => $token, "file" => $file));
return json_decode($resp["responseText"]);
} catch (Exception $e) {
return null;
}
}
function updateMetadata($requestId, $meta) {
try {
$this->post("/verification/${requestId}/metadata", $meta);
} catch (Exception $e) {}
}
function updateOrderId($requestId, $orderId) {
$this->updateMetadata($requestId, array("orderId" => $orderId));
}
// TODO: implement other service methods
// ...
/* helper methods */
public function getFields($affiliation_types) {
//TODO: use service
$fields = array("FIRST_NAME", "LAST_NAME");
if (array_search('STUDENT_FULL_TIME', $affiliation_types) !== FALSE || array_search('STUDENT_PART_TIME', $affiliation_types) !== FALSE || array_search('ACTIVE_DUTY', $affiliation_types) !== FALSE || array_search('VETERAN', $affiliation_types) !== FALSE) {
$fields[] = 'BIRTH_DATE';
}
if (array_search('FACULTY', $affiliation_types) !== FALSE) {
$fields[] = 'POSTAL_CODE';
}
if (array_search('VETERAN', $affiliation_types) !== FALSE) {
$fields[] = "STATUS_START_DATE";
}
return $fields;
}
public function getOrganizationType($affiliation_types) {
//TODO: improve / use service
if (array_search("ACTIVE_DUTY", $affiliation_types) !== false || array_search("VETERAN", $affiliation_types) !== false) {
return "MILITARY";
} else {
return "UNIVERSITY";
}
}
/* utility methods */
function get($path, $params=array()) {
$req = new SheerIDRequest($this->accessToken, "GET", $this->url($path), $params, $this->verbose);
return $req->execute();
}
function getJson($path, $params=array()) {
$resp = $this->get($path, $params);
return json_decode($resp["responseText"]);
}
function post($path, $params=array()) {
$req = new SheerIDRequest($this->accessToken, "POST", $this->url($path), $params, $this->verbose);
return $req->execute();
}
function delete($path) {
$req = new SheerIDRequest($this->accessToken, "DELETE", $this->url($path), array(), $this->verbose);
return $req->execute();
}
function url($path='') {
return sprintf("%s/rest/%s%s", $this->baseUrl, SHEERID_API_VERSION, $path);
}
}
class SheerIDRequest {
var $method;
var $url;
var $params;
var $headers;
var $verbose;
function SheerIDRequest($accessToken, $method, $url, $params=array(), $verbose=false) {
$this->method = $method;
$this->url = $url;
$this->params = $params;
$this->headers = array("Authorization: Bearer $accessToken");
$this->verbose = $verbose;
}
function execute() {
$ch = curl_init();
$url = $this->url;
$query = $this->getQueryString();
if ("GET" === $this->method && $query) {
$url .= "?$query";
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ("POST" === $this->method){
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, (isset($this->params['file'])) ? $this->params : $query);
if ($this->verbose) {
error_log("[SheerID] POST $url $query");
}
} else if ($this->verbose) {
error_log(sprintf("[SheerID] %s %s", $this->method, $url));
}
if ("DELETE" === $this->method) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
$data = curl_exec($ch);
if(curl_errno($ch)){
$err = curl_error($ch);
curl_close($ch);
throw new Exception($err);
} else {
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status != 200 && $status != 204) {
throw new Exception("Server returned status code: $status for url: $url");
}
$response = array(
"status" => $status,
"responseText" => $data
);
curl_close($ch);
return $response;
}
}
function getQueryString() {
$parts = array();
foreach ($this->params as $k => $v) {
$parts[] = urlencode($k) . "=" . urlencode($v);
}
return implode("&", $parts);
}
}