3 * StatusNet, the distributed open-source microblogging tool
5 * Base class for API actions that require authentication
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR.'/lib/twitterapi.php';
37 * Actions extending this class will require auth
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
46 class ApiAuthAction extends TwitterapiAction
49 var $auth_user = null;
52 * Does this API resource require authentication?
54 * @return boolean true
57 function requiresAuth()
63 * Check for a user specified via HTTP basic auth. If there isn't
64 * one, try to get one by outputting the basic auth header.
66 * @return boolean true or false
69 function checkBasicAuthUser()
71 $this->basicAuthProcessHeader();
73 if (!isset($this->auth_user)) {
74 header('WWW-Authenticate: Basic realm="StatusNet API"');
76 // show error if the user clicks 'cancel'
78 $this->showBasicAuthError();
82 $nickname = $this->auth_user;
83 $password = $this->auth_pw;
84 $this->auth_user = common_check_user($nickname, $password);
86 if (empty($this->auth_user)) {
88 // basic authentication failed
90 list($proxy, $ip) = common_client_ip();
93 'Failed API auth attempt, nickname = ' .
94 "$nickname, proxy = $proxy, ip = $ip."
96 $this->showBasicAuthError();
104 * Read the HTTP headers and set the auth user. Decodes HTTP_AUTHORIZATION
105 * param to support basic auth when PHP is running in CGI mode.
110 function basicAuthProcessHeader()
112 if (isset($_SERVER['AUTHORIZATION'])
113 || isset($_SERVER['HTTP_AUTHORIZATION'])
115 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])
116 ? $_SERVER['HTTP_AUTHORIZATION'] : $_SERVER['AUTHORIZATION'];
119 if (isset($_SERVER['PHP_AUTH_USER'])) {
120 $this->auth_user = $_SERVER['PHP_AUTH_USER'];
121 $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
122 } elseif (isset($authorization_header)
123 && strstr(substr($authorization_header, 0, 5), 'Basic')) {
125 // decode the HTTP_AUTHORIZATION header on php-cgi server self
126 // on fcgid server the header name is AUTHORIZATION
128 $auth_hash = base64_decode(substr($authorization_header, 6));
129 list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
131 // set all to null on a empty basic auth request
133 if ($this->auth_user == "") {
134 $this->auth_user = null;
135 $this->auth_pw = null;
138 $this->auth_user = null;
139 $this->auth_pw = null;
144 * Output an authentication error message. Use XML or JSON if one
145 * of those formats is specified, otherwise output plain text
150 function showBasicAuthError()
152 header('HTTP/1.1 401 Unauthorized');
153 $msg = 'Could not authenticate you.';
155 if ($this->arg('format') == 'xml') {
156 header('Content-Type: application/xml; charset=utf-8');
158 $this->elementStart('hash');
159 $this->element('error', null, $msg);
160 $this->element('request', null, $_SERVER['REQUEST_URI']);
161 $this->elementEnd('hash');
163 } elseif ($this->arg('format') == 'json') {
164 header('Content-Type: application/json; charset=utf-8');
165 $error_array = array('error' => $msg,
166 'request' => $_SERVER['REQUEST_URI']);
167 print(json_encode($error_array));
169 header('Content-type: text/plain');