enwikipedia-acc /
waca
| 1 | <?php |
||
| 2 | /****************************************************************************** |
||
| 3 | * Wikipedia Account Creation Assistance tool * |
||
| 4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
| 5 | * * |
||
| 6 | * This is free and unencumbered software released into the public domain. * |
||
| 7 | * Please see LICENSE.md for the full licencing statement. * |
||
| 8 | ******************************************************************************/ |
||
| 9 | |||
| 10 | namespace Waca\Pages\UserAuth; |
||
| 11 | |||
| 12 | use Exception; |
||
| 13 | use Waca\Exceptions\ApplicationLogicException; |
||
| 14 | use Waca\Exceptions\CurlException; |
||
| 15 | use Waca\Exceptions\OptimisticLockFailedException; |
||
| 16 | use Waca\Helpers\OAuthUserHelper; |
||
| 17 | use Waca\Tasks\InternalPageBase; |
||
| 18 | use Waca\WebRequest; |
||
| 19 | |||
| 20 | class PageOAuthCallback extends InternalPageBase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @return bool |
||
| 24 | */ |
||
| 25 | protected function isProtectedPage() |
||
| 26 | { |
||
| 27 | // This page is critical to ensuring OAuth functionality is operational. |
||
| 28 | return false; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Main function for this page, when no specific actions are called. |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | protected function main() |
||
| 36 | { |
||
| 37 | // This should never get hit except by URL manipulation. |
||
| 38 | $this->redirect(''); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Registered endpoint for the account creation callback. |
||
| 43 | * |
||
| 44 | * If this ever gets hit, something is wrong somewhere. |
||
| 45 | */ |
||
| 46 | protected function create() |
||
| 47 | { |
||
| 48 | throw new Exception('OAuth account creation endpoint triggered.'); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Callback entry point |
||
| 53 | * @throws ApplicationLogicException |
||
| 54 | * @throws OptimisticLockFailedException |
||
| 55 | */ |
||
| 56 | protected function authorise() |
||
| 57 | { |
||
| 58 | $oauthToken = WebRequest::getString('oauth_token'); |
||
| 59 | $oauthVerifier = WebRequest::getString('oauth_verifier'); |
||
| 60 | |||
| 61 | $this->doCallbackValidation($oauthToken, $oauthVerifier); |
||
| 62 | |||
| 63 | $database = $this->getDatabase(); |
||
| 64 | |||
| 65 | $user = OAuthUserHelper::findUserByRequestToken($oauthToken, $database); |
||
| 66 | $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
||
| 67 | |||
| 68 | try { |
||
| 69 | $oauth->completeHandshake($oauthVerifier); |
||
| 70 | } |
||
| 71 | catch (CurlException $ex) { |
||
| 72 | throw new ApplicationLogicException($ex->getMessage(), 0, $ex); |
||
| 73 | } |
||
| 74 | |||
| 75 | // OK, we're the same session that just did a partial login that was redirected to OAuth. Let's upgrade the |
||
| 76 | // login to a full login |
||
| 77 | if (WebRequest::getOAuthPartialLogin() === $user->getId()) { |
||
| 78 | WebRequest::setLoggedInUser($user); |
||
| 79 | $this->getDomainAccessManager()->switchToDefaultDomain($user); |
||
| 80 | } |
||
| 81 | |||
| 82 | // My thinking is there are three cases here: |
||
| 83 | // a) new user => redirect to prefs - it's the only thing they can access other than stats |
||
| 84 | // b) existing user hit the connect button in prefs => redirect to prefs since it's where they were |
||
| 85 | // c) existing user logging in => redirect to wherever they came from |
||
| 86 | $redirectDestination = WebRequest::clearPostLoginRedirect(); |
||
| 87 | if ($redirectDestination !== null && !$user->isNewUser()) { |
||
| 88 | $this->redirectUrl($redirectDestination); |
||
| 89 | } |
||
| 90 | else { |
||
| 91 | $this->redirect('preferences', null, null, 'internal.php'); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $oauthToken |
||
| 97 | * @param string $oauthVerifier |
||
| 98 | * |
||
| 99 | * @throws ApplicationLogicException |
||
| 100 | */ |
||
| 101 | private function doCallbackValidation($oauthToken, $oauthVerifier) |
||
| 102 | { |
||
| 103 | if ($oauthToken === null) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 104 | throw new ApplicationLogicException('No token provided'); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($oauthVerifier === null) { |
||
|
0 ignored issues
–
show
|
|||
| 108 | throw new ApplicationLogicException('No oauth verifier provided.'); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |