author | Dan |
Thu, 30 Apr 2009 16:11:13 -0400 | |
changeset 16 | 3163b9f58ae8 |
parent 10 | 748fa1b80031 |
child 18 | dd8c53454f31 |
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 : '' ) . '" />'; |
|
18 |
if ( $value ) |
|
19 |
{ |
|
20 |
$html .= '<span id="yubistat' . $fid . '" class="yubikey_status enrolled">' . $lang->get('yubiauth_ctl_status_enrolled') . '</span>'; |
|
21 |
$atext = $lang->get('yubiauth_ctl_btn_change_key'); |
|
22 |
$classadd = ' abutton_green'; |
|
23 |
} |
|
24 |
else |
|
25 |
{ |
|
26 |
$html .= '<span id="yubistat' . $fid . '" class="yubikey_status empty">' . $lang->get('yubiauth_ctl_status_empty') . '</span>'; |
|
27 |
$atext = $lang->get('yubiauth_ctl_btn_enroll'); |
|
28 |
$classadd = ''; |
|
29 |
} |
|
30 |
$html .= ' <a class="abutton' . $classadd . ' yubikey_enroll" onclick="yk_mb_init(\'yubifield' . $fid . '\', \'yubistat' . $fid . '\'); return false;" href="#enroll">' . $atext . '</a>'; |
|
31 |
if ( $value ) |
|
32 |
{ |
|
33 |
$html .= ' <a class="abutton abutton_red yubikey_enroll" onclick="yk_clear(\'yubifield' . $fid . '\', \'yubistat' . $fid . '\'); return false;" href="#enroll">' |
|
34 |
. $lang->get('yubiauth_ctl_btn_clear') . |
|
35 |
'</a>'; |
|
36 |
} |
|
37 |
$html = '<noscript><input type="text" name="' . $name . '" class="yubikey_noscript" value="' . ( is_string($value) ? $value : '' ) . '" /> </noscript>' |
|
38 |
. $html; // '<script type="text/javascript">document.write(unescape("' . rawurlencode($html) . '"));</script>'; |
|
39 |
return $html; |
|
40 |
} |
|
41 |
||
42 |
function yubikey_validate_otp($otp) |
|
43 |
{ |
|
44 |
$api_key = getConfig('yubikey_api_key'); |
|
45 |
$api_id = getConfig('yubikey_api_key_id'); |
|
46 |
if ( !$api_key || !$api_id ) |
|
47 |
{ |
|
48 |
return array( |
|
49 |
'success' => false, |
|
50 |
'error' => 'missing_api_key' |
|
51 |
); |
|
52 |
} |
|
53 |
if ( !preg_match('/^[cbdefghijklnrtuv]{44}$/', $otp) ) |
|
54 |
{ |
|
55 |
return array( |
|
56 |
'success' => false, |
|
57 |
'error' => 'otp_invalid_chars' |
|
58 |
); |
|
59 |
} |
|
60 |
// make HTTP request |
|
61 |
require_once( ENANO_ROOT . '/includes/http.php' ); |
|
62 |
$auth_url = getConfig('yubikey_auth_server', YK_DEFAULT_VERIFY_URL); |
|
63 |
$auth_url = preg_replace('#^https?://#i', '', $auth_url); |
|
16 | 64 |
if ( !preg_match('#^(\[?[a-z0-9-:]+(?:\.[a-z0-9-:]+\]?)*)(?::([0-9]+))?(/.*)$#U', $auth_url, $match) ) |
0 | 65 |
{ |
66 |
return array( |
|
67 |
'success' => false, |
|
68 |
'error' => 'invalid_auth_url' |
|
69 |
); |
|
70 |
} |
|
71 |
$auth_server =& $match[1]; |
|
16 | 72 |
$auth_port = ( !empty($match[2]) ) ? intval($match[2]) : 80; |
73 |
$auth_uri =& $match[3]; |
|
74 |
try |
|
75 |
{ |
|
76 |
$req = new Request_HTTP($auth_server, $auth_uri, 'GET', $auth_port); |
|
77 |
$req->add_get('id', strval($api_id)); |
|
78 |
$req->add_get('otp', $otp); |
|
79 |
$req->add_get('h', yubikey_sign($req->parms_get)); |
|
0 | 80 |
|
16 | 81 |
$response = $req->get_response_body(); |
82 |
} |
|
83 |
catch ( Exception $e ) |
|
84 |
{ |
|
85 |
return array( |
|
86 |
'success' => false, |
|
87 |
'error' => 'http_failed', |
|
88 |
'http_error' => $e->getMessage() |
|
89 |
); |
|
90 |
} |
|
0 | 91 |
|
92 |
if ( $req->response_code != HTTP_OK ) |
|
93 |
{ |
|
94 |
return array( |
|
95 |
'success' => false, |
|
96 |
'error' => 'http_response_error' |
|
97 |
); |
|
98 |
} |
|
99 |
$response = trim($response); |
|
4
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
100 |
if ( !preg_match_all('/^([a-z0-9_]+)=(.*?)\r?$/m', $response, $matches) ) |
0 | 101 |
{ |
102 |
return array( |
|
103 |
'success' => false, |
|
104 |
'error' => 'malformed_response' |
|
105 |
); |
|
106 |
} |
|
107 |
$response = array(); |
|
108 |
foreach ( $matches[0] as $i => $_ ) |
|
109 |
{ |
|
110 |
$response[$matches[1][$i]] = $matches[2][$i]; |
|
111 |
} |
|
112 |
// make sure we have a status |
|
113 |
if ( !isset($response['status']) ) |
|
114 |
{ |
|
115 |
return array( |
|
116 |
'success' => false, |
|
117 |
'error' => 'response_missing_status' |
|
118 |
); |
|
119 |
} |
|
120 |
// verify response signature |
|
121 |
// MISSING_PARAMETER is the ONLY situation under which an unsigned response is acceptable |
|
122 |
if ( $response['status'] !== 'MISSING_PARAMETER' ) |
|
123 |
{ |
|
124 |
if ( !isset($response['h']) ) |
|
125 |
{ |
|
126 |
return array( |
|
127 |
'success' => false, |
|
128 |
'error' => 'response_missing_sig' |
|
129 |
); |
|
130 |
} |
|
131 |
if ( yubikey_sign($response) !== $response['h'] ) |
|
132 |
{ |
|
133 |
return array( |
|
134 |
'success' => false, |
|
135 |
'error' => 'response_invalid_sig' |
|
136 |
); |
|
137 |
} |
|
138 |
} |
|
139 |
if ( $response['status'] === 'OK' ) |
|
140 |
{ |
|
10 | 141 |
if ( yubikey_verify_timestamp($response['t']) ) |
142 |
{ |
|
143 |
return array( |
|
144 |
'success' => true |
|
145 |
); |
|
146 |
} |
|
147 |
else |
|
148 |
{ |
|
149 |
return array( |
|
150 |
'success' => false, |
|
151 |
'error' => 'timestamp_check_failed' |
|
152 |
); |
|
153 |
} |
|
0 | 154 |
} |
155 |
else |
|
156 |
{ |
|
157 |
return array( |
|
158 |
'success' => false, |
|
159 |
'error' => strtolower("response_{$response['status']}") |
|
160 |
); |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
function yubikey_sign($arr) |
|
165 |
{ |
|
166 |
static $api_key = false; |
|
167 |
||
168 |
ksort($arr); |
|
169 |
||
170 |
if ( !$api_key ) |
|
171 |
{ |
|
172 |
$api_key = getConfig('yubikey_api_key'); |
|
173 |
$api_key = hexencode(base64_decode($api_key), '', ''); |
|
174 |
} |
|
175 |
||
4
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
176 |
if ( isset($arr['h']) ) |
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
177 |
unset($arr['h']); |
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
178 |
|
0 | 179 |
$req = array(); |
180 |
foreach ( $arr as $key => $val ) |
|
181 |
{ |
|
182 |
$req[] = "$key=$val"; |
|
183 |
} |
|
184 |
$req = implode('&', $req); |
|
185 |
||
186 |
$sig = hmac_sha1($req, $api_key); |
|
187 |
$sig = hexdecode($sig); |
|
188 |
$sig = base64_encode($sig); |
|
189 |
||
190 |
return $sig; |
|
191 |
} |
|
192 |
||
10 | 193 |
/** |
194 |
* Validate the timestamp returned in a Yubico API response. Borrowed from Drupal and backported for friendliness with earlier versions of PHP. |
|
195 |
* @param string Yubico timestamp |
|
196 |
* @return bool True if valid, false otherwise |
|
197 |
*/ |
|
198 |
||
199 |
function yubikey_verify_timestamp($timestamp) |
|
200 |
{ |
|
201 |
$tolerance = intval(getConfig('yubikey_api_ts_tolerance', 150)); |
|
202 |
||
203 |
$now = time(); |
|
16 | 204 |
$timestamp = preg_replace('/Z[0-9]{3}$/', '', $timestamp); |
205 |
$timestamp_seconds = strtotime($timestamp); |
|
10 | 206 |
|
207 |
if ( !$timestamp || !$now ) |
|
208 |
{ |
|
209 |
return false; |
|
210 |
} |
|
211 |
||
212 |
if ( ( $timestamp_seconds + $tolerance ) > $now && ( $timestamp_seconds - $tolerance ) < $now ) |
|
213 |
{ |
|
214 |
return true; |
|
215 |
} |
|
216 |
||
217 |
return false; |
|
218 |
} |
|
219 |
||
220 |
||
0 | 221 |
$plugins->attachHook('compile_template', 'yubikey_attach_headers($this);'); |
222 |
||
223 |
function yubikey_attach_headers(&$template) |
|
224 |
{ |
|
10 | 225 |
global $db, $session, $paths, $template, $plugins; // Common objects |
226 |
||
3
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
227 |
if ( getConfig('yubikey_enable', '1') != '1' ) |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
228 |
return true; |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
229 |
|
0 | 230 |
$template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/yubikey/yubikey.js"></script>'); |
231 |
$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
|
232 |
// config option for all users have yubikey |
10 | 233 |
$user_flags = 0; |
234 |
if ( $session->user_logged_in ) |
|
235 |
{ |
|
236 |
$q = $db->sql_query('SELECT COUNT(yubi_uid) > 0 FROM ' . table_prefix . "yubikey WHERE user_id = {$session->user_id};"); |
|
237 |
if ( !$q ) |
|
238 |
$db->_die(); |
|
239 |
||
240 |
list($user_flags) = $db->fetchrow_num(); |
|
241 |
$db->free_result(); |
|
242 |
} |
|
243 |
||
244 |
$template->add_header('<script type="text/javascript">var yk_reg_require_otp = ' . getConfig('yubikey_reg_require_otp', '0') . '; var yk_user_enabled = ' . $user_flags . ';</script>'); |
|
0 | 245 |
} |
246 |