diff --git a/src/applications/auth/provider/PhabricatorAuthProviderOAuth1MediaWiki.php b/src/applications/auth/provider/PhabricatorAuthProviderOAuth1MediaWiki.php new file mode 100644 --- /dev/null +++ b/src/applications/auth/provider/PhabricatorAuthProviderOAuth1MediaWiki.php @@ -0,0 +1,103 @@ +getLoginURI()); + + return pht( + "To configure MediaWiki OAuth, create a new application here:". + "\n\n". + "https://www.mediawiki.org/wiki/Special:OAuthConsumerRegistration/propose". + "\n\n". + "When creating your application, use these settings:". + "\n\n". + " - **Callback URL:** Set this to: `%s`". + "\n\n". + "After completing configuration, copy the **Consumer Key** and ". + "**Consumer Secret** to the fields above.", + $login_uri); + } + + protected function newOAuthAdapter() { + return new PhutilAuthAdapterOAuthMediaWiki(); + } + + protected function getLoginIcon() { + return 'MediaWiki'; + } + + public function processLoginRequest( + PhabricatorAuthLoginController $controller) { + + $request = $controller->getRequest(); + $adapter = $this->getAdapter(); + $account = null; + $response = null; + + if ($request->isHTTPPost()) { + $callback_uri = $adapter->getCallbackURI(); + $adapter->setCallbackURI($callback_uri); + $uri = $adapter->getClientRedirectURI(); + $response = id(new AphrontRedirectResponse())->setURI($uri); + return array($account, $response); + } + + $denied = $request->getStr('denied'); + if (strlen($denied)) { + // Twitter indicates that the user cancelled the login attempt by + // returning "denied" as a parameter. + throw new PhutilAuthUserAbortedException(); + } + + // NOTE: You can get here via GET, this should probably be a bit more + // user friendly. + + $token = $request->getStr('oauth_token'); + $verifier = $request->getStr('oauth_verifier'); + + if (!$token) { + throw new Exception("Expected 'oauth_token' in request!"); + } + + if (!$verifier) { + throw new Exception("Expected 'oauth_verifier' in request!"); + } + + list ( $ctok, $csec ) = explode(':', $_COOKIE['mwoauth'], 2); + if ($ctok !== $token) { + throw new Exception("Token from callback doesn't match your cookie."); + } + + $adapter->setToken($token); + $adapter->setTokenSecret($csec); + $adapter->setVerifier($verifier); + + // NOTE: As a side effect, this will cause the OAuth adapter to request + // an access token. + + try { + $account_id = $adapter->getAccountID(); + } catch (Exception $ex) { + throw $ex; + } + + if (!strlen($account_id)) { + $response = $controller->buildProviderErrorResponse( + $this, + pht( + 'The OAuth provider failed to retrieve an account ID.')); + + return array($account, $response); + } + + return array($this->loadOrCreateAccount($account_id), $response); + } + + +}