author | Dan |
Tue, 04 Aug 2009 19:37:38 -0400 | |
changeset 29 | 7cd9707ed72f |
parent 27 | 647f0aa485dd |
child 35 | 03d6287d4a8b |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
||
3 |
define('YK_SEC_NORMAL_USERNAME', 1); |
|
4 |
define('YK_SEC_NORMAL_PASSWORD', 2); |
|
5 |
define('YK_SEC_ELEV_USERNAME', 4); |
|
6 |
define('YK_SEC_ELEV_PASSWORD', 8); |
|
7 |
define('YK_SEC_ALLOW_NO_OTP', 16); |
|
8 |
||
9 |
define('YK_DEFAULT_VERIFY_URL', 'http://api.yubico.com/wsapi/verify'); |
|
10 |
||
11 |
function generate_yubikey_field($name = 'yubikey_otp', $value = false) |
|
12 |
{ |
|
13 |
global $lang; |
|
14 |
||
15 |
$fid = substr(sha1(microtime() . mt_rand()), 0, 12); |
|
16 |
$class = $value ? 'wasfull' : 'wasempty'; |
|
17 |
$html = '<input id="yubifield' . $fid . '" class="' . $class . '" type="hidden" name="' . $name . '" value="' . ( is_string($value) ? $value : '' ) . '" />'; |
|
29
7cd9707ed72f
Fixed OTP field behavior (improper/empty submission) exposed in YMS
Dan
parents:
27
diff
changeset
|
18 |
$html .= '<noscript><input type="text" name="' . $name . '" class="yubikey_noscript" value="' . ( is_string($value) ? $value : '' ) . '" /> </noscript>'; |
0 | 19 |
if ( $value ) |
20 |
{ |
|
21 |
$html .= '<span id="yubistat' . $fid . '" class="yubikey_status enrolled">' . $lang->get('yubiauth_ctl_status_enrolled') . '</span>'; |
|
22 |
$atext = $lang->get('yubiauth_ctl_btn_change_key'); |
|
23 |
$classadd = ' abutton_green'; |
|
24 |
} |
|
25 |
else |
|
26 |
{ |
|
27 |
$html .= '<span id="yubistat' . $fid . '" class="yubikey_status empty">' . $lang->get('yubiauth_ctl_status_empty') . '</span>'; |
|
28 |
$atext = $lang->get('yubiauth_ctl_btn_enroll'); |
|
29 |
$classadd = ''; |
|
30 |
} |
|
31 |
$html .= ' <a class="abutton' . $classadd . ' yubikey_enroll" onclick="yk_mb_init(\'yubifield' . $fid . '\', \'yubistat' . $fid . '\'); return false;" href="#enroll">' . $atext . '</a>'; |
|
32 |
if ( $value ) |
|
33 |
{ |
|
34 |
$html .= ' <a class="abutton abutton_red yubikey_enroll" onclick="yk_clear(\'yubifield' . $fid . '\', \'yubistat' . $fid . '\'); return false;" href="#enroll">' |
|
35 |
. $lang->get('yubiauth_ctl_btn_clear') . |
|
36 |
'</a>'; |
|
37 |
} |
|
38 |
return $html; |
|
39 |
} |
|
40 |
||
41 |
function yubikey_validate_otp($otp) |
|
42 |
{ |
|
43 |
$api_key = getConfig('yubikey_api_key'); |
|
44 |
$api_id = getConfig('yubikey_api_key_id'); |
|
45 |
if ( !$api_key || !$api_id ) |
|
46 |
{ |
|
47 |
return array( |
|
48 |
'success' => false, |
|
49 |
'error' => 'missing_api_key' |
|
50 |
); |
|
51 |
} |
|
52 |
if ( !preg_match('/^[cbdefghijklnrtuv]{44}$/', $otp) ) |
|
53 |
{ |
|
54 |
return array( |
|
55 |
'success' => false, |
|
56 |
'error' => 'otp_invalid_chars' |
|
57 |
); |
|
58 |
} |
|
27 | 59 |
// are we using local YMS? |
60 |
if ( getConfig('yubikey_use_local_yms', 0) && defined('YMS_INSTALLED') ) |
|
61 |
{ |
|
62 |
$result = yms_validate_otp($otp, $api_id); |
|
63 |
if ( $result == 'OK' ) |
|
64 |
{ |
|
65 |
return array( |
|
66 |
'success' => true |
|
67 |
); |
|
68 |
} |
|
69 |
else |
|
70 |
{ |
|
71 |
return array( |
|
72 |
'success' => false, |
|
73 |
'error' => strtolower("response_{$result}") |
|
74 |
); |
|
75 |
} |
|
76 |
} |
|
0 | 77 |
// make HTTP request |
78 |
require_once( ENANO_ROOT . '/includes/http.php' ); |
|
79 |
$auth_url = getConfig('yubikey_auth_server', YK_DEFAULT_VERIFY_URL); |
|
80 |
$auth_url = preg_replace('#^https?://#i', '', $auth_url); |
|
16 | 81 |
if ( !preg_match('#^(\[?[a-z0-9-:]+(?:\.[a-z0-9-:]+\]?)*)(?::([0-9]+))?(/.*)$#U', $auth_url, $match) ) |
0 | 82 |
{ |
83 |
return array( |
|
84 |
'success' => false, |
|
85 |
'error' => 'invalid_auth_url' |
|
86 |
); |
|
87 |
} |
|
88 |
$auth_server =& $match[1]; |
|
16 | 89 |
$auth_port = ( !empty($match[2]) ) ? intval($match[2]) : 80; |
90 |
$auth_uri =& $match[3]; |
|
91 |
try |
|
92 |
{ |
|
93 |
$req = new Request_HTTP($auth_server, $auth_uri, 'GET', $auth_port); |
|
94 |
$req->add_get('id', strval($api_id)); |
|
95 |
$req->add_get('otp', $otp); |
|
96 |
$req->add_get('h', yubikey_sign($req->parms_get)); |
|
0 | 97 |
|
16 | 98 |
$response = $req->get_response_body(); |
99 |
} |
|
100 |
catch ( Exception $e ) |
|
101 |
{ |
|
102 |
return array( |
|
103 |
'success' => false, |
|
104 |
'error' => 'http_failed', |
|
105 |
'http_error' => $e->getMessage() |
|
106 |
); |
|
107 |
} |
|
0 | 108 |
|
109 |
if ( $req->response_code != HTTP_OK ) |
|
110 |
{ |
|
111 |
return array( |
|
112 |
'success' => false, |
|
113 |
'error' => 'http_response_error' |
|
114 |
); |
|
115 |
} |
|
116 |
$response = trim($response); |
|
4
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
117 |
if ( !preg_match_all('/^([a-z0-9_]+)=(.*?)\r?$/m', $response, $matches) ) |
0 | 118 |
{ |
119 |
return array( |
|
120 |
'success' => false, |
|
121 |
'error' => 'malformed_response' |
|
122 |
); |
|
123 |
} |
|
124 |
$response = array(); |
|
125 |
foreach ( $matches[0] as $i => $_ ) |
|
126 |
{ |
|
127 |
$response[$matches[1][$i]] = $matches[2][$i]; |
|
128 |
} |
|
129 |
// make sure we have a status |
|
130 |
if ( !isset($response['status']) ) |
|
131 |
{ |
|
132 |
return array( |
|
133 |
'success' => false, |
|
134 |
'error' => 'response_missing_status' |
|
135 |
); |
|
136 |
} |
|
137 |
// verify response signature |
|
138 |
// MISSING_PARAMETER is the ONLY situation under which an unsigned response is acceptable |
|
139 |
if ( $response['status'] !== 'MISSING_PARAMETER' ) |
|
140 |
{ |
|
141 |
if ( !isset($response['h']) ) |
|
142 |
{ |
|
143 |
return array( |
|
144 |
'success' => false, |
|
145 |
'error' => 'response_missing_sig' |
|
146 |
); |
|
147 |
} |
|
148 |
if ( yubikey_sign($response) !== $response['h'] ) |
|
149 |
{ |
|
150 |
return array( |
|
151 |
'success' => false, |
|
152 |
'error' => 'response_invalid_sig' |
|
153 |
); |
|
154 |
} |
|
155 |
} |
|
156 |
if ( $response['status'] === 'OK' ) |
|
157 |
{ |
|
10 | 158 |
if ( yubikey_verify_timestamp($response['t']) ) |
159 |
{ |
|
160 |
return array( |
|
161 |
'success' => true |
|
162 |
); |
|
163 |
} |
|
164 |
else |
|
165 |
{ |
|
166 |
return array( |
|
167 |
'success' => false, |
|
168 |
'error' => 'timestamp_check_failed' |
|
169 |
); |
|
170 |
} |
|
0 | 171 |
} |
172 |
else |
|
173 |
{ |
|
174 |
return array( |
|
175 |
'success' => false, |
|
176 |
'error' => strtolower("response_{$response['status']}") |
|
177 |
); |
|
178 |
} |
|
179 |
} |
|
180 |
||
27 | 181 |
function yubikey_sign($arr, $use_api_key = false) |
0 | 182 |
{ |
183 |
static $api_key = false; |
|
184 |
||
185 |
ksort($arr); |
|
186 |
||
27 | 187 |
if ( !$use_api_key ) |
0 | 188 |
{ |
27 | 189 |
if ( !$api_key ) |
190 |
{ |
|
191 |
$api_key = getConfig('yubikey_api_key'); |
|
192 |
$api_key = hexencode(base64_decode($api_key), '', ''); |
|
193 |
} |
|
194 |
$use_api_key = $api_key; |
|
0 | 195 |
} |
27 | 196 |
/* |
197 |
else |
|
198 |
{ |
|
199 |
$use_api_key = hexencode(base64_decode($use_api_key), '', ''); |
|
200 |
} |
|
201 |
*/ |
|
0 | 202 |
|
27 | 203 |
foreach ( array('h', 'title', 'auth', 'do') as $key ) |
204 |
{ |
|
205 |
if ( isset($arr[$key]) ) |
|
206 |
unset($arr[$key]); |
|
207 |
} |
|
4
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
208 |
|
0 | 209 |
$req = array(); |
210 |
foreach ( $arr as $key => $val ) |
|
211 |
{ |
|
212 |
$req[] = "$key=$val"; |
|
213 |
} |
|
214 |
$req = implode('&', $req); |
|
215 |
||
27 | 216 |
$sig = hmac_sha1($req, $use_api_key); |
0 | 217 |
$sig = hexdecode($sig); |
218 |
$sig = base64_encode($sig); |
|
219 |
||
220 |
return $sig; |
|
221 |
} |
|
222 |
||
10 | 223 |
/** |
224 |
* Validate the timestamp returned in a Yubico API response. Borrowed from Drupal and backported for friendliness with earlier versions of PHP. |
|
225 |
* @param string Yubico timestamp |
|
226 |
* @return bool True if valid, false otherwise |
|
227 |
*/ |
|
228 |
||
229 |
function yubikey_verify_timestamp($timestamp) |
|
230 |
{ |
|
231 |
$tolerance = intval(getConfig('yubikey_api_ts_tolerance', 150)); |
|
232 |
||
233 |
$now = time(); |
|
21
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
234 |
$timestamp_seconds = yk_strtotime($timestamp); |
10 | 235 |
|
21
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
236 |
if ( !$timestamp || !$now || !$timestamp_seconds ) |
10 | 237 |
{ |
238 |
return false; |
|
239 |
} |
|
240 |
||
241 |
if ( ( $timestamp_seconds + $tolerance ) > $now && ( $timestamp_seconds - $tolerance ) < $now ) |
|
242 |
{ |
|
243 |
return true; |
|
244 |
} |
|
245 |
||
246 |
return false; |
|
247 |
} |
|
248 |
||
21
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
249 |
function yk_strtotime($timestamp) |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
250 |
{ |
22 | 251 |
if ( !preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z[0-9]+)?$/', $timestamp, $match) ) |
21
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
252 |
return 0; |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
253 |
|
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
254 |
$hour = intval($match[4]); |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
255 |
$minute = intval($match[5]); |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
256 |
$second = intval($match[6]); |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
257 |
$month = intval($match[2]); |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
258 |
$day = intval($match[3]); |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
259 |
$year = intval($match[1]); |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
260 |
|
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
261 |
return gmmktime($hour, $minute, $second, $month, $day, $year); |
f34ecfa459ab
Wrote my own Yubico-time-to-Unix-time conversion, as strtotime() was unreliable.
Dan
parents:
20
diff
changeset
|
262 |
} |
10 | 263 |
|
0 | 264 |
$plugins->attachHook('compile_template', 'yubikey_attach_headers($this);'); |
265 |
||
266 |
function yubikey_attach_headers(&$template) |
|
267 |
{ |
|
10 | 268 |
global $db, $session, $paths, $template, $plugins; // Common objects |
269 |
||
3
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
270 |
if ( getConfig('yubikey_enable', '1') != '1' ) |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
271 |
return true; |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
272 |
|
0 | 273 |
$template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/yubikey/yubikey.js"></script>'); |
274 |
$template->add_header('<link rel="stylesheet" type="text/css" href="' . scriptPath . '/plugins/yubikey/yubikey.css" />'); |
|
9
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
4
diff
changeset
|
275 |
// config option for all users have yubikey |
10 | 276 |
$user_flags = 0; |
18
dd8c53454f31
Yubikey flags are no longer fetched from server at login time, instead provided with load
Dan
parents:
16
diff
changeset
|
277 |
$yk_enabled = 0; |
10 | 278 |
if ( $session->user_logged_in ) |
279 |
{ |
|
20
5a359c7ebc48
Fixed "Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause"
Dan
parents:
19
diff
changeset
|
280 |
$q = $db->sql_query('SELECT COUNT(y.yubi_uid) > 0, u.user_yubikey_flags FROM ' . table_prefix . "yubikey AS y LEFT JOIN " . table_prefix . "users AS u ON ( u.user_id = y.user_id ) WHERE y.user_id = {$session->user_id} GROUP BY u.user_id, u.user_yubikey_flags;"); |
10 | 281 |
if ( !$q ) |
282 |
$db->_die(); |
|
283 |
||
18
dd8c53454f31
Yubikey flags are no longer fetched from server at login time, instead provided with load
Dan
parents:
16
diff
changeset
|
284 |
list($yk_enabled, $user_flags) = $db->fetchrow_num(); |
10 | 285 |
$db->free_result(); |
286 |
} |
|
287 |
||
18
dd8c53454f31
Yubikey flags are no longer fetched from server at login time, instead provided with load
Dan
parents:
16
diff
changeset
|
288 |
$template->add_header('<script type="text/javascript">var yk_reg_require_otp = ' . getConfig('yubikey_reg_require_otp', '0') . '; var yk_user_enabled = ' . $yk_enabled . '; var yk_user_flags = ' . $user_flags . ';</script>'); |
0 | 289 |
} |
290 |