author | Dan |
Sun, 08 Mar 2009 00:45:45 -0500 | |
changeset 10 | 748fa1b80031 |
parent 9 | 65965da01c41 |
child 16 | 3163b9f58ae8 |
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); |
|
64 |
if ( !preg_match('#^(\[?[a-z0-9-:]+(?:\.[a-z0-9-:]+\]?)*)(/.*)$#', $auth_url, $match) ) |
|
65 |
{ |
|
66 |
return array( |
|
67 |
'success' => false, |
|
68 |
'error' => 'invalid_auth_url' |
|
69 |
); |
|
70 |
} |
|
71 |
$auth_server =& $match[1]; |
|
72 |
$auth_uri =& $match[2]; |
|
73 |
$req = new Request_HTTP($auth_server, $auth_uri); |
|
74 |
$req->add_get('id', strval($api_id)); |
|
75 |
$req->add_get('otp', $otp); |
|
76 |
$req->add_get('h', yubikey_sign($req->parms_get)); |
|
77 |
||
78 |
$response = $req->get_response_body(); |
|
79 |
||
80 |
if ( $req->response_code != HTTP_OK ) |
|
81 |
{ |
|
82 |
return array( |
|
83 |
'success' => false, |
|
84 |
'error' => 'http_response_error' |
|
85 |
); |
|
86 |
} |
|
87 |
$response = trim($response); |
|
4
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
88 |
if ( !preg_match_all('/^([a-z0-9_]+)=(.*?)\r?$/m', $response, $matches) ) |
0 | 89 |
{ |
90 |
return array( |
|
91 |
'success' => false, |
|
92 |
'error' => 'malformed_response' |
|
93 |
); |
|
94 |
} |
|
95 |
$response = array(); |
|
96 |
foreach ( $matches[0] as $i => $_ ) |
|
97 |
{ |
|
98 |
$response[$matches[1][$i]] = $matches[2][$i]; |
|
99 |
} |
|
100 |
// make sure we have a status |
|
101 |
if ( !isset($response['status']) ) |
|
102 |
{ |
|
103 |
return array( |
|
104 |
'success' => false, |
|
105 |
'error' => 'response_missing_status' |
|
106 |
); |
|
107 |
} |
|
108 |
// verify response signature |
|
109 |
// MISSING_PARAMETER is the ONLY situation under which an unsigned response is acceptable |
|
110 |
if ( $response['status'] !== 'MISSING_PARAMETER' ) |
|
111 |
{ |
|
112 |
if ( !isset($response['h']) ) |
|
113 |
{ |
|
114 |
return array( |
|
115 |
'success' => false, |
|
116 |
'error' => 'response_missing_sig' |
|
117 |
); |
|
118 |
} |
|
119 |
if ( yubikey_sign($response) !== $response['h'] ) |
|
120 |
{ |
|
121 |
return array( |
|
122 |
'success' => false, |
|
123 |
'error' => 'response_invalid_sig' |
|
124 |
); |
|
125 |
} |
|
126 |
} |
|
127 |
if ( $response['status'] === 'OK' ) |
|
128 |
{ |
|
10 | 129 |
if ( yubikey_verify_timestamp($response['t']) ) |
130 |
{ |
|
131 |
return array( |
|
132 |
'success' => true |
|
133 |
); |
|
134 |
} |
|
135 |
else |
|
136 |
{ |
|
137 |
return array( |
|
138 |
'success' => false, |
|
139 |
'error' => 'timestamp_check_failed' |
|
140 |
); |
|
141 |
} |
|
0 | 142 |
} |
143 |
else |
|
144 |
{ |
|
145 |
return array( |
|
146 |
'success' => false, |
|
147 |
'error' => strtolower("response_{$response['status']}") |
|
148 |
); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
function yubikey_sign($arr) |
|
153 |
{ |
|
154 |
static $api_key = false; |
|
155 |
||
156 |
ksort($arr); |
|
157 |
||
158 |
if ( !$api_key ) |
|
159 |
{ |
|
160 |
$api_key = getConfig('yubikey_api_key'); |
|
161 |
$api_key = hexencode(base64_decode($api_key), '', ''); |
|
162 |
} |
|
163 |
||
4
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
164 |
if ( isset($arr['h']) ) |
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
165 |
unset($arr['h']); |
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
166 |
|
0 | 167 |
$req = array(); |
168 |
foreach ( $arr as $key => $val ) |
|
169 |
{ |
|
170 |
$req[] = "$key=$val"; |
|
171 |
} |
|
172 |
$req = implode('&', $req); |
|
173 |
||
174 |
$sig = hmac_sha1($req, $api_key); |
|
175 |
$sig = hexdecode($sig); |
|
176 |
$sig = base64_encode($sig); |
|
177 |
||
178 |
return $sig; |
|
179 |
} |
|
180 |
||
10 | 181 |
/** |
182 |
* Validate the timestamp returned in a Yubico API response. Borrowed from Drupal and backported for friendliness with earlier versions of PHP. |
|
183 |
* @param string Yubico timestamp |
|
184 |
* @return bool True if valid, false otherwise |
|
185 |
*/ |
|
186 |
||
187 |
function yubikey_verify_timestamp($timestamp) |
|
188 |
{ |
|
189 |
$tolerance = intval(getConfig('yubikey_api_ts_tolerance', 150)); |
|
190 |
||
191 |
$now = time(); |
|
192 |
$timestamp_seconds = strtotime(substr($timestamp, 0, -4)); |
|
193 |
||
194 |
if ( !$timestamp || !$now ) |
|
195 |
{ |
|
196 |
return false; |
|
197 |
} |
|
198 |
||
199 |
if ( ( $timestamp_seconds + $tolerance ) > $now && ( $timestamp_seconds - $tolerance ) < $now ) |
|
200 |
{ |
|
201 |
return true; |
|
202 |
} |
|
203 |
||
204 |
return false; |
|
205 |
} |
|
206 |
||
207 |
||
0 | 208 |
$plugins->attachHook('compile_template', 'yubikey_attach_headers($this);'); |
209 |
||
210 |
function yubikey_attach_headers(&$template) |
|
211 |
{ |
|
10 | 212 |
global $db, $session, $paths, $template, $plugins; // Common objects |
213 |
||
3
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
214 |
if ( getConfig('yubikey_enable', '1') != '1' ) |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
215 |
return true; |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
216 |
|
0 | 217 |
$template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/yubikey/yubikey.js"></script>'); |
218 |
$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
|
219 |
// config option for all users have yubikey |
10 | 220 |
$user_flags = 0; |
221 |
if ( $session->user_logged_in ) |
|
222 |
{ |
|
223 |
$q = $db->sql_query('SELECT COUNT(yubi_uid) > 0 FROM ' . table_prefix . "yubikey WHERE user_id = {$session->user_id};"); |
|
224 |
if ( !$q ) |
|
225 |
$db->_die(); |
|
226 |
||
227 |
list($user_flags) = $db->fetchrow_num(); |
|
228 |
$db->free_result(); |
|
229 |
} |
|
230 |
||
231 |
$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 | 232 |
} |
233 |