From 8a35f100e22b16a9ec4f3bc199d76c1d63d4a940 Mon Sep 17 00:00:00 2001 From: David Greminger Date: Thu, 12 Nov 2020 09:36:52 +0100 Subject: [PATCH] Make the challenge delay configurable --- src/Instagram/Api.php | 15 +++++++++++---- src/Instagram/Auth/Login.php | 19 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Instagram/Api.php b/src/Instagram/Api.php index 33830b3..f2107b8 100644 --- a/src/Instagram/Api.php +++ b/src/Instagram/Api.php @@ -60,14 +60,21 @@ class Api */ protected $session = null; + /** + * @var int|null + */ + protected $challengeDelay; + /** * @param CacheItemPoolInterface $cachePool * @param ClientInterface|null $client + * @param int|null $challengeDelay */ - public function __construct(CacheItemPoolInterface $cachePool, ClientInterface $client = null) + public function __construct(CacheItemPoolInterface $cachePool, ClientInterface $client = null, ?int $challengeDelay = null) { - $this->cachePool = $cachePool; - $this->client = $client ?: new Client(); + $this->cachePool = $cachePool; + $this->client = $client ?: new Client(); + $this->challengeDelay = $challengeDelay; } /** @@ -81,7 +88,7 @@ public function __construct(CacheItemPoolInterface $cachePool, ClientInterface $ */ public function login(string $username, string $password, ?ImapClient $imapClient = null): void { - $login = new Login($this->client, $username, $password, $imapClient); + $login = new Login($this->client, $username, $password, $imapClient, $this->challengeDelay); // fetch previous session an re-use it $sessionData = $this->cachePool->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($username)); diff --git a/src/Instagram/Auth/Login.php b/src/Instagram/Auth/Login.php index 88bd852..f66c351 100644 --- a/src/Instagram/Auth/Login.php +++ b/src/Instagram/Auth/Login.php @@ -32,18 +32,25 @@ class Login */ private $imapClient; + /** + * @var int|null + */ + private $challengeDelay; + /** * @param ClientInterface $client * @param string $login * @param string $password * @param ImapClient|null $imapClient + * @param int|null $challengeDelay */ - public function __construct(ClientInterface $client, string $login, string $password, ?ImapClient $imapClient = null) + public function __construct(ClientInterface $client, string $login, string $password, ?ImapClient $imapClient = null, ?int $challengeDelay = null) { - $this->client = $client; - $this->login = $login; - $this->password = $password; - $this->imapClient = $imapClient; + $this->client = $client; + $this->login = $login; + $this->password = $password; + $this->imapClient = $imapClient; + $this->challengeDelay = $challengeDelay; } /** @@ -124,7 +131,7 @@ private function checkpointChallenge(CookieJar $cookieJar, \StdClass $data): Coo throw new InstagramAuthException('Checkpoint required, please provide IMAP credentials to process authentication.'); } - $challenge = new Challenge($this->client, $cookieJar, $data->checkpoint_url); + $challenge = new Challenge($this->client, $cookieJar, $data->checkpoint_url, $this->challengeDelay); $challengeContent = $challenge->fetchChallengeContent();