-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
229 lines (204 loc) · 8.98 KB
/
index.php
File metadata and controls
229 lines (204 loc) · 8.98 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
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
// Load environment variables from .env file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$defaultHeader = [
'Accept' => 'application/json',
'Content-Type' => "application/json",
'api-version' => '1.0',
'x-api-key' => $_ENV['API_KEY']
];
// Call Incode's `omni/start` API to create an Incode session which will include a
// token in the JSON response.
function start() {
global $defaultHeader;
$body = [
'configurationId' => $_ENV['FLOW_ID'],
'countryCode' => 'ALL',
'language' => 'en-US',
// 'redirectionUrl' => 'https://example.com?custom_parameter=some+value',
// 'externalCustomerId' => 'the id of the customer in your system',
];
$Client = new Client(['base_uri' => $_ENV['API_URL']]);
$Request = new Request('POST', '/omni/start', $defaultHeader, json_encode($body));
$Response = $Client->sendAsync($Request)->wait();
$Data = json_decode($Response->getBody());
$session = ['token'=> $Data->token, 'interviewId' => $Data->interviewId];
echo json_encode($session);
}
// Calls incodes `omni/start` and then with the token calls `0/omni/onboarding-url`
// to retrieve the unique onboarding-url for the newly created session.
function onboardingUrl() {
global $defaultHeader;
$body = [
'configurationId' => $_ENV['FLOW_ID'],
'countryCode' => 'ALL',
'language' => 'en-US',
// 'redirectionUrl' => 'https://example.com?custom_parameter=some+value',
// 'externalCustomerId' => 'the id of the customer in your system',
];
$Client = new Client(['base_uri' => $_ENV['API_URL']]);
$Request = new Request('POST', '/omni/start', $defaultHeader, json_encode($body));
$Response = $Client->sendAsync($Request)->wait();
$startData = json_decode($Response->getBody());
$onboardingHeader = [
'Accept' => 'application/json',
'Content-Type'=> "application/json",
'X-Incode-Hardware-Id'=> $startData->token,
'api-version'=> '1.0',
'query'=> 'clientId='.urlEncode($_ENV['CLIENT_ID'])
];
$Request = new Request('GET', '/0/omni/onboarding-url', $onboardingHeader);
$Response = $Client->sendAsync($Request)->wait();
$OnboardingUrlData = json_decode($Response->getBody());
$session = ['token'=> $startData->token, 'interviewId' => $startData->interviewId, 'url'=> $OnboardingUrlData->url];
echo json_encode($session);
}
// Webhook to receive onboarding status, configure it in
// incode dasboard >settings > webhook >onboarding status
function webhook() {
// We receive raw json data
$payload = file_get_contents('php://input');
$data = json_decode($payload, true); // Decode JSON payload
var_dump($data);
// Process received data (for demonstration, just returning the received payload
// and include the timestamp)
$response = array(
'timestamp' => date("Y-m-d H:i:s"),
'success' => true,
'data' => $data
);
echo json_encode($response);
// Write to a log so you can debug it. Use the command `tail -f debug.log` to watch the file in realtime.
file_put_contents('debug.log', json_encode($response, JSON_PRETTY_PRINT)."\n", FILE_APPEND | LOCK_EX);
}
// Webhook to receive onboarding status, configure it in
// incode dasboard > settings > webhook > onboarding status
// This endpoint will auto-approve(create an identity) for
// any sessions that PASS.
function approve() {
// We receive raw json data
$payload = file_get_contents('php://input');
$data = json_decode($payload, true); // Decode JSON payload
if($data['onboardingStatus']=="ONBOARDING_FINISHED"){
$Client = new Client(['base_uri' => $_ENV['API_URL']]);
// Admin Token + ApiKey are needed for approving and fetching scores
$adminHeaders = [
'Content-Type' => "application/json",
'x-api-key' => $_ENV['API_KEY'],
'X-Incode-Hardware-Id' => $_ENV['ADMIN_TOKEN'],
'api-version' => '1.0'
];
$scoreUrl='/omni/get/score?id='.urlEncode($data['interviewId']);
$Request = new Request('GET', $scoreUrl, $adminHeaders);
$Response = $Client->sendAsync($Request)->wait();
$onboardingScore = json_decode($Response->getBody());
if($onboardingScore->overall->status==='OK'){
$approveURL='/omni/process/approve?interviewId='.urlEncode($data['interviewId']);
$Request = new Request('POST', $approveURL, $adminHeaders);
$Response = $Client->sendAsync($Request)->wait();
$identityData = json_decode($Response->getBody());
$response = array(
'timestamp' => date("Y-m-d H:i:s"),
'success' => true,
'data' => $identityData
);
// This would return something like this:
// {
// timestamp: '2024-01-04 00:38:28',
// success: true,
// data: {
// success: true,
// uuid: '6595c84ce69d469f69ad39fb',
// token: 'eyJhbGciOiJ4UzI1NiJ9.eyJleHRlcm5hbFVzZXJJZCI6IjY1OTVjODRjZTY5ZDk2OWY2OWF33kMjlmYiIsInJvbGUiOiJBQ0NFU5MiLCJrZXlSZWYiOiI2MmZlNjQ3ZTJjODJlOTVhZDNhZTRjMzkiLCJleHAiOjE3MTIxOTExMDksImlhdCI6MTcwNDMyODcwOX0.fbhlcTQrp-h-spgxKU2J7wpEBN4I4iOYG5CBwuQKPLQ72',
// totalScore: 'OK',
// existingCustomer: false
// }
// }
// UUID: You can save the generated uuid of your user to link your user with our systems.
// Token: Is long lived and could be used to do calls in the name of the user if needed.
// Existing Customer: Will return true in case the user was already in the database, in such case we are returning the UUID of the already existing user.
echo json_encode($response);
} else {
$response = array(
'timestamp' => date("Y-m-d H:i:s"),
'success' => false,
'error' => "Session didn't PASS, identity was not created"
);
echo json_encode($response);
}
} else {
// Process received data (for demonstration, just returning the received payload
// and include the timestamp)
$response = array(
'timestamp' => date("Y-m-d H:i:s"),
'success' => true,
'data' => $data
);
echo json_encode($response);
}
// Write to a log so you can debug it. Use the command `tail -f debug.log` to watch the file in realtime.
file_put_contents('debug.log', json_encode($response, JSON_PRETTY_PRINT)."\n", FILE_APPEND | LOCK_EX);
}
// Receives the information about a faceMatch attempt and verifies if it was correct and has not been tampered.
function auth() {
global $defaultHeader;
// We receive raw json data
$payload = file_get_contents('php://input');
$data = json_decode($payload, true); // Decode JSON payload
$body = [
'transactionId' => $data["transactionId"],
'token' => $data["token"],
'interviewToken' => $data["interviewToken"],
];
$Client = new Client(['base_uri' => $_ENV['API_URL']]);
$Request = new Request('POST', '/omni/auth-attempt/verify', $defaultHeader, json_encode($body));
$Response = $Client->sendAsync($Request)->wait();
$Data = json_decode($Response->getBody());
echo json_encode($Data);
}
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 0'); // Do not cache
}
// All responses are in json
header('Content-Type: application/json');
// Main logic to handle requests
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if ( str_starts_with($_SERVER['REQUEST_URI'], '/start') ) {
start();
exit(0);
} elseif ( str_starts_with($_SERVER['REQUEST_URI'], '/onboarding-url') ) {
onboardingUrl();
exit(0);
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ( str_starts_with($_SERVER['REQUEST_URI'],'/webhook') ) {
webhook();
exit(0);
} elseif ( str_starts_with($_SERVER['REQUEST_URI'],'/approve') ) {
approve();
exit(0);
} elseif ( str_starts_with($_SERVER['REQUEST_URI'],'/auth') ) {
auth();
exit(0);
}
} else if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
// List only valid methods
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
}
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
}
exit(0);
}
// No method and uri not found
http_response_code(404);
header("Content-Type: application/json");
echo "{\"error\":\"Cannot {$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']}\"}";