]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/otp.php
Merge branch 'sessionidparam' into 0.9.x
[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
49 class OtpAction extends Action
50 {
51     var $user;
52     var $token;
53     var $rememberme;
54     var $returnto;
55     var $lt;
56
57     function prepare($args)
58     {
59         parent::prepare($args);
60
61         if (common_is_real_login()) {
62             $this->clientError(_('Already logged in.'));
63             return false;
64         }
65
66         $id = $this->trimmed('user_id');
67
68         if (empty($id)) {
69             $this->clientError(_('No user ID specified.'));
70             return false;
71         }
72
73         $this->user = User::staticGet('id', $id);
74
75         if (empty($this->user)) {
76             $this->clientError(_('No such user.'));
77             return false;
78         }
79
80         $this->token = $this->trimmed('token');
81
82         if (empty($this->token)) {
83             $this->clientError(_('No login token specified.'));
84             return false;
85         }
86
87         $this->lt = Login_token::staticGet('user_id', $id);
88
89         if (empty($this->lt)) {
90             $this->clientError(_('No login token requested.'));
91             return false;
92         }
93
94         if ($this->lt->token != $this->token) {
95             $this->clientError(_('Invalid login token specified.'));
96             return false;
97         }
98
99         if ($this->lt->modified > time() + Login_token::TIMEOUT) {
100             //token has expired
101             //delete the token as it is useless
102             $this->lt->delete();
103             $this->lt = null;
104             $this->clientError(_('Login token expired.'));
105             return false;
106         }
107
108         $this->rememberme = $this->boolean('rememberme');
109         $this->returnto = $this->trimmed('returnto');
110
111         return true;
112     }
113
114         function handle($args)
115     {
116         parent::handle($args);
117
118         // success!
119         if (!common_set_user($this->user)) {
120             $this->serverError(_('Error setting user. You are probably not authorized.'));
121             return;
122         }
123
124         // We're now logged in; disable the lt
125
126         $this->lt->delete();
127         $this->lt = null;
128
129         if ($this->rememberme) {
130             common_rememberme($this->user);
131         }
132
133         if (!empty($this->returnto)) {
134             $url = $this->returnto;
135             // We don't have to return to it again
136             common_set_returnto(null);
137         } else {
138             $url = common_local_url('all',
139                                     array('nickname' =>
140                                           $this->user->nickname));
141         }
142
143         common_redirect($url, 303);
144     }
145 }