From e5b4ce5525d7b8e6debfb6a78f8062fd4e2dd396 Mon Sep 17 00:00:00 2001
From: epriestley <git@epriestley.com>
Date: Tue, 3 Sep 2013 10:30:53 -0700
Subject: [PATCH] Reduce the amount of OAuth1/OAuth2 code duplication for
 rendering login buttons

Summary: Ref T3687. These buttons don't work quite the same way, but are similar enough that the code seems worth consolidating.

Test Plan: Viewed and clicked both OAuth1 (Twitter, JIRA) and OAuth2 (Facebook) login buttons. Got logins.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T3687

Differential Revision: https://secure.phabricator.com/D6874
---
 .../auth/provider/PhabricatorAuthProvider.php | 76 +++++++++++++++++++
 .../provider/PhabricatorAuthProviderOAuth.php | 51 ++-----------
 .../PhabricatorAuthProviderOAuth1.php         | 53 ++-----------
 3 files changed, 87 insertions(+), 93 deletions(-)

diff --git a/src/applications/auth/provider/PhabricatorAuthProvider.php b/src/applications/auth/provider/PhabricatorAuthProvider.php
index b02594f068..32fb052b4b 100644
--- a/src/applications/auth/provider/PhabricatorAuthProvider.php
+++ b/src/applications/auth/provider/PhabricatorAuthProvider.php
@@ -360,4 +360,80 @@ abstract class PhabricatorAuthProvider {
     return false;
   }
 
+
+  /**
+   * Render a standard login/register button element.
+   *
+   * The `$attributes` parameter takes these keys:
+   *
+   *   - `uri`: URI the button should take the user to when clicked.
+   *   - `method`: Optional HTTP method the button should use, defaults to GET.
+   *
+   * @param   AphrontRequest  HTTP request.
+   * @param   string          Request mode string.
+   * @param   map             Additional parameters, see above.
+   * @return  wild            Login button.
+   */
+  protected function renderStandardLoginButton(
+    AphrontRequest $request,
+    $mode,
+    array $attributes = array()) {
+
+    PhutilTypeSpec::checkMap(
+      $attributes,
+      array(
+        'method' => 'optional string',
+        'uri' => 'string',
+      ));
+
+    $viewer = $request->getUser();
+    $adapter = $this->getAdapter();
+
+    if ($mode == 'link') {
+      $button_text = pht('Link External Account');
+    } else if ($mode == 'refresh') {
+      $button_text = pht('Refresh Account Link');
+    } else if ($this->shouldAllowRegistration()) {
+      $button_text = pht('Login or Register');
+    } else {
+      $button_text = pht('Login');
+    }
+
+    $icon = id(new PHUIIconView())
+      ->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
+      ->setSpriteIcon($this->getLoginIcon());
+
+    $button = id(new PHUIButtonView())
+        ->setSize(PHUIButtonView::BIG)
+        ->setColor(PHUIButtonView::GREY)
+        ->setIcon($icon)
+        ->setText($button_text)
+        ->setSubtext($this->getProviderName());
+
+    $uri = $attributes['uri'];
+    $uri = new PhutilURI($uri);
+    $params = $uri->getQueryParams();
+    $uri->setQueryParams(array());
+
+    $content = array($button);
+
+    foreach ($params as $key => $value) {
+      $content[] = phutil_tag(
+        'input',
+        array(
+          'type' => 'hidden',
+          'name' => $key,
+          'value' => $value,
+        ));
+    }
+
+    return phabricator_form(
+      $viewer,
+      array(
+        'method' => idx($attributes, 'method', 'GET'),
+        'action' => (string)$uri,
+      ),
+      $content);
+  }
+
 }
diff --git a/src/applications/auth/provider/PhabricatorAuthProviderOAuth.php b/src/applications/auth/provider/PhabricatorAuthProviderOAuth.php
index 88d1b60369..9578e3e2f4 100644
--- a/src/applications/auth/provider/PhabricatorAuthProviderOAuth.php
+++ b/src/applications/auth/provider/PhabricatorAuthProviderOAuth.php
@@ -34,56 +34,17 @@ abstract class PhabricatorAuthProviderOAuth extends PhabricatorAuthProvider {
   }
 
   protected function renderLoginForm(AphrontRequest $request, $mode) {
-    $viewer = $request->getUser();
-
-    if ($mode == 'link') {
-      $button_text = pht('Link External Account');
-    } else if ($mode == 'refresh') {
-      $button_text = pht('Refresh Account Link');
-    } else if ($this->shouldAllowRegistration()) {
-      $button_text = pht('Login or Register');
-    } else {
-      $button_text = pht('Login');
-    }
-
-    $icon = id(new PHUIIconView())
-      ->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
-      ->setSpriteIcon($this->getLoginIcon());
-
-    $button = id(new PHUIButtonView())
-        ->setSize(PHUIButtonView::BIG)
-        ->setColor(PHUIButtonView::GREY)
-        ->setIcon($icon)
-        ->setText($button_text)
-        ->setSubtext($this->getProviderName());
-
     $adapter = $this->getAdapter();
     $adapter->setState(PhabricatorHash::digest($request->getCookie('phcid')));
 
-    $uri = new PhutilURI($adapter->getAuthenticateURI());
-    $params = $uri->getQueryParams();
-    $uri->setQueryParams(array());
+    $attributes = array(
+      'method' => 'GET',
+      'uri' => $adapter->getAuthenticateURI(),
+    );
 
-    $content = array($button);
-
-    foreach ($params as $key => $value) {
-      $content[] = phutil_tag(
-        'input',
-        array(
-          'type' => 'hidden',
-          'name' => $key,
-          'value' => $value,
-        ));
-    }
-
-    return phabricator_form(
-      $viewer,
-      array(
-        'method' => 'GET',
-        'action' => (string)$uri,
-      ),
-      $content);
+    return $this->renderStandardLoginButton($request, $mode, $attributes);
   }
+
   public function processLoginRequest(
     PhabricatorAuthLoginController $controller) {
 
diff --git a/src/applications/auth/provider/PhabricatorAuthProviderOAuth1.php b/src/applications/auth/provider/PhabricatorAuthProviderOAuth1.php
index c5931cacd0..96cc5d7c1e 100644
--- a/src/applications/auth/provider/PhabricatorAuthProviderOAuth1.php
+++ b/src/applications/auth/provider/PhabricatorAuthProviderOAuth1.php
@@ -39,54 +39,11 @@ abstract class PhabricatorAuthProviderOAuth1 extends PhabricatorAuthProvider {
   }
 
   protected function renderLoginForm(AphrontRequest $request, $mode) {
-    $viewer = $request->getUser();
-
-    if ($mode == 'link') {
-      $button_text = pht('Link External Account');
-    } else if ($mode == 'refresh') {
-      $button_text = pht('Refresh Account Link');
-    } else if ($this->shouldAllowRegistration()) {
-      $button_text = pht('Login or Register');
-    } else {
-      $button_text = pht('Login');
-    }
-
-    $icon = id(new PHUIIconView())
-      ->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
-      ->setSpriteIcon($this->getLoginIcon());
-
-    $button = id(new PHUIButtonView())
-        ->setSize(PHUIButtonView::BIG)
-        ->setColor(PHUIButtonView::GREY)
-        ->setIcon($icon)
-        ->setText($button_text)
-        ->setSubtext($this->getProviderName());
-
-    $adapter = $this->getAdapter();
-
-    $uri = new PhutilURI($this->getLoginURI());
-    $params = $uri->getQueryParams();
-    $uri->setQueryParams(array());
-
-    $content = array($button);
-
-    foreach ($params as $key => $value) {
-      $content[] = phutil_tag(
-        'input',
-        array(
-          'type' => 'hidden',
-          'name' => $key,
-          'value' => $value,
-        ));
-    }
-
-    return phabricator_form(
-      $viewer,
-      array(
-        'method' => 'POST',
-        'action' => (string)$uri,
-      ),
-      $content);
+    $attributes = array(
+      'method' => 'POST',
+      'uri' => $this->getLoginURI(),
+    );
+    return $this->renderStandardLoginButton($request, $mode, $attributes);
   }
 
   public function processLoginRequest(