1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-21 04:50:55 +01:00

Auth - restore Phabricator OAuth as a provider

Summary: So meta it hurts. Fixes T887.

Test Plan: created a second instance of phabricator locally. made an account on oauth server phabricator. set up my normal dev phabricator to use this new oauth phabricator. noted the form worked. created an account via the oauth method and it worked.

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: Korvin, epriestley

Maniphest Tasks: T887

Differential Revision: https://secure.phabricator.com/D11287
This commit is contained in:
Bob Trahan 2015-01-08 16:28:04 -08:00
parent cecc08756e
commit 2be746fb1f
3 changed files with 210 additions and 1 deletions

View file

@ -2122,6 +2122,7 @@ phutil_register_library_map(array(
'PhabricatorPeopleUserPHIDType' => 'applications/people/phid/PhabricatorPeopleUserPHIDType.php', 'PhabricatorPeopleUserPHIDType' => 'applications/people/phid/PhabricatorPeopleUserPHIDType.php',
'PhabricatorPeopleWelcomeController' => 'applications/people/controller/PhabricatorPeopleWelcomeController.php', 'PhabricatorPeopleWelcomeController' => 'applications/people/controller/PhabricatorPeopleWelcomeController.php',
'PhabricatorPersonaAuthProvider' => 'applications/auth/provider/PhabricatorPersonaAuthProvider.php', 'PhabricatorPersonaAuthProvider' => 'applications/auth/provider/PhabricatorPersonaAuthProvider.php',
'PhabricatorPhabricatorAuthProvider' => 'applications/auth/provider/PhabricatorPhabricatorAuthProvider.php',
'PhabricatorPhameApplication' => 'applications/phame/application/PhabricatorPhameApplication.php', 'PhabricatorPhameApplication' => 'applications/phame/application/PhabricatorPhameApplication.php',
'PhabricatorPhameBlogPHIDType' => 'applications/phame/phid/PhabricatorPhameBlogPHIDType.php', 'PhabricatorPhameBlogPHIDType' => 'applications/phame/phid/PhabricatorPhameBlogPHIDType.php',
'PhabricatorPhameConfigOptions' => 'applications/phame/config/PhabricatorPhameConfigOptions.php', 'PhabricatorPhameConfigOptions' => 'applications/phame/config/PhabricatorPhameConfigOptions.php',
@ -5321,6 +5322,7 @@ phutil_register_library_map(array(
'PhabricatorPeopleUserPHIDType' => 'PhabricatorPHIDType', 'PhabricatorPeopleUserPHIDType' => 'PhabricatorPHIDType',
'PhabricatorPeopleWelcomeController' => 'PhabricatorPeopleController', 'PhabricatorPeopleWelcomeController' => 'PhabricatorPeopleController',
'PhabricatorPersonaAuthProvider' => 'PhabricatorAuthProvider', 'PhabricatorPersonaAuthProvider' => 'PhabricatorAuthProvider',
'PhabricatorPhabricatorAuthProvider' => 'PhabricatorOAuth2AuthProvider',
'PhabricatorPhameApplication' => 'PhabricatorApplication', 'PhabricatorPhameApplication' => 'PhabricatorApplication',
'PhabricatorPhameBlogPHIDType' => 'PhabricatorPHIDType', 'PhabricatorPhameBlogPHIDType' => 'PhabricatorPHIDType',
'PhabricatorPhameConfigOptions' => 'PhabricatorApplicationConfigOptions', 'PhabricatorPhameConfigOptions' => 'PhabricatorApplicationConfigOptions',

View file

@ -159,7 +159,11 @@ final class PhabricatorAuthEditController
} }
if ($is_new) { if ($is_new) {
if ($provider->hasSetupStep()) {
$button = pht('Next Step');
} else {
$button = pht('Add Provider'); $button = pht('Add Provider');
}
$crumb = pht('Add Provider'); $crumb = pht('Add Provider');
$title = pht('Add Authentication Provider'); $title = pht('Add Authentication Provider');
$cancel_uri = $this->getApplicationURI('/config/new/'); $cancel_uri = $this->getApplicationURI('/config/new/');

View file

@ -0,0 +1,203 @@
<?php
final class PhabricatorPhabricatorAuthProvider
extends PhabricatorOAuth2AuthProvider {
const PROPERTY_PHABRICATOR_NAME = 'oauth2:phabricator:name';
const PROPERTY_PHABRICATOR_URI = 'oauth2:phabricator:uri';
public function getProviderName() {
return pht('Phabricator');
}
public function getConfigurationHelp() {
if ($this->isCreate()) {
return pht(
'**Step 1 of 2** - Name Phabricator OAuth Instance'.
'Choose a permanent name for the OAuth server instance of '.
'Phabricator. //This// instance of Phabricator uses this name '.
'internally to keep track of the OAuth server instance of '.
'Phabricator, in case the URL changes later.');
}
return parent::getConfigurationHelp();
}
protected function getProviderConfigurationHelp() {
$config = $this->getProviderConfig();
$base_uri = rtrim(
$config->getProperty(self::PROPERTY_PHABRICATOR_URI), '/');
$login_uri = PhabricatorEnv::getURI($this->getLoginURI());
return pht(
"**Step 2 of 2 - Configure Phabricator OAuth Instance**".
"To configure Phabricator OAuth, create a new application here:".
"\n\n".
"%s/oauthserver/client/create/".
"\n\n".
"When creating your application, use these settings:".
"\n\n".
" - **Redirect URI:** Set this to: `%s`".
"\n\n".
"After completing configuration, copy the **Client ID** and ".
"**Client Secret** to the fields above. (You may need to generate the ".
"client secret by clicking 'New Secret' first.)",
$base_uri,
$login_uri);
}
protected function newOAuthAdapter() {
$config = $this->getProviderConfig();
return id(new PhutilPhabricatorAuthAdapter())
->setAdapterDomain($config->getProviderDomain())
->setPhabricatorBaseURI(
$config->getProperty(self::PROPERTY_PHABRICATOR_URI));
}
protected function getLoginIcon() {
return 'Phabricator';
}
private function isCreate() {
return !$this->getProviderConfig()->getID();
}
public function readFormValuesFromProvider() {
$config = $this->getProviderConfig();
$uri = $config->getProperty(self::PROPERTY_PHABRICATOR_URI);
return parent::readFormValuesFromProvider() + array(
self::PROPERTY_PHABRICATOR_NAME => $this->getProviderDomain(),
self::PROPERTY_PHABRICATOR_URI => $uri,
);
}
public function readFormValuesFromRequest(AphrontRequest $request) {
$is_setup = $this->isCreate();
if ($is_setup) {
$parent_values = array();
$name = $request->getStr(self::PROPERTY_PHABRICATOR_NAME);
} else {
$parent_values = parent::readFormValuesFromRequest($request);
$name = $this->getProviderDomain();
}
return $parent_values + array(
self::PROPERTY_PHABRICATOR_NAME => $name,
self::PROPERTY_PHABRICATOR_URI =>
$request->getStr(self::PROPERTY_PHABRICATOR_URI),
);
}
public function processEditForm(
AphrontRequest $request,
array $values) {
$is_setup = $this->isCreate();
if (!$is_setup) {
list($errors, $issues, $values) =
parent::processEditForm($request, $values);
} else {
$errors = array();
$issues = array();
}
$key_name = self::PROPERTY_PHABRICATOR_NAME;
$key_uri = self::PROPERTY_PHABRICATOR_URI;
if (!strlen($values[$key_name])) {
$errors[] = pht('Phabricator instance name is required.');
$issues[$key_name] = pht('Required');
} else if (!preg_match('/^[a-z0-9.]+\z/', $values[$key_name])) {
$errors[] = pht(
'Phabricator instance name must contain only lowercase letters, '.
'digits, and periods.');
$issues[$key_name] = pht('Invalid');
}
if (!strlen($values[$key_uri])) {
$errors[] = pht('Phabricator base URI is required.');
$issues[$key_uri] = pht('Required');
} else {
$uri = new PhutilURI($values[$key_uri]);
if (!$uri->getProtocol()) {
$errors[] = pht(
'Phabricator base URI should include protocol (like "https://").');
$issues[$key_uri] = pht('Invalid');
}
}
if (!$errors && $is_setup) {
$config = $this->getProviderConfig();
$config->setProviderDomain($values[$key_name]);
}
return array($errors, $issues, $values);
}
public function extendEditForm(
AphrontRequest $request,
AphrontFormView $form,
array $values,
array $issues) {
$is_setup = $this->isCreate();
$e_required = $request->isFormPost() ? null : true;
$v_name = $values[self::PROPERTY_PHABRICATOR_NAME];
if ($is_setup) {
$e_name = idx($issues, self::PROPERTY_PHABRICATOR_NAME, $e_required);
} else {
$e_name = null;
}
$v_uri = $values[self::PROPERTY_PHABRICATOR_URI];
$e_uri = idx($issues, self::PROPERTY_PHABRICATOR_URI, $e_required);
if ($is_setup) {
$form
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Phabricator Instance Name'))
->setValue($v_name)
->setName(self::PROPERTY_PHABRICATOR_NAME)
->setError($e_name)
->setCaption(pht(
'Use lowercase letters, digits, and periods. For example: %s',
phutil_tag(
'tt',
array(),
'`phabricator.oauthserver`'))));
} else {
$form
->appendChild(
id(new AphrontFormStaticControl())
->setLabel(pht('Phabricator Instance Name'))
->setValue($v_name));
}
$form
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Phabricator Base URI'))
->setValue($v_uri)
->setName(self::PROPERTY_PHABRICATOR_URI)
->setCaption(
pht(
'The URI where the OAuth server instance of Phabricator is '.
'installed. For example: %s',
phutil_tag('tt', array(), 'https://phabricator.mycompany.com/')))
->setError($e_uri));
if (!$is_setup) {
parent::extendEditForm($request, $form, $values, $issues);
}
}
public function hasSetupStep() {
return true;
}
}