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 Adrian Lang <mail@adrianlang.de>
25 * @author Brenda Wallace <shiny@cpan.org>
26 * @author Craig Andrews <candrews@integralblue.com>
27 * @author Dan Moore <dan@moore.cx>
28 * @author Evan Prodromou <evan@status.net>
29 * @author mEDI <medi@milaro.net>
30 * @author Sarven Capadisli <csarven@status.net>
31 * @author Zach Copley <zach@status.net>
32 * @copyright 2009 StatusNet, Inc.
33 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
34 * @link http://status.net/
37 if (!defined('STATUSNET')) {
41 require_once INSTALLDIR . '/lib/api.php';
44 * Actions extending this class will require auth
48 * @author Zach Copley <zach@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
53 class ApiAuthAction extends ApiAction
56 var $auth_user = null;
59 * Take arguments for running, and output basic auth header if needed
61 * @param array $args $_REQUEST args
63 * @return boolean success flag
67 function prepare($args)
69 common_debug('ApiAction::prepare()');
70 parent::prepare($args);
72 if ($this->requiresAuth()) {
73 $this->checkBasicAuthUser();
80 * Does this API resource require authentication?
82 * @return boolean true
85 function requiresAuth()
91 * Check for a user specified via HTTP basic auth. If there isn't
92 * one, try to get one by outputting the basic auth header.
94 * @return boolean true or false
97 function checkBasicAuthUser()
99 $this->basicAuthProcessHeader();
101 $realm = common_config('site', 'name') . ' API';
103 if (!isset($this->auth_user)) {
104 header('WWW-Authenticate: Basic realm="' . $realm . '"');
106 // show error if the user clicks 'cancel'
108 $this->showBasicAuthError();
112 $nickname = $this->auth_user;
113 $password = $this->auth_pw;
114 $this->auth_user = common_check_user($nickname, $password);
116 if (empty($this->auth_user)) {
118 // basic authentication failed
120 list($proxy, $ip) = common_client_ip();
123 'Failed API auth attempt, nickname = ' .
124 "$nickname, proxy = $proxy, ip = $ip."
126 $this->showBasicAuthError();
134 * Read the HTTP headers and set the auth user. Decodes HTTP_AUTHORIZATION
135 * param to support basic auth when PHP is running in CGI mode.
140 function basicAuthProcessHeader()
142 if (isset($_SERVER['AUTHORIZATION'])
143 || isset($_SERVER['HTTP_AUTHORIZATION'])
145 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])
146 ? $_SERVER['HTTP_AUTHORIZATION'] : $_SERVER['AUTHORIZATION'];
149 if (isset($_SERVER['PHP_AUTH_USER'])) {
150 $this->auth_user = $_SERVER['PHP_AUTH_USER'];
151 $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
152 } elseif (isset($authorization_header)
153 && strstr(substr($authorization_header, 0, 5), 'Basic')) {
155 // decode the HTTP_AUTHORIZATION header on php-cgi server self
156 // on fcgid server the header name is AUTHORIZATION
158 $auth_hash = base64_decode(substr($authorization_header, 6));
159 list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
161 // set all to null on a empty basic auth request
163 if ($this->auth_user == "") {
164 $this->auth_user = null;
165 $this->auth_pw = null;
168 $this->auth_user = null;
169 $this->auth_pw = null;
174 * Output an authentication error message. Use XML or JSON if one
175 * of those formats is specified, otherwise output plain text
180 function showBasicAuthError()
182 header('HTTP/1.1 401 Unauthorized');
183 $msg = 'Could not authenticate you.';
185 if ($this->format == 'xml') {
186 header('Content-Type: application/xml; charset=utf-8');
188 $this->elementStart('hash');
189 $this->element('error', null, $msg);
190 $this->element('request', null, $_SERVER['REQUEST_URI']);
191 $this->elementEnd('hash');
193 } elseif ($this->format == 'json') {
194 header('Content-Type: application/json; charset=utf-8');
195 $error_array = array('error' => $msg,
196 'request' => $_SERVER['REQUEST_URI']);
197 print(json_encode($error_array));
199 header('Content-type: text/plain');