Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / auth / provider / PhabricatorOAuthAuthProvider.php
blobdf76c655f39e68acbfd358f9f04e710c04068184
1 <?php
3 abstract class PhabricatorOAuthAuthProvider extends PhabricatorAuthProvider {
5 const PROPERTY_NOTE = 'oauth:app:note';
7 protected $adapter;
9 abstract protected function newOAuthAdapter();
10 abstract protected function getIDKey();
11 abstract protected function getSecretKey();
13 public function getDescriptionForCreate() {
14 return pht('Configure %s OAuth.', $this->getProviderName());
17 public function getAdapter() {
18 if (!$this->adapter) {
19 $adapter = $this->newOAuthAdapter();
20 $this->adapter = $adapter;
21 $this->configureAdapter($adapter);
23 return $this->adapter;
26 public function isLoginFormAButton() {
27 return true;
30 public function readFormValuesFromProvider() {
31 $config = $this->getProviderConfig();
32 $id = $config->getProperty($this->getIDKey());
33 $secret = $config->getProperty($this->getSecretKey());
34 $note = $config->getProperty(self::PROPERTY_NOTE);
36 return array(
37 $this->getIDKey() => $id,
38 $this->getSecretKey() => $secret,
39 self::PROPERTY_NOTE => $note,
43 public function readFormValuesFromRequest(AphrontRequest $request) {
44 return array(
45 $this->getIDKey() => $request->getStr($this->getIDKey()),
46 $this->getSecretKey() => $request->getStr($this->getSecretKey()),
47 self::PROPERTY_NOTE => $request->getStr(self::PROPERTY_NOTE),
51 protected function processOAuthEditForm(
52 AphrontRequest $request,
53 array $values,
54 $id_error,
55 $secret_error) {
57 $errors = array();
58 $issues = array();
59 $key_id = $this->getIDKey();
60 $key_secret = $this->getSecretKey();
62 if (!strlen($values[$key_id])) {
63 $errors[] = $id_error;
64 $issues[$key_id] = pht('Required');
67 if (!strlen($values[$key_secret])) {
68 $errors[] = $secret_error;
69 $issues[$key_secret] = pht('Required');
72 // If the user has not changed the secret, don't update it (that is,
73 // don't cause a bunch of "****" to be written to the database).
74 if (preg_match('/^[*]+$/', $values[$key_secret])) {
75 unset($values[$key_secret]);
78 return array($errors, $issues, $values);
81 public function getConfigurationHelp() {
82 $help = $this->getProviderConfigurationHelp();
84 return $help."\n\n".
85 pht(
86 'Use the **OAuth App Notes** field to record details about which '.
87 'account the external application is registered under.');
90 abstract protected function getProviderConfigurationHelp();
92 protected function extendOAuthEditForm(
93 AphrontRequest $request,
94 AphrontFormView $form,
95 array $values,
96 array $issues,
97 $id_label,
98 $secret_label) {
100 $key_id = $this->getIDKey();
101 $key_secret = $this->getSecretKey();
102 $key_note = self::PROPERTY_NOTE;
104 $v_id = $values[$key_id];
105 $v_secret = $values[$key_secret];
106 if ($v_secret) {
107 $v_secret = str_repeat('*', strlen($v_secret));
109 $v_note = $values[$key_note];
111 $e_id = idx($issues, $key_id, $request->isFormPost() ? null : true);
112 $e_secret = idx($issues, $key_secret, $request->isFormPost() ? null : true);
114 $form
115 ->appendChild(
116 id(new AphrontFormTextControl())
117 ->setLabel($id_label)
118 ->setName($key_id)
119 ->setValue($v_id)
120 ->setError($e_id))
121 ->appendChild(
122 id(new AphrontFormPasswordControl())
123 ->setLabel($secret_label)
124 ->setDisableAutocomplete(true)
125 ->setName($key_secret)
126 ->setValue($v_secret)
127 ->setError($e_secret))
128 ->appendChild(
129 id(new AphrontFormTextAreaControl())
130 ->setLabel(pht('OAuth App Notes'))
131 ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)
132 ->setName($key_note)
133 ->setValue($v_note));
136 public function renderConfigPropertyTransactionTitle(
137 PhabricatorAuthProviderConfigTransaction $xaction) {
139 $author_phid = $xaction->getAuthorPHID();
140 $old = $xaction->getOldValue();
141 $new = $xaction->getNewValue();
142 $key = $xaction->getMetadataValue(
143 PhabricatorAuthProviderConfigTransaction::PROPERTY_KEY);
145 switch ($key) {
146 case self::PROPERTY_NOTE:
147 if (strlen($old)) {
148 return pht(
149 '%s updated the OAuth application notes for this provider.',
150 $xaction->renderHandleLink($author_phid));
151 } else {
152 return pht(
153 '%s set the OAuth application notes for this provider.',
154 $xaction->renderHandleLink($author_phid));
159 return parent::renderConfigPropertyTransactionTitle($xaction);
162 protected function willSaveAccount(PhabricatorExternalAccount $account) {
163 parent::willSaveAccount($account);
164 $this->synchronizeOAuthAccount($account);
167 abstract protected function synchronizeOAuthAccount(
168 PhabricatorExternalAccount $account);