1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\Pages; |
10
|
|
|
|
11
|
|
|
use Waca\DataObjects\User; |
12
|
|
|
use Waca\Exceptions\AccessDeniedException; |
13
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
14
|
|
|
use Waca\Session; |
15
|
|
|
use Waca\Tasks\InternalPageBase; |
16
|
|
|
use Waca\WebRequest; |
17
|
|
|
|
18
|
|
|
class PageOAuth extends InternalPageBase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Attach entry point |
22
|
|
|
* |
23
|
|
|
* must be posted, or will redirect to preferences |
24
|
|
|
*/ |
25
|
|
|
protected function attach() |
26
|
|
|
{ |
27
|
|
|
if (!WebRequest::wasPosted()) { |
28
|
|
|
$this->redirect('preferences'); |
29
|
|
|
|
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->validateCSRFToken(); |
34
|
|
|
|
35
|
|
|
$oauthHelper = $this->getOAuthHelper(); |
36
|
|
|
$user = User::getCurrent($this->getDatabase()); |
37
|
|
|
|
38
|
|
|
$requestToken = $oauthHelper->getRequestToken(); |
39
|
|
|
|
40
|
|
|
$user->setOAuthRequestToken($requestToken->key); |
41
|
|
|
$user->setOAuthRequestSecret($requestToken->secret); |
42
|
|
|
$user->save(); |
43
|
|
|
|
44
|
|
|
$this->redirectUrl($oauthHelper->getAuthoriseUrl($requestToken->key)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Detach account entry point |
49
|
|
|
*/ |
50
|
|
|
protected function detach() |
51
|
|
|
{ |
52
|
|
|
if ($this->getSiteConfiguration()->getEnforceOAuth()) { |
53
|
|
|
throw new AccessDeniedException($this->getSecurityManager()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$user = User::getCurrent($this->getDatabase()); |
57
|
|
|
|
58
|
|
|
$user->setOnWikiName($user->getOnWikiName()); |
59
|
|
|
$user->setOAuthAccessSecret(null); |
60
|
|
|
$user->setOAuthAccessToken(null); |
61
|
|
|
$user->setOAuthRequestSecret(null); |
62
|
|
|
$user->setOAuthRequestToken(null); |
63
|
|
|
|
64
|
|
|
$user->clearOAuthData(); |
65
|
|
|
|
66
|
|
|
$user->setForcelogout(true); |
67
|
|
|
|
68
|
|
|
$user->save(); |
69
|
|
|
|
70
|
|
|
// force the user to log out |
71
|
|
|
Session::destroy(); |
72
|
|
|
|
73
|
|
|
$this->redirect('login'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Callback entry point |
78
|
|
|
*/ |
79
|
|
|
protected function callback() |
80
|
|
|
{ |
81
|
|
|
$oauthToken = WebRequest::getString('oauth_token'); |
82
|
|
|
$oauthVerifier = WebRequest::getString('oauth_verifier'); |
83
|
|
|
|
84
|
|
|
$this->doCallbackValidation($oauthToken, $oauthVerifier); |
85
|
|
|
|
86
|
|
|
$user = User::getByRequestToken($oauthToken, $this->getDatabase()); |
87
|
|
|
if ($user === false) { |
88
|
|
|
throw new ApplicationLogicException('Token not found in store, please try again'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$accessToken = $this->getOAuthHelper()->callbackCompleted( |
92
|
|
|
$user->getOAuthRequestToken(), |
93
|
|
|
$user->getOAuthRequestSecret(), |
94
|
|
|
$oauthVerifier); |
95
|
|
|
|
96
|
|
|
$user->setOAuthRequestSecret(null); |
97
|
|
|
$user->setOAuthRequestToken(null); |
98
|
|
|
$user->setOAuthAccessToken($accessToken->key); |
99
|
|
|
$user->setOAuthAccessSecret($accessToken->secret); |
100
|
|
|
|
101
|
|
|
// @todo we really should stop doing this kind of thing... it adds performance bottlenecks and breaks 3NF |
102
|
|
|
$user->setOnWikiName('##OAUTH##'); |
103
|
|
|
|
104
|
|
|
$user->save(); |
105
|
|
|
|
106
|
|
|
// OK, we're the same session that just did a partial login that was redirected to OAuth. Let's upgrade the |
107
|
|
|
// login to a full login |
108
|
|
|
if (WebRequest::getPartialLogin() === $user->getId()) { |
109
|
|
|
WebRequest::setLoggedInUser($user); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// My thinking is there are three cases here: |
113
|
|
|
// a) new user => redirect to prefs - it's the only thing they can access other than stats |
114
|
|
|
// b) existing user hit the connect button in prefs => redirect to prefs since it's where they were |
115
|
|
|
// c) existing user logging in => redirect to wherever they came from |
116
|
|
|
$redirectDestination = WebRequest::clearPostLoginRedirect(); |
117
|
|
|
if ($redirectDestination !== null && !$user->isNewUser()) { |
118
|
|
|
$this->redirectUrl($redirectDestination); |
119
|
|
|
} |
120
|
|
|
else { |
121
|
|
|
$this->redirect('preferences', null, null, 'internal.php'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Main function for this page, when no specific actions are called. |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected function main() |
130
|
|
|
{ |
131
|
|
|
$this->redirect('preferences'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $oauthToken |
136
|
|
|
* @param string $oauthVerifier |
137
|
|
|
* |
138
|
|
|
* @throws ApplicationLogicException |
139
|
|
|
*/ |
140
|
|
|
protected function doCallbackValidation($oauthToken, $oauthVerifier) |
141
|
|
|
{ |
142
|
|
|
if ($oauthToken === null) { |
143
|
|
|
throw new ApplicationLogicException('No token provided'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($oauthVerifier === null) { |
147
|
|
|
throw new ApplicationLogicException('No oauth verifier provided.'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|