]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apiauth.php
Merge branch 'diskcache' into 0.9.x
[quix0rs-gnu-social.git] / lib / apiauth.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for API actions that require authentication
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  API
23  * @package   StatusNet
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/
35  */
36
37 if (!defined('STATUSNET')) {
38     exit(1);
39 }
40
41 require_once INSTALLDIR . '/lib/api.php';
42
43 /**
44  * Actions extending this class will require auth
45  *
46  * @category API
47  * @package  StatusNet
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/
51  */
52
53 class ApiAuthAction extends ApiAction
54 {
55
56     /**
57      * Take arguments for running, and output basic auth header if needed
58      *
59      * @param array $args $_REQUEST args
60      *
61      * @return boolean success flag
62      *
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         if ($this->requiresAuth()) {
70             $this->checkBasicAuthUser();
71         }
72
73         return true;
74     }
75
76     /**
77      * Does this API resource require authentication?
78      *
79      * @return boolean true
80      */
81
82     function requiresAuth()
83     {
84         return true;
85     }
86
87     /**
88      * Check for a user specified via HTTP basic auth. If there isn't
89      * one, try to get one by outputting the basic auth header.
90      *
91      * @return boolean true or false
92      */
93
94     function checkBasicAuthUser()
95     {
96         $this->basicAuthProcessHeader();
97
98         $realm = common_config('site', 'name') . ' API';
99
100         if (!isset($this->auth_user)) {
101             header('WWW-Authenticate: Basic realm="' . $realm . '"');
102
103             // show error if the user clicks 'cancel'
104
105             $this->showBasicAuthError();
106             exit;
107
108         } else {
109             $nickname = $this->auth_user;
110             $password = $this->auth_pw;
111             $user = common_check_user($nickname, $password);
112             if (Event::handle('StartSetApiUser', array(&$user))) {
113                 $this->auth_user = $user;
114                 Event::handle('EndSetApiUser', array($user));
115             }
116
117             if (empty($this->auth_user)) {
118
119                 // basic authentication failed
120
121                 list($proxy, $ip) = common_client_ip();
122                 common_log(
123                     LOG_WARNING,
124                     'Failed API auth attempt, nickname = ' .
125                     "$nickname, proxy = $proxy, ip = $ip."
126                 );
127                 $this->showBasicAuthError();
128                 exit;
129             }
130         }
131         return true;
132     }
133
134     /**
135      * Read the HTTP headers and set the auth user.  Decodes HTTP_AUTHORIZATION
136      * param to support basic auth when PHP is running in CGI mode.
137      *
138      * @return void
139      */
140
141     function basicAuthProcessHeader()
142     {
143         if (isset($_SERVER['AUTHORIZATION'])
144             || isset($_SERVER['HTTP_AUTHORIZATION'])
145         ) {
146                 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])
147                 ? $_SERVER['HTTP_AUTHORIZATION'] : $_SERVER['AUTHORIZATION'];
148         }
149
150         if (isset($_SERVER['PHP_AUTH_USER'])) {
151             $this->auth_user = $_SERVER['PHP_AUTH_USER'];
152             $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
153         } elseif (isset($authorization_header)
154             && strstr(substr($authorization_header, 0, 5), 'Basic')) {
155
156             // decode the HTTP_AUTHORIZATION header on php-cgi server self
157             // on fcgid server the header name is AUTHORIZATION
158
159             $auth_hash = base64_decode(substr($authorization_header, 6));
160             list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
161
162             // set all to null on a empty basic auth request
163
164             if ($this->auth_user == "") {
165                 $this->auth_user = null;
166                 $this->auth_pw = null;
167             }
168         } else {
169             $this->auth_user = null;
170             $this->auth_pw = null;
171         }
172     }
173
174     /**
175      * Output an authentication error message.  Use XML or JSON if one
176      * of those formats is specified, otherwise output plain text
177      *
178      * @return void
179      */
180
181     function showBasicAuthError()
182     {
183         header('HTTP/1.1 401 Unauthorized');
184         $msg = 'Could not authenticate you.';
185
186         if ($this->format == 'xml') {
187             header('Content-Type: application/xml; charset=utf-8');
188             $this->startXML();
189             $this->elementStart('hash');
190             $this->element('error', null, $msg);
191             $this->element('request', null, $_SERVER['REQUEST_URI']);
192             $this->elementEnd('hash');
193             $this->endXML();
194         } elseif ($this->format == 'json') {
195             header('Content-Type: application/json; charset=utf-8');
196             $error_array = array('error' => $msg,
197                                  'request' => $_SERVER['REQUEST_URI']);
198             print(json_encode($error_array));
199         } else {
200             header('Content-type: text/plain');
201             print "$msg\n";
202         }
203     }
204
205 }