Page MenuHomePhabricator

D9203.id21849.diff
No OneTemporary

D9203.id21849.diff

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 @@
+<?php
+
+final class PhabricatorAuthProviderOAuth1MediaWiki
+ extends PhabricatorAuthProviderOAuth1 {
+
+ public function getProviderName() {
+ return pht('MediaWiki');
+ }
+
+ protected function getProviderConfigurationHelp() {
+ $login_uri = PhabricatorEnv::getURI($this->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);
+ }
+
+
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 27, 7:50 AM (3 w, 8 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7718862
Default Alt Text
D9203.id21849.diff (3 KB)

Event Timeline