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';
36 class ApiAuthAction extends TwitterapiAction
39 * Does this API resource require authentication?
41 * @return boolean true
44 function requiresAuth()
49 function checkBasicAuthUser()
51 $this->basicAuthProcessHeader();
53 if (!isset($this->auth_user)) {
54 header('WWW-Authenticate: Basic realm="StatusNet API"');
56 // show error if the user clicks 'cancel'
58 $this->showBasicAuthError();
62 $nickname = $this->auth_user;
63 $password = $this->auth_pw;
64 $this->auth_user = common_check_user($nickname, $password);
66 if (empty($this->auth_user)) {
68 // basic authentication failed
70 list($proxy, $ip) = common_client_ip();
71 common_log(LOG_WARNING,
72 "Failed API auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
73 $this->showBasicAuthError();
80 function basicAuthProcessHeader()
82 if (isset($_SERVER['AUTHORIZATION']) || isset($_SERVER['HTTP_AUTHORIZATION'])) {
83 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])? $_SERVER['HTTP_AUTHORIZATION'] : $_SERVER['AUTHORIZATION'];
86 if (isset($_SERVER['PHP_AUTH_USER'])) {
87 $this->auth_user = $_SERVER['PHP_AUTH_USER'];
88 $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
89 } elseif (isset($authorization_header) && strstr(substr($authorization_header, 0, 5), 'Basic')) {
90 // decode the HTTP_AUTHORIZATION header on php-cgi server self
91 // on fcgid server the header name is AUTHORIZATION
93 $auth_hash = base64_decode(substr($authorization_header, 6));
94 list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
96 // set all to null on a empty basic auth request
97 if ($this->auth_user == "") {
98 $this->auth_user = null;
99 $this->auth_pw = null;
102 $this->auth_user = null;
103 $this->auth_pw = null;
107 function showBasicAuthError()
109 header('HTTP/1.1 401 Unauthorized');
110 $msg = 'Could not authenticate you.';
112 if ($this->arg('format') == 'xml') {
113 header('Content-Type: application/xml; charset=utf-8');
115 $this->elementStart('hash');
116 $this->element('error', null, $msg);
117 $this->element('request', null, $_SERVER['REQUEST_URI']);
118 $this->elementEnd('hash');
120 } elseif ($this->arg('format') == 'json') {
121 header('Content-Type: application/json; charset=utf-8');
122 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
123 print(json_encode($error_array));
125 header('Content-type: text/plain');