]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apiauth.php
Delete action/api.php and rename lib/twitterapi.php to lib/api.php
[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    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/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/api.php';
35
36 /**
37  * Actions extending this class will require auth
38  *
39  * @category API
40  * @package  StatusNet
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/
44  */
45
46 class ApiAuthAction extends ApiAction
47 {
48
49     var $auth_user = null;
50
51     /**
52      * Does this API resource require authentication?
53      *
54      * @return boolean true
55      */
56
57     function requiresAuth()
58     {
59         return true;
60     }
61
62     /**
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.
65      *
66      * @return boolean true or false
67      */
68
69     function checkBasicAuthUser()
70     {
71         $this->basicAuthProcessHeader();
72
73         if (!isset($this->auth_user)) {
74             header('WWW-Authenticate: Basic realm="StatusNet API"');
75
76             // show error if the user clicks 'cancel'
77
78             $this->showBasicAuthError();
79             return false;
80
81         } else {
82             $nickname = $this->auth_user;
83             $password = $this->auth_pw;
84             $this->auth_user = common_check_user($nickname, $password);
85
86             if (empty($this->auth_user)) {
87
88                 // basic authentication failed
89
90                 list($proxy, $ip) = common_client_ip();
91                 common_log(
92                     LOG_WARNING,
93                     'Failed API auth attempt, nickname = ' .
94                     "$nickname, proxy = $proxy, ip = $ip."
95                 );
96                 $this->showBasicAuthError();
97                 return false;
98             }
99         }
100         return true;
101     }
102
103     /**
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.
106      *
107      * @return void
108      */
109
110     function basicAuthProcessHeader()
111     {
112         if (isset($_SERVER['AUTHORIZATION'])
113             || isset($_SERVER['HTTP_AUTHORIZATION'])
114         ) {
115                 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])
116                 ? $_SERVER['HTTP_AUTHORIZATION'] : $_SERVER['AUTHORIZATION'];
117         }
118
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')) {
124
125             // decode the HTTP_AUTHORIZATION header on php-cgi server self
126             // on fcgid server the header name is AUTHORIZATION
127
128             $auth_hash = base64_decode(substr($authorization_header, 6));
129             list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
130
131             // set all to null on a empty basic auth request
132
133             if ($this->auth_user == "") {
134                 $this->auth_user = null;
135                 $this->auth_pw = null;
136             }
137         } else {
138             $this->auth_user = null;
139             $this->auth_pw = null;
140         }
141     }
142
143     /**
144      * Output an authentication error message.  Use XML or JSON if one
145      * of those formats is specified, otherwise output plain text
146      *
147      * @return void
148      */
149
150     function showBasicAuthError()
151     {
152         header('HTTP/1.1 401 Unauthorized');
153         $msg = 'Could not authenticate you.';
154
155         if ($this->arg('format') == 'xml') {
156             header('Content-Type: application/xml; charset=utf-8');
157             $this->startXML();
158             $this->elementStart('hash');
159             $this->element('error', null, $msg);
160             $this->element('request', null, $_SERVER['REQUEST_URI']);
161             $this->elementEnd('hash');
162             $this->endXML();
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));
168         } else {
169             header('Content-type: text/plain');
170             print "$msg\n";
171         }
172     }
173
174 }