-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
218 lines (204 loc) · 6.9 KB
/
app.php
File metadata and controls
218 lines (204 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/* Login page
*
* Copyright 2009 Mo McRoberts.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) of this software may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
uses('form', 'auth');
if(!defined('DEFAULT_LOGIN_KIND')) define('DEFAULT_LOGIN_KIND', 'openid');
class LoginPage extends Page
{
protected $skin = 'login';
protected $templateName = 'login.phtml';
protected $loginForm;
protected $supportedMethods = array('GET', 'POST');
protected $loginKinds = array('openid' => 'an OpenID', 'default' => 'an E-mail address and password');
protected $kind = null;
protected $defaultSchemes = array();
public function __construct()
{
parent::__construct();
$this->defaultSchemes['openid'] = 'https';
}
protected function getObject()
{
if(isset($this->request->params[1]) && isset($this->request->objects[0]) && !strcmp($this->request->params[0], 'return'))
{
/* Validate and perform callback */
$scheme = $this->request->params[1];
$token = $this->request->objects[0];
if(isset($this->session->loginIRI) && isset($this->session->loginIRI[$token]) && !strcmp($this->session->loginIRI[$token][0], $scheme))
{
$engine = Auth::authEngineForScheme($scheme);
if(!$engine)
{
$this->loginForm()->errors[] = 'Incorrect sign-in name or password';
return true;
}
if(($udata = $engine->callback($this->request, $scheme, $this->session->loginIRI[$token][1])))
{
if($udata instanceof Exception)
{
$this->loginForm()->errors[] = $udata->getMessage();
}
else
{
$this->setIdentity($udata);
}
}
else
{
$this->loginForm()->errors[] = 'Incorrect sign-in name or password';
}
}
}
else if(isset($this->request->params[0]))
{
$this->kind = $this->request->params[0];
}
return true;
}
protected function assignTemplate()
{
parent::assignTemplate();
if(!isset($this->loginKinds[$this->kind])) $this->kind = null;
if($this->kind == null && isset($this->session->loginKind))
{
$this->kind = $this->session->loginKind;
}
if(!isset($this->loginKinds[$this->kind])) $this->kind = DEFAULT_LOGIN_KIND;
$this->session->begin($this->request);
$this->session->loginKind = $this->kind;
$this->session->commit();
if(!empty($this->session->user))
{
if(!empty($this->request->query['logout']))
{
$this->session->begin($this->request);
$this->session->user = null;
$this->session->commit();
}
return $this->redirect();
}
$this->useChromaHash();
$this->loginForm();
$urlparams = array();
if(isset($this->request->query['redirect']))
{
$this->loginForm['redirect'] = $this->request->query['redirect'];
$urlparams[] = 'redirect=' . urlencode($this->request->query['redirect']);
}
if(isset($this->defaultSchemes[$this->kind]))
{
$this->loginForm['defaultScheme'] = $this->defaultSchemes[$this->kind];
}
$urlparams[] = 'sid=' . urlencode($this->session->sid);
$this->vars['loginKinds'] = $this->loginKinds;
$this->vars['kind'] = $this->kind;
$this->vars['page_type'] = 'login login-' . $this->kind;
$this->vars['page_title'] = 'Sign in';
$this->vars['loginForm'] = $this->loginForm->render($this->request);
$this->vars['urlparams'] = implode(';', $urlparams);
}
protected function redirect()
{
if(isset($this->request->postData['redirect']))
{
$this->request->redirect($this->request->postData['redirect'], 302, true);
}
else if(isset($this->request->query['redirect']))
{
$this->request->redirect($this->request->query['redirect'], 302, true);
}
else
{
$this->request->redirect($this->request->base, 302, true);
}
return;
}
protected function performSubmission()
{
$iri = $this->loginForm['iri'];
$authData = $this->loginForm['auth'];
$scheme = null;
$engine = Auth::authEngineForIRI($iri, $scheme);
if(!$engine)
{
$this->loginForm->errors[] = 'Incorrect sign-in name or password';
return false;
}
$sessionToken = md5($this->session->nonce . $iri);
$this->session->begin($this->request);
if(!isset($this->session->loginIRI)) $this->session->loginIRI = array();
$this->session->loginIRI[$sessionToken] = array($scheme, $iri);
$callback = $this->request->absolutePage . 'return/' . $scheme . '/-/' . $sessionToken;
$this->session->commit();
$udata = $engine->verifyAuth($this->request, $scheme, $iri, $authData, $callback);
if($udata instanceof Exception)
{
$this->loginForm->errors[] = $udata->getMessage();
return false;
}
if(!$udata)
{
$this->loginForm->errors[] = 'Incorrect sign-in name or password';
return false;
}
$this->setIdentity($udata);
}
protected function setIdentity($userData)
{
$this->session->begin($this->request);
$this->session->user = $userData;
unset($this->session->loginIRI);
unset($this->session->nonce);
$this->session->commit();
}
protected function getForms()
{
$this->forms['login'] = $this->loginForm();
}
protected function loginForm()
{
if(!$this->loginForm)
{
$this->loginForm = new Form('login');
if($this->kind == 'openid')
{
$this->loginForm->field(array('name' => 'iri', 'type' => 'text', 'label' => 'OpenID:', 'required' => true));
$this->loginForm->field(array('name' => 'auth', 'type' => 'hidden', 'required' => false));
}
else
{
$this->loginForm->field(array('name' => 'iri', 'type' => 'text', 'label' => 'E-mail address:', 'required' => true));
$this->loginForm->field(array('name' => 'auth', 'type' => 'password', 'label' => 'Password:', 'required' => false));
}
$this->loginForm->field(array('name' => 'redirect', 'type' => 'hidden'));
$this->loginForm->submit('Sign in');
}
return $this->loginForm;
}
}