Path: blob/master/src/applications/auth/provider/PhabricatorOAuthAuthProvider.php
12256 views
<?php12abstract class PhabricatorOAuthAuthProvider extends PhabricatorAuthProvider {34const PROPERTY_NOTE = 'oauth:app:note';56protected $adapter;78abstract protected function newOAuthAdapter();9abstract protected function getIDKey();10abstract protected function getSecretKey();1112public function getDescriptionForCreate() {13return pht('Configure %s OAuth.', $this->getProviderName());14}1516public function getAdapter() {17if (!$this->adapter) {18$adapter = $this->newOAuthAdapter();19$this->adapter = $adapter;20$this->configureAdapter($adapter);21}22return $this->adapter;23}2425public function isLoginFormAButton() {26return true;27}2829public function readFormValuesFromProvider() {30$config = $this->getProviderConfig();31$id = $config->getProperty($this->getIDKey());32$secret = $config->getProperty($this->getSecretKey());33$note = $config->getProperty(self::PROPERTY_NOTE);3435return array(36$this->getIDKey() => $id,37$this->getSecretKey() => $secret,38self::PROPERTY_NOTE => $note,39);40}4142public function readFormValuesFromRequest(AphrontRequest $request) {43return array(44$this->getIDKey() => $request->getStr($this->getIDKey()),45$this->getSecretKey() => $request->getStr($this->getSecretKey()),46self::PROPERTY_NOTE => $request->getStr(self::PROPERTY_NOTE),47);48}4950protected function processOAuthEditForm(51AphrontRequest $request,52array $values,53$id_error,54$secret_error) {5556$errors = array();57$issues = array();58$key_id = $this->getIDKey();59$key_secret = $this->getSecretKey();6061if (!strlen($values[$key_id])) {62$errors[] = $id_error;63$issues[$key_id] = pht('Required');64}6566if (!strlen($values[$key_secret])) {67$errors[] = $secret_error;68$issues[$key_secret] = pht('Required');69}7071// If the user has not changed the secret, don't update it (that is,72// don't cause a bunch of "****" to be written to the database).73if (preg_match('/^[*]+$/', $values[$key_secret])) {74unset($values[$key_secret]);75}7677return array($errors, $issues, $values);78}7980public function getConfigurationHelp() {81$help = $this->getProviderConfigurationHelp();8283return $help."\n\n".84pht(85'Use the **OAuth App Notes** field to record details about which '.86'account the external application is registered under.');87}8889abstract protected function getProviderConfigurationHelp();9091protected function extendOAuthEditForm(92AphrontRequest $request,93AphrontFormView $form,94array $values,95array $issues,96$id_label,97$secret_label) {9899$key_id = $this->getIDKey();100$key_secret = $this->getSecretKey();101$key_note = self::PROPERTY_NOTE;102103$v_id = $values[$key_id];104$v_secret = $values[$key_secret];105if ($v_secret) {106$v_secret = str_repeat('*', strlen($v_secret));107}108$v_note = $values[$key_note];109110$e_id = idx($issues, $key_id, $request->isFormPost() ? null : true);111$e_secret = idx($issues, $key_secret, $request->isFormPost() ? null : true);112113$form114->appendChild(115id(new AphrontFormTextControl())116->setLabel($id_label)117->setName($key_id)118->setValue($v_id)119->setError($e_id))120->appendChild(121id(new AphrontFormPasswordControl())122->setLabel($secret_label)123->setDisableAutocomplete(true)124->setName($key_secret)125->setValue($v_secret)126->setError($e_secret))127->appendChild(128id(new AphrontFormTextAreaControl())129->setLabel(pht('OAuth App Notes'))130->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)131->setName($key_note)132->setValue($v_note));133}134135public function renderConfigPropertyTransactionTitle(136PhabricatorAuthProviderConfigTransaction $xaction) {137138$author_phid = $xaction->getAuthorPHID();139$old = $xaction->getOldValue();140$new = $xaction->getNewValue();141$key = $xaction->getMetadataValue(142PhabricatorAuthProviderConfigTransaction::PROPERTY_KEY);143144switch ($key) {145case self::PROPERTY_NOTE:146if (strlen($old)) {147return pht(148'%s updated the OAuth application notes for this provider.',149$xaction->renderHandleLink($author_phid));150} else {151return pht(152'%s set the OAuth application notes for this provider.',153$xaction->renderHandleLink($author_phid));154}155156}157158return parent::renderConfigPropertyTransactionTitle($xaction);159}160161protected function willSaveAccount(PhabricatorExternalAccount $account) {162parent::willSaveAccount($account);163$this->synchronizeOAuthAccount($account);164}165166abstract protected function synchronizeOAuthAccount(167PhabricatorExternalAccount $account);168169}170171172