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