]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/otp.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / otp.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Allow one-time password login
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  Login
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Allow one-time password login
36  *
37  * This action will automatically log in the user identified by the user_id
38  * parameter. A login_token record must be constructed beforehand, typically
39  * by code where the user is already authenticated.
40  *
41  * @category  Login
42  * @package   StatusNet
43  * @author    Evan Prodromou <evan@status.net>
44  * @copyright 2010 StatusNet, Inc.
45  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
46  * @link      http://status.net/
47  */
48 class OtpAction extends Action
49 {
50     var $user;
51     var $token;
52     var $rememberme;
53     var $returnto;
54     var $lt;
55
56     function prepare($args)
57     {
58         parent::prepare($args);
59
60         if (common_is_real_login()) {
61             // TRANS: Client error displayed trying to use "one time password login" when already logged in.
62             $this->clientError(_('Already logged in.'));
63         }
64
65         $id = $this->trimmed('user_id');
66
67         if (empty($id)) {
68             // TRANS: Client error displayed trying to use "one time password login" without specifying a user.
69             $this->clientError(_('No user ID specified.'));
70         }
71
72         $this->user = User::getKV('id', $id);
73
74         if (empty($this->user)) {
75             // TRANS: Client error displayed trying to use "one time password login" without using an existing user.
76             $this->clientError(_('No such user.'));
77         }
78
79         $this->token = $this->trimmed('token');
80
81         if (empty($this->token)) {
82             // TRANS: Client error displayed trying to use "one time password login" without specifying a login token.
83             $this->clientError(_('No login token specified.'));
84         }
85
86         $this->lt = Login_token::getKV('user_id', $id);
87
88         if (empty($this->lt)) {
89             // TRANS: Client error displayed trying to use "one time password login" without requesting a login token.
90             $this->clientError(_('No login token requested.'));
91         }
92
93         if ($this->lt->token != $this->token) {
94             // TRANS: Client error displayed trying to use "one time password login" while specifying an invalid login token.
95             $this->clientError(_('Invalid login token specified.'));
96         }
97
98         if ($this->lt->modified > time() + Login_token::TIMEOUT) {
99             //token has expired
100             //delete the token as it is useless
101             $this->lt->delete();
102             $this->lt = null;
103             // TRANS: Client error displayed trying to use "one time password login" while specifying an expired login token.
104             $this->clientError(_('Login token expired.'));
105         }
106
107         $this->rememberme = $this->boolean('rememberme');
108         $this->returnto = $this->trimmed('returnto');
109
110         return true;
111     }
112
113     function handle($args)
114     {
115         parent::handle($args);
116
117         // success!
118         if (!common_set_user($this->user)) {
119             // TRANS: Server error displayed when a user object could not be created trying to login using "one time password login".
120             $this->serverError(_('Error setting user. You are probably not authorized.'));
121         }
122
123         // We're now logged in; disable the lt
124
125         $this->lt->delete();
126         $this->lt = null;
127
128         common_real_login(true);
129
130         if ($this->rememberme) {
131             common_rememberme($this->user);
132         }
133
134         if (!empty($this->returnto)) {
135             $url = $this->returnto;
136             // We don't have to return to it again
137             common_set_returnto(null);
138         } else {
139             $url = common_local_url('all',
140                                     array('nickname' =>
141                                           $this->user->nickname));
142         }
143
144         common_redirect($url, 303);
145     }
146 }