Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/auth/provider/PhabricatorPhabricatorAuthProvider.php
12256 views
1
<?php
2
3
final class PhabricatorPhabricatorAuthProvider
4
extends PhabricatorOAuth2AuthProvider {
5
6
const PROPERTY_PHABRICATOR_NAME = 'oauth2:phabricator:name';
7
const PROPERTY_PHABRICATOR_URI = 'oauth2:phabricator:uri';
8
9
public function getProviderName() {
10
return PlatformSymbols::getPlatformServerName();
11
}
12
13
public function getConfigurationHelp() {
14
if ($this->isCreate()) {
15
return pht(
16
"**Step 1 of 2 - Name Remote Server**\n\n".
17
'Choose a permanent name for the remote server you want to connect '.
18
'to. This name is used internally to keep track of the remote '.
19
'server, in case the URL changes later.');
20
}
21
22
return parent::getConfigurationHelp();
23
}
24
protected function getProviderConfigurationHelp() {
25
$config = $this->getProviderConfig();
26
$base_uri = rtrim(
27
$config->getProperty(self::PROPERTY_PHABRICATOR_URI), '/');
28
$login_uri = PhabricatorEnv::getURI($this->getLoginURI());
29
30
return pht(
31
"**Step 2 of 2 - Configure OAuth Server**\n\n".
32
"To configure OAuth, create a new application here:".
33
"\n\n".
34
"%s/oauthserver/client/create/".
35
"\n\n".
36
"When creating your application, use these settings:".
37
"\n\n".
38
" - **Redirect URI:** Set this to: `%s`".
39
"\n\n".
40
"After completing configuration, copy the **Client ID** and ".
41
"**Client Secret** to the fields above. (You may need to generate the ".
42
"client secret by clicking 'New Secret' first.)",
43
$base_uri,
44
$login_uri);
45
}
46
47
protected function newOAuthAdapter() {
48
$config = $this->getProviderConfig();
49
return id(new PhutilPhabricatorAuthAdapter())
50
->setAdapterDomain($config->getProviderDomain())
51
->setPhabricatorBaseURI(
52
$config->getProperty(self::PROPERTY_PHABRICATOR_URI));
53
}
54
55
protected function getLoginIcon() {
56
return PlatformSymbols::getPlatformServerName();
57
}
58
59
private function isCreate() {
60
return !$this->getProviderConfig()->getID();
61
}
62
63
public function readFormValuesFromProvider() {
64
$config = $this->getProviderConfig();
65
$uri = $config->getProperty(self::PROPERTY_PHABRICATOR_URI);
66
67
return parent::readFormValuesFromProvider() + array(
68
self::PROPERTY_PHABRICATOR_NAME => $this->getProviderDomain(),
69
self::PROPERTY_PHABRICATOR_URI => $uri,
70
);
71
}
72
73
public function readFormValuesFromRequest(AphrontRequest $request) {
74
$is_setup = $this->isCreate();
75
if ($is_setup) {
76
$parent_values = array();
77
$name = $request->getStr(self::PROPERTY_PHABRICATOR_NAME);
78
} else {
79
$parent_values = parent::readFormValuesFromRequest($request);
80
$name = $this->getProviderDomain();
81
}
82
83
return $parent_values + array(
84
self::PROPERTY_PHABRICATOR_NAME => $name,
85
self::PROPERTY_PHABRICATOR_URI =>
86
$request->getStr(self::PROPERTY_PHABRICATOR_URI),
87
);
88
}
89
90
public function processEditForm(
91
AphrontRequest $request,
92
array $values) {
93
94
$is_setup = $this->isCreate();
95
96
if (!$is_setup) {
97
list($errors, $issues, $values) =
98
parent::processEditForm($request, $values);
99
} else {
100
$errors = array();
101
$issues = array();
102
}
103
104
$key_name = self::PROPERTY_PHABRICATOR_NAME;
105
$key_uri = self::PROPERTY_PHABRICATOR_URI;
106
107
if (!strlen($values[$key_name])) {
108
$errors[] = pht('Server name is required.');
109
$issues[$key_name] = pht('Required');
110
} else if (!preg_match('/^[a-z0-9.]+\z/', $values[$key_name])) {
111
$errors[] = pht(
112
'Server name must contain only lowercase letters, '.
113
'digits, and periods.');
114
$issues[$key_name] = pht('Invalid');
115
}
116
117
if (!strlen($values[$key_uri])) {
118
$errors[] = pht('Base URI is required.');
119
$issues[$key_uri] = pht('Required');
120
} else {
121
$uri = new PhutilURI($values[$key_uri]);
122
if (!$uri->getProtocol()) {
123
$errors[] = pht(
124
'Base URI should include protocol (like "%s").',
125
'https://');
126
$issues[$key_uri] = pht('Invalid');
127
}
128
}
129
130
if (!$errors && $is_setup) {
131
$config = $this->getProviderConfig();
132
133
$config->setProviderDomain($values[$key_name]);
134
}
135
136
return array($errors, $issues, $values);
137
}
138
139
public function extendEditForm(
140
AphrontRequest $request,
141
AphrontFormView $form,
142
array $values,
143
array $issues) {
144
145
$is_setup = $this->isCreate();
146
147
$e_required = $request->isFormPost() ? null : true;
148
149
$v_name = $values[self::PROPERTY_PHABRICATOR_NAME];
150
if ($is_setup) {
151
$e_name = idx($issues, self::PROPERTY_PHABRICATOR_NAME, $e_required);
152
} else {
153
$e_name = null;
154
}
155
156
$v_uri = $values[self::PROPERTY_PHABRICATOR_URI];
157
$e_uri = idx($issues, self::PROPERTY_PHABRICATOR_URI, $e_required);
158
159
if ($is_setup) {
160
$form
161
->appendChild(
162
id(new AphrontFormTextControl())
163
->setLabel(pht('Server Name'))
164
->setValue($v_name)
165
->setName(self::PROPERTY_PHABRICATOR_NAME)
166
->setError($e_name)
167
->setCaption(pht(
168
'Use lowercase letters, digits, and periods. For example: %s',
169
phutil_tag(
170
'tt',
171
array(),
172
'`example.oauthserver`'))));
173
} else {
174
$form
175
->appendChild(
176
id(new AphrontFormStaticControl())
177
->setLabel(pht('Server Name'))
178
->setValue($v_name));
179
}
180
181
$form
182
->appendChild(
183
id(new AphrontFormTextControl())
184
->setLabel(pht('Base URI'))
185
->setValue($v_uri)
186
->setName(self::PROPERTY_PHABRICATOR_URI)
187
->setCaption(
188
pht(
189
'The URI where the OAuth server is installed. For example: %s',
190
phutil_tag('tt', array(), 'https://devtools.example.com/')))
191
->setError($e_uri));
192
193
if (!$is_setup) {
194
parent::extendEditForm($request, $form, $values, $issues);
195
}
196
}
197
198
public function hasSetupStep() {
199
return true;
200
}
201
202
public function getPhabricatorURI() {
203
$config = $this->getProviderConfig();
204
return $config->getProperty(self::PROPERTY_PHABRICATOR_URI);
205
}
206
207
}
208
209