-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPhabricatorMediaWikiAuthProvider.php
303 lines (253 loc) · 8.86 KB
/
PhabricatorMediaWikiAuthProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
// Source: https://raw.githubusercontent.com/wikimedia/phabricator-extensions/wmf/stable/src/oauth/PhabricatorMediaWikiAuthProvider.php
final class PhabricatorMediaWikiAuthProvider extends PhabricatorOAuth1AuthProvider {
public const PROPERTY_MEDIAWIKI_NAME = 'oauth1:mediawiki:name';
public const PROPERTY_MEDIAWIKI_URI = 'oauth1:mediawiki:uri';
public const PROPERTY_PRIVATE_KEY = 'oauth1:mediawiki:key:private';
public const PROPERTY_PUBLIC_KEY = 'oauth1:mediawiki:key:public';
public function getProviderConfig() {
$config = parent::getProviderConfig();
$config->setProviderType( 'mediawiki' );
return $config;
}
public function readFormValuesFromProvider() {
$config = $this->getProviderConfig();
return [
self::PROPERTY_MEDIAWIKI_NAME =>
$this->getProviderDomain(),
self::PROPERTY_MEDIAWIKI_URI =>
$config->getProperty( self::PROPERTY_MEDIAWIKI_URI ),
self::PROPERTY_CONSUMER_KEY =>
$config->getProperty( self::PROPERTY_CONSUMER_KEY ),
self::PROPERTY_CONSUMER_SECRET =>
$config->getProperty( self::PROPERTY_CONSUMER_SECRET ),
];
}
public function readFormValuesFromRequest( AphrontRequest $request ) {
$is_setup = $this->isSetup();
if ( $is_setup ) {
$name = $request->getStr( self::PROPERTY_MEDIAWIKI_NAME );
} else {
$name = $this->getProviderDomain();
}
return [
self::PROPERTY_MEDIAWIKI_NAME => $name,
self::PROPERTY_MEDIAWIKI_URI =>
$request->getStr( self::PROPERTY_MEDIAWIKI_URI ),
self::PROPERTY_CONSUMER_KEY =>
$request->getStr( self::PROPERTY_CONSUMER_KEY ),
self::PROPERTY_CONSUMER_SECRET =>
$request->getStr( self::PROPERTY_CONSUMER_SECRET ),
];
}
public function getProviderName() {
return pht( 'MediaWiki' );
}
public function getWikiURI() {
$config = $this->getProviderConfig();
$uri = $config->getProperty( self::PROPERTY_MEDIAWIKI_URI );
$uri = new PhutilURI( $uri );
$normalized = $uri->getProtocol() . '://' . $uri->getDomain();
if ( $uri->getPort() != 80 && $uri->getPort() != 443 ) {
$normalized .= ':' . $uri->getPort();
}
if ( strlen( ( $uri->getPath() ) ) > 0 && $uri->getPath() !== '/' ) {
$normalized .= $uri->getPath();
}
if ( substr( $normalized, -1 ) == '/' ) {
$normalized = substr( $normalized, 0, -1 );
}
return $normalized;
}
protected function getProviderConfigurationHelp() {
$login_uri = PhabricatorEnv::getURI( $this->getLoginURI() );
if ( $this->isSetup() ) {
return pht(
"**Step 1 of 2**: Provide the name and URI for your MediaWiki install.\n\n" .
"In the next step, you will create an auth consumer in MediaWiki to be used by Phorge oauth." );
} else {
$wiki_uri = $this->getWikiURI();
return pht(
"**Step 2 of 2**: Create a MediaWiki auth consumer for this Phorge instance." .
"\n\n" .
"NOTE: Propose a consumer with the form at this url: %s" .
"\n\n" .
"Provide the following settings on the consumer registration:\n\n" .
" - **Callback URL:** Set this to: `%s`\n" .
" - **Grants:** `Basic Rights` is all that is needed for authentication.\n" .
"\n\n" .
"After you register the consumer, a **Consumer Key** and " .
"**Consumer Secret** will be provided to you by MediaWiki. " .
"To complete configuration of Phorge, copy the provided keys into " .
"the corresponding fields above." .
"\n\n" .
"NOTE: Before Phorge can successfully authenticate to your MediaWiki," .
" a wiki admin must approve the oauth consumer registration using the form" .
" which can be found at the following url: %s",
$wiki_uri . '/index.php?title=Special:OAuthConsumerRegistration/propose',
$login_uri,
$wiki_uri . '/index.php?title=Special:OAuthManageConsumers/proposed'
);
}
}
protected function newOAuthAdapter() {
$config = $this->getProviderConfig();
return id( new PhutilMediaWikiAuthAdapter() )
->setAdapterDomain( $config->getProviderDomain() )
->setMediaWikiBaseURI( $this->getWikiURI() );
}
protected function getLoginIcon() {
return 'MediaWiki';
}
private function isSetup() {
return !$this->getProviderConfig()->getID();
}
public function hasSetupStep() {
return true;
}
public function processEditForm(
AphrontRequest $request,
array $values
) {
$errors = [];
$issues = [];
$is_setup = $this->isSetup();
$key_name = self::PROPERTY_MEDIAWIKI_NAME;
$key_uri = self::PROPERTY_MEDIAWIKI_URI;
$key_secret = self::PROPERTY_CONSUMER_SECRET;
$key_consumer = self::PROPERTY_CONSUMER_KEY;
if ( !strlen( $values[$key_uri] ) ) {
$errors[] = pht( 'MediaWiki base URI is required.' );
$issues[$key_uri] = pht( 'Required' );
} else {
$uri = new PhutilURI( $values[$key_uri] );
if ( !$uri->getProtocol() ) {
$errors[] = pht(
'MediaWiki base URI should include protocol ' . '(like "https://").'
);
$issues[$key_uri] = pht( 'Invalid' );
}
}
if ( !$is_setup && !strlen( $values[$key_secret] ) ) {
$errors[] = pht( 'Consumer Secret is required' );
$issues[$key_secret] = pht( 'Required' );
}
if ( !$is_setup && !strlen( $values[$key_consumer] ) ) {
$errors[] = pht( 'Consumer Key is required' );
$issues[$key_consumer] = pht( 'Required' );
}
if ( !count( $errors ) ) {
$config = $this->getProviderConfig();
$config->setProviderDomain( $values[$key_name] );
$config->setProperty( $key_name, $values[$key_name] );
if ( $is_setup ) {
$config->setProperty( $key_uri, $values[$key_uri] );
} else {
$config->setProperty( $key_uri, $values[$key_uri] );
$config->setProperty( $key_secret, $values[$key_secret] );
$config->setProperty( $key_consumer, $values[$key_consumer] );
}
$config->save();
}
return [ $errors, $issues, $values ];
}
public function extendEditForm(
AphrontRequest $request,
AphrontFormView $form,
array $values,
array $issues
) {
$is_setup = $this->isSetup();
$e_required = $request->isFormPost() ? null : true;
$v_name = $values[self::PROPERTY_MEDIAWIKI_NAME];
if ( $is_setup ) {
$e_name = idx( $issues, self::PROPERTY_MEDIAWIKI_NAME, $e_required );
} else {
$e_name = null;
}
$v_uri = $values[self::PROPERTY_MEDIAWIKI_URI];
$e_uri = idx( $issues, self::PROPERTY_MEDIAWIKI_URI, $e_required );
$config = $this->getProviderConfig();
if ( $is_setup ) {
$form->appendRemarkupInstructions(
pht(
"**MediaWiki Instance Name**\n\n" .
"Choose a permanent name for this instance of MediaWiki." .
"Phorge uses this name internally to keep track of " .
"this instance of MediaWiki, in case the URL changes later." .
"\n\n" .
"Use lowercase letters, digits, and period. For example: " .
"\n\n`mediawiki`, `mediawiki.mycompany` " .
"or `mediawiki.engineering` are reasonable names."
)
)
->appendChild(
id( new AphrontFormTextControl() )
->setLabel( pht( 'MediaWiki Instance Name' ) )
->setValue( $v_name )
->setName( self::PROPERTY_MEDIAWIKI_NAME )
->setError( $e_name )
);
} else {
$form->appendChild(
id( new AphrontFormTextControl() )
->setLabel( pht( 'MediaWiki Instance Name' ) )
->setValue( $v_name )
->setName( self::PROPERTY_MEDIAWIKI_NAME )
->setDisabled( true )
->setError( $e_name )
);
}
$form->appendChild(
id( new AphrontFormTextControl() )
->setLabel( pht( 'MediaWiki Base URI' ) )
->setValue( $v_uri )
->setName( self::PROPERTY_MEDIAWIKI_URI )
->setPlaceholder( 'https://www.mediawiki.org/w' )
->setCaption( pht( 'The full URL to your MediaWiki install, up to but not including "index.php"' ) )
->setError( $e_uri )
);
if ( !$is_setup ) {
if ( !strlen( $config->getProperty( self::PROPERTY_CONSUMER_KEY ) ) ) {
$form->appendRemarkupInstructions(
pht(
'NOTE: Copy the keys generated by the MediaWiki OAuth' .
' consumer registration and paste them here.'
)
);
}
$form->appendChild(
id( new AphrontFormTextControl() )
->setLabel( pht( 'Consumer Key' ) )
->setName( self::PROPERTY_CONSUMER_KEY )
->setValue( $values[self::PROPERTY_CONSUMER_KEY] )
)
->appendChild(
id( new AphrontFormTextControl() )
->setLabel( pht( 'Secret Key' ) )
->setName( self::PROPERTY_CONSUMER_SECRET )
->setValue( $values[self::PROPERTY_CONSUMER_SECRET] )
);
}
}
public static function getMediaWikiProvider() {
$providers = self::getAllEnabledProviders();
foreach ( $providers as $provider ) {
if ( $provider instanceof PhabricatorMediaWikiAuthProvider ) {
return $provider;
}
}
return null;
}
protected function getContentSecurityPolicyFormActions() {
$csp_actions = $this->getAdapter()->getContentSecurityPolicyFormActions();
$uri = new phutilURI( $csp_actions[0] );
$mobile_uri = new phutilURI( $uri );
$domain = preg_replace( '/^www\./', 'm.', $uri->getDomain() );
$mobile_uri->setDomain( $domain );
if ( (string)$uri != (string)$mobile_uri ) {
$csp_actions[] = (string)$mobile_uri;
}
return $csp_actions;
}
}