-
Notifications
You must be signed in to change notification settings - Fork 11
/
validate.php
60 lines (44 loc) · 1.3 KB
/
validate.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
<?php
// These are required!
$product_token = getenv('KEYGEN_PRODUCT_TOKEN');
$account_id = getenv('KEYGEN_ACCOUNT_ID');
$policy_id = getenv('KEYGEN_POLICY_ID');
// Validate the provided license key within the scope of the user's
// current machine.
$fingerprint = $_GET['fingerprint'];
$key = $_GET['key'];
if (!isset($key)) {
http_response_code(400);
echo "License key is required for license validation";
exit(1);
}
$url = "https://api.keygen.sh/v1/accounts/{$account_id}/licenses/actions/validate-key";
$ch = curl_init($url);
$body = json_encode([
'meta' => [
'scope' => ['fingerprint' => $fingerprint],
'key' => $key
]
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json'
]);
$json = curl_exec($ch);
$res = json_decode($json);
// Check if we received an error from Keygen during validation.
if (isset($res->errors)) {
http_response_code(500);
$messages = join(', ', array_map(function($err) { return $err->detail; }, $res->errors));
echo "Error: $messages\n";
exit(1);
}
if ($res->meta->valid) {
http_response_code(200);
} else {
http_response_code(422);
}
echo "The license {$res->meta->detail}";