Skip to content

Commit 1dd6a8a

Browse files
authored
Bump mediawiki/oauthclient to 2.0.* and set user agent in requests (x-tools#572)
The WMF cluster now requires a user agent, see https://w.wiki/4wJS This gets set for all Guzzle requests, which is what we should use instead of file_get_contents() etc. Later when we upgrade Symfony, we'll move to Symfony's Guzzle-compatible HTTP client. The host in the Guzzle user agent is hard-coded in YAML configuration for now. Bug: T400929
1 parent 6d60f74 commit 1dd6a8a

File tree

8 files changed

+53
-33
lines changed

8 files changed

+53
-33
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"eightpoints/guzzle-bundle": "^8.0",
3636
"jms/serializer-bundle": "^3.4",
3737
"krinkle/intuition": "^2.3",
38-
"mediawiki/oauthclient": "^1.2",
38+
"mediawiki/oauthclient": "2.0.*",
3939
"nelmio/api-doc-bundle": "~4.11",
4040
"nelmio/cors-bundle": "^2.3",
4141
"phpdocumentor/reflection-docblock": "^5.3",

composer.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/packages/eight_points_guzzle.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ eight_points_guzzle:
66
options:
77
headers:
88
Accept: "application/json"
9+
User-Agent: 'XTools/%env(APP_VERSION)% (https://xtools.wmcloud.org %env(MAILER_TO_EMAIL)%) GuzzleHttp'
910
timeout: 30

config/services.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ parameters:
4242
enable.TopEdits: '%env(bool:APP_ENABLE_TOPEDITS)%'
4343
oauth_key: '%env(OAUTH_KEY)%'
4444
oauth_secret: '%env(OAUTH_SECRET)%'
45+
mailer.to_email: '%env(MAILER_TO_EMAIL)%'
4546

4647
services:
4748
_defaults:
@@ -107,7 +108,7 @@ services:
107108
# These need to not be public, but they are, for now...
108109
app.automated_edits_helper:
109110
class: App\Helper\AutomatedEditsHelper
110-
arguments: ['@request_stack', '@cache.app']
111+
arguments: ['@request_stack', '@cache.app', 'eight_points_guzzle.client.xtools']
111112
public: true
112113
app.i18n_helper:
113114
class: App\Helper\I18nHelper

src/Controller/DefaultController.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpFoundation\RequestStack;
1919
use Symfony\Component\HttpFoundation\Response;
2020
use Symfony\Component\Routing\Annotation\Route;
21+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2122

2223
/**
2324
* The DefaultController handles the homepage, about pages, and user authentication.
@@ -64,10 +65,12 @@ public function loginAction(
6465
Request $request,
6566
RequestStack $requestStack,
6667
ProjectRepository $projectRepo,
68+
UrlGeneratorInterface $urlGenerator,
6769
string $centralAuthProject
6870
): RedirectResponse {
6971
try {
70-
[ $next, $token ] = $this->getOauthClient($request, $projectRepo, $centralAuthProject)->initiate();
72+
[$next, $token] = $this->getOauthClient($request, $projectRepo, $urlGenerator, $centralAuthProject)
73+
->initiate();
7174
} catch (Exception $oauthException) {
7275
$this->addFlashMessage('notice', 'error-login');
7376
return $this->redirectToRoute('homepage');
@@ -84,12 +87,14 @@ public function loginAction(
8487
* @Route("/oauthredirector.php", name="old_oauth_callback")
8588
* @param RequestStack $requestStack
8689
* @param ProjectRepository $projectRepo
90+
* @param UrlGeneratorInterface $urlGenerator
8791
* @param string $centralAuthProject
8892
* @return RedirectResponse
8993
*/
9094
public function oauthCallbackAction(
9195
RequestStack $requestStack,
9296
ProjectRepository $projectRepo,
97+
UrlGeneratorInterface $urlGenerator,
9398
string $centralAuthProject
9499
): RedirectResponse {
95100
$request = $requestStack->getCurrentRequest();
@@ -100,7 +105,7 @@ public function oauthCallbackAction(
100105
}
101106

102107
// Complete authentication.
103-
$client = $this->getOauthClient($request, $projectRepo, $centralAuthProject);
108+
$client = $this->getOauthClient($request, $projectRepo, $urlGenerator, $centralAuthProject);
104109
$token = $requestStack->getSession()->get('oauth_request_token');
105110

106111
if (!is_a($token, Token::class)) {
@@ -136,13 +141,15 @@ public function oauthCallbackAction(
136141
* (This shouldn't really be in this class, but oh well.)
137142
* @param Request $request
138143
* @param ProjectRepository $projectRepo
144+
* @param UrlGeneratorInterface $urlGenerator
139145
* @param string $centralAuthProject
140146
* @return Client
141147
* @codeCoverageIgnore
142148
*/
143149
protected function getOauthClient(
144150
Request $request,
145151
ProjectRepository $projectRepo,
152+
UrlGeneratorInterface $urlGenerator,
146153
string $centralAuthProject
147154
): Client {
148155
if (isset($this->oauthClient)) {
@@ -156,6 +163,13 @@ protected function getOauthClient(
156163
$consumerKey = $this->getParameter('oauth_key');
157164
$consumerSecret = $this->getParameter('oauth_secret');
158165
$conf->setConsumer(new Consumer($consumerKey, $consumerSecret));
166+
$conf->setUserAgent(
167+
'XTools/'.$this->getParameter('app.version').' ('.
168+
rtrim(
169+
$urlGenerator->generate($this->getIndexRoute(), [], UrlGeneratorInterface::ABSOLUTE_URL),
170+
'/'
171+
).' '.$this->getParameter('mailer.to_email').')'
172+
);
159173
$this->oauthClient = new Client($conf);
160174

161175
// Set the callback URL if given. Used to redirect back to target page after logging in.

src/Controller/XtoolsController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ protected function maxLimit(): int
198198
* @param ProjectRepository $projectRepo
199199
* @param UserRepository $userRepo
200200
* @param PageRepository $pageRepo
201+
* @param Environment $twig
201202
* @param bool $isWMF
202203
* @param string $defaultProject
203204
*/

src/Helper/AutomatedEditsHelper.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
class AutomatedEditsHelper
1818
{
19-
protected CacheItemPoolInterface $cache;
2019
protected SessionInterface $session;
2120

2221
/** @var array The list of tools that are considered reverting. */
@@ -29,11 +28,14 @@ class AutomatedEditsHelper
2928
* AutomatedEditsHelper constructor.
3029
* @param RequestStack $requestStack
3130
* @param CacheItemPoolInterface $cache
31+
* @param \GuzzleHttp\Client $guzzle
3232
*/
33-
public function __construct(RequestStack $requestStack, CacheItemPoolInterface $cache)
34-
{
33+
public function __construct(
34+
RequestStack $requestStack,
35+
protected CacheItemPoolInterface $cache,
36+
protected \GuzzleHttp\Client $guzzle
37+
) {
3538
$this->session = $requestStack->getSession();
36-
$this->cache = $cache;
3739
}
3840

3941
/**
@@ -99,7 +101,7 @@ public function getConfig(bool $useSandbox = false): array
99101
$uri
100102
));
101103
} else {
102-
$resp = json_decode(file_get_contents($uri));
104+
$resp = json_decode($this->guzzle->get($uri)->getBody()->getContents());
103105
}
104106

105107
$ret = json_decode($resp->query->pages[0]->revisions[0]->slots->main->content, true);

tests/TestAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ protected function getAutomatedEditsHelper(?KernelBrowser $client = null): Autom
7070
$session = $this->createSession($client);
7171
return new AutomatedEditsHelper(
7272
$this->getRequestStack($session),
73-
static::getContainer()->get('cache.app')
73+
static::getContainer()->get('cache.app'),
74+
static::getContainer()->get('eight_points_guzzle.client.xtools')
7475
);
7576
}
7677
}

0 commit comments

Comments
 (0)