Skip to content

Commit 2cd36f0

Browse files
committed
try to not make another request to authorize and rather use the response of the POST /session request
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 73a3375 commit 2cd36f0

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

tests/integration/GithubOauthIntegrationTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ private function loginToGitHub(string $authorizeUrl): array {
223223
$redirectHistory = $loginResponse->getHeader('X-Guzzle-Redirect-History');
224224
if (!empty($redirectHistory)) {
225225
$finalUrl = end($redirectHistory);
226+
if (str_contains($finalUrl, 'code=')) {
227+
return [
228+
'status' => 'redirect_with_code',
229+
'redirect_url' => $finalUrl,
230+
'body' => $loginBody,
231+
];
232+
}
226233
if (str_contains($finalUrl, 'oauth/authorize')) {
227234
return [
228235
'status' => 'authorize_page',
@@ -231,15 +238,24 @@ private function loginToGitHub(string $authorizeUrl): array {
231238
}
232239
}
233240

241+
if (str_contains($loginBody, 'oauth/authorize') || str_contains($loginBody, 'Authorize')) {
242+
return [
243+
'status' => 'authorize_page',
244+
'body' => $loginBody,
245+
];
246+
}
247+
234248
return [
235249
'status' => 'logged_in',
236250
'body' => $loginBody,
237251
];
238252
}
239253

240-
private function handleAuthorizePage(string $authorizeUrl): string {
241-
$response = $this->client->get($authorizeUrl);
242-
$body = $response->getBody()->getContents();
254+
private function handleAuthorizePage(string $authorizeUrl, ?string $body = null): string {
255+
if ($body === null) {
256+
$response = $this->client->get($authorizeUrl);
257+
$body = $response->getBody()->getContents();
258+
}
243259

244260
$doc = new DOMDocument();
245261
libxml_use_internal_errors(true);
@@ -305,9 +321,12 @@ public function testOAuthLogin(): void {
305321
$this->fail('Invalid GitHub credentials');
306322
}
307323

308-
echo 'STATUS of login: ' . $loginResult['status'] . "\n\n";
309324
$redirectUrl = null;
310-
if ($loginResult['status'] === 'authorize_page' || $loginResult['status'] === 'logged_in') {
325+
if ($loginResult['status'] === 'redirect_with_code') {
326+
$redirectUrl = $loginResult['redirect_url'];
327+
} elseif ($loginResult['status'] === 'authorize_page') {
328+
$redirectUrl = $this->handleAuthorizePage($authorizeUrl, $loginResult['body']);
329+
} elseif ($loginResult['status'] === 'logged_in') {
311330
$redirectUrl = $this->handleAuthorizePage($authorizeUrl);
312331
} else {
313332
$this->fail('Unexpected login status: ' . $loginResult['status']);

0 commit comments

Comments
 (0)