This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YubiAuth.php
187 lines (130 loc) · 4.19 KB
/
YubiAuth.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
<?php
/**
* Class for verifying Yubico OTP
*
* Created by Matyáš Koc
*
* Published under Creative Commons BY-NC-SA license
*/
class YubiAuth {
private $api_id;
private $api_key;
private $server;
/* Creates a new validation object - requires ID and API key, accepts custom server */
public function __construct($api_id, $api_key, $server = 'https://api.yubico.com/wsapi/2.0/verify') {
$this->api_id = $api_id;
$this->api_key = $api_key;
$this->server = $server;
}
/* Validates OTP using validation server, then tests if the OTP matches to user key ID */
public function validate($otp, $key_id) {
/* Prepare query and make hash for validation */
$urlparams = "id=". $this->api_id ."&nonce=". $this->cryptoRandomNonce() ."&otp=". $otp;
$h = urlencode($this->makeSignature($urlparams));
/* Do the query */
$response = $this->queryServer($this->server ."?". $urlparams ."&h=". $h);
/* Make array from result */
$response = $this->parseResultToArray($response);
/* Validate response signature */
if (!$this->validateHash($response)) {
return false;
}
/* Validate status */
if (!$this->validateStatus($response)) {
return false;
}
/* Check if key ID matches supplied correct one */
if (!$this->validateKeyID($otp, $key_id)) {
return false;
}
return true;
}
/* Make query to validation server using cURL */
private function queryServer($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_ENCODING => "",
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
/* Generate HMAC hash for validation */
private function makeSignature($data) {
$signature = base64_encode(hash_hmac("sha1", $data, base64_decode($this->api_key), true));
return $signature;
}
/* Generate cryptographically strong random alphanumeric nonce */
private function cryptoRandomNonce() {
return preg_replace("/[^A-Za-z0-9 ]/", '', base64_encode(openssl_random_pseudo_bytes(14)));
}
/* Make sorted array from result */
private function parseResultToArray($result) {
$result_array = explode("\n", $result);
/* This removes all nonprintable and NULL fields from array */
foreach ($result_array as $key => $param) {
$param = preg_replace('/[\x00-\x1F\x7F]/u', '', $param);
if ($param == NULL) {
unset($result_array[$key]);
}
}
asort($result_array);
return $result_array;
}
/* Validates response from array */
private function validateHash($result_array) {
/* Look for the response signature and put it aside + make string from params */
foreach ($result_array as $key => $param) {
if (substr($param, 0, 1) == "h") {
$signature = $param;
unset($result_array[$key]);
continue;
}
$result_string .= $param . "&";
}
/* Remove whitespace and last & from string */
$result_string = preg_replace('/\s/', '', $result_string);
$result_string = substr($result_string, 0, -1);
/* Remove the h= and whitespace from signature */
$signature = substr($signature, 2);
$signature = preg_replace('/\s/', '', $signature);
/* Hash it and then compare */
if ($this->makeSignature($result_string) == $signature) {
return true;
}
return false;
}
/* Validates status from array */
private function validateStatus($result_array) {
/* Look for the status */
foreach ($result_array as $param) {
if (substr($param, 0, 6) == "status") {
$status = $param;
break;
}
}
/* Remove whirespace from status */
$status = preg_replace('/\s/', '', $status);
/* Check if status is OK, ignore case */
if (strtolower($status) == "status=ok") {
return true;
}
return false;
}
/* Validates if key ID matches the supplied correct one */
private function validateKeyID($otp, $key_id) {
/* Key ID are the first 12 characters in OTP */
$otp_key_id = substr($otp, 0, 12);
/* Check if key ID is correct */
if ($otp_key_id == $key_id) {
return true;
}
return false;
}
}
?>