Skip to content

Commit

Permalink
Add catching of generic login errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrimaud committed Mar 13, 2021
1 parent c696b6c commit 2d6e0ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/Instagram/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class Login

/**
* @param ClientInterface $client
* @param string $login
* @param string $password
* @param string $login
* @param string $password
* @param ImapClient|null $imapClient
* @param int|null $challengeDelay
* @param int|null $challengeDelay
*/
public function __construct(ClientInterface $client, string $login, string $password, ?ImapClient $imapClient = null, ?int $challengeDelay = 3)
{
Expand All @@ -63,11 +63,11 @@ public function process(): CookieJar
{
$baseRequest = $this->client->request('GET', InstagramHelper::URL_BASE, [
'headers' => [
'user-agent' => UserAgentHelper::AGENT_DEFAULT
]
'user-agent' => UserAgentHelper::AGENT_DEFAULT,
],
]);

$html = (string)$baseRequest->getBody();
$html = (string) $baseRequest->getBody();

preg_match('/<script type="text\/javascript">window\._sharedData\s?=(.+);<\/script>/', $html, $matches);

Expand All @@ -91,10 +91,10 @@ public function process(): CookieJar
'x-csrftoken' => $data->config->csrf_token,
'user-agent' => UserAgentHelper::AGENT_DEFAULT,
],
'cookies' => $cookieJar
'cookies' => $cookieJar,
]);
} catch (ClientException $exception) {
$data = json_decode((string)$exception->getResponse()->getBody());
$data = json_decode((string) $exception->getResponse()->getBody());

if ($data && $data->message === 'checkpoint_required') {
// @codeCoverageIgnoreStart
Expand All @@ -105,10 +105,12 @@ public function process(): CookieJar
}
}

$response = json_decode((string)$query->getBody());
$response = json_decode((string) $query->getBody());

if ($response->authenticated == true) {
if (property_exists($response, 'authenticated') && $response->authenticated == true) {
return $cookieJar;
} else if (property_exists($response, 'error_type') && $response->error_type === 'generic_request_error') {
throw new InstagramAuthException('Generic error / Your IP may be block from Instagram. You should consider using a proxy.');
} else {
throw new InstagramAuthException('Wrong login / password');
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Auth/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ public function testLoginWithUnknownError()
$this->assertInstanceOf(CookieJar::class, $cookies);
}

public function testLoginWithGenericError()
{
$this->expectException(InstagramAuthException::class);
$this->expectExceptionMessage('Generic error / Your IP may be block from Instagram. You should consider using a proxy.');

$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/../fixtures/home.html')),
new Response(200, [], file_get_contents(__DIR__ . '/../fixtures/login-generic-errors.json')),
]);

$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);

$login = new Login($client, 'username', 'password');
$cookies = $login->process();

$this->assertInstanceOf(CookieJar::class, $cookies);
}

public function testSucceededLogin()
{
$mock = new MockHandler([
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/login-generic-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"errors":{"error":["Sorry, there was a problem with your request."]},"status":"ok","error_type":"generic_request_error"}

0 comments on commit 2d6e0ab

Please sign in to comment.