author | Dan |
Thu, 26 Feb 2009 16:33:06 -0500 | |
changeset 4 | 73aecd46bb56 |
parent 3 | d0fe7acaf0e8 |
child 9 | 65965da01c41 |
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 |
{ |
|
129 |
return array( |
|
130 |
'success' => true |
|
131 |
); |
|
132 |
} |
|
133 |
else |
|
134 |
{ |
|
135 |
return array( |
|
136 |
'success' => false, |
|
137 |
'error' => strtolower("response_{$response['status']}") |
|
138 |
); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
function yubikey_sign($arr) |
|
143 |
{ |
|
144 |
static $api_key = false; |
|
145 |
||
146 |
ksort($arr); |
|
147 |
||
148 |
if ( !$api_key ) |
|
149 |
{ |
|
150 |
$api_key = getConfig('yubikey_api_key'); |
|
151 |
$api_key = hexencode(base64_decode($api_key), '', ''); |
|
152 |
} |
|
153 |
||
4
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
154 |
if ( isset($arr['h']) ) |
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
155 |
unset($arr['h']); |
73aecd46bb56
Should work with Yubico's official server now - forgot to account for newlines.
Dan
parents:
3
diff
changeset
|
156 |
|
0 | 157 |
$req = array(); |
158 |
foreach ( $arr as $key => $val ) |
|
159 |
{ |
|
160 |
$req[] = "$key=$val"; |
|
161 |
} |
|
162 |
$req = implode('&', $req); |
|
163 |
||
164 |
$sig = hmac_sha1($req, $api_key); |
|
165 |
$sig = hexdecode($sig); |
|
166 |
$sig = base64_encode($sig); |
|
167 |
||
168 |
return $sig; |
|
169 |
} |
|
170 |
||
171 |
$plugins->attachHook('compile_template', 'yubikey_attach_headers($this);'); |
|
172 |
||
173 |
function yubikey_attach_headers(&$template) |
|
174 |
{ |
|
3
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
175 |
if ( getConfig('yubikey_enable', '1') != '1' ) |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
176 |
return true; |
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
0
diff
changeset
|
177 |
|
0 | 178 |
$template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/yubikey/yubikey.js"></script>'); |
179 |
$template->add_header('<link rel="stylesheet" type="text/css" href="' . scriptPath . '/plugins/yubikey/yubikey.css" />'); |
|
180 |
} |
|
181 |