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