Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/auth/provider/PhabricatorOAuthAuthProvider.php
12256 views
1
<?php
2
3
abstract class PhabricatorOAuthAuthProvider extends PhabricatorAuthProvider {
4
5
const PROPERTY_NOTE = 'oauth:app:note';
6
7
protected $adapter;
8
9
abstract protected function newOAuthAdapter();
10
abstract protected function getIDKey();
11
abstract protected function getSecretKey();
12
13
public function getDescriptionForCreate() {
14
return pht('Configure %s OAuth.', $this->getProviderName());
15
}
16
17
public function getAdapter() {
18
if (!$this->adapter) {
19
$adapter = $this->newOAuthAdapter();
20
$this->adapter = $adapter;
21
$this->configureAdapter($adapter);
22
}
23
return $this->adapter;
24
}
25
26
public function isLoginFormAButton() {
27
return true;
28
}
29
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);
35
36
return array(
37
$this->getIDKey() => $id,
38
$this->getSecretKey() => $secret,
39
self::PROPERTY_NOTE => $note,
40
);
41
}
42
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),
48
);
49
}
50
51
protected function processOAuthEditForm(
52
AphrontRequest $request,
53
array $values,
54
$id_error,
55
$secret_error) {
56
57
$errors = array();
58
$issues = array();
59
$key_id = $this->getIDKey();
60
$key_secret = $this->getSecretKey();
61
62
if (!strlen($values[$key_id])) {
63
$errors[] = $id_error;
64
$issues[$key_id] = pht('Required');
65
}
66
67
if (!strlen($values[$key_secret])) {
68
$errors[] = $secret_error;
69
$issues[$key_secret] = pht('Required');
70
}
71
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]);
76
}
77
78
return array($errors, $issues, $values);
79
}
80
81
public function getConfigurationHelp() {
82
$help = $this->getProviderConfigurationHelp();
83
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.');
88
}
89
90
abstract protected function getProviderConfigurationHelp();
91
92
protected function extendOAuthEditForm(
93
AphrontRequest $request,
94
AphrontFormView $form,
95
array $values,
96
array $issues,
97
$id_label,
98
$secret_label) {
99
100
$key_id = $this->getIDKey();
101
$key_secret = $this->getSecretKey();
102
$key_note = self::PROPERTY_NOTE;
103
104
$v_id = $values[$key_id];
105
$v_secret = $values[$key_secret];
106
if ($v_secret) {
107
$v_secret = str_repeat('*', strlen($v_secret));
108
}
109
$v_note = $values[$key_note];
110
111
$e_id = idx($issues, $key_id, $request->isFormPost() ? null : true);
112
$e_secret = idx($issues, $key_secret, $request->isFormPost() ? null : true);
113
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));
134
}
135
136
public function renderConfigPropertyTransactionTitle(
137
PhabricatorAuthProviderConfigTransaction $xaction) {
138
139
$author_phid = $xaction->getAuthorPHID();
140
$old = $xaction->getOldValue();
141
$new = $xaction->getNewValue();
142
$key = $xaction->getMetadataValue(
143
PhabricatorAuthProviderConfigTransaction::PROPERTY_KEY);
144
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));
155
}
156
157
}
158
159
return parent::renderConfigPropertyTransactionTitle($xaction);
160
}
161
162
protected function willSaveAccount(PhabricatorExternalAccount $account) {
163
parent::willSaveAccount($account);
164
$this->synchronizeOAuthAccount($account);
165
}
166
167
abstract protected function synchronizeOAuthAccount(
168
PhabricatorExternalAccount $account);
169
170
}
171
172