-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathLogin.php
192 lines (155 loc) · 5.84 KB
/
Login.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
<?php
declare(strict_types=1);
namespace Instagram\Auth;
use GuzzleHttp\{ClientInterface, Cookie\SetCookie, Cookie\CookieJar};
use GuzzleHttp\Exception\ClientException;
use Instagram\Auth\Checkpoint\{Challenge, ImapClient};
use Instagram\Exception\InstagramAuthException;
use Instagram\Utils\{InstagramHelper, OptionHelper, CacheResponse};
class Login
{
/**
* @var ClientInterface
*/
private $client;
/**
* @var string
*/
private $login;
/**
* @var string
*/
private $password;
/**
* @var ImapClient|null
*/
private $imapClient;
/**
* @var int
*/
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, ?int $challengeDelay = 3)
{
$this->client = $client;
$this->login = $login;
$this->password = $password;
$this->imapClient = $imapClient;
$this->challengeDelay = $challengeDelay;
}
/**
* @return CookieJar
*
* @throws InstagramAuthException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function process(): CookieJar
{
$baseRequest = $this->client->request('GET', InstagramHelper::URL_BASE, [
'headers' => [
'user-agent' => OptionHelper::$USER_AGENT,
],
]);
CacheResponse::setResponse($baseRequest);
$html = (string) $baseRequest->getBody();
$pattern = '/{"csrf_token":"([^"]+)"}/';
preg_match($pattern, $html, $matches);
if (!isset($matches[1])) {
throw new InstagramAuthException('Unable to extract JSON data');
}
$csrfToken = $matches[1];
$cookieJar = new CookieJar();
try {
$query = $this->client->request('POST', InstagramHelper::URL_AUTH, [
'form_params' => [
'username' => $this->login,
'enc_password' => '#PWD_INSTAGRAM_BROWSER:0:' . time() . ':' . $this->password,
],
'headers' => [
'cookie' => 'ig_cb=1; csrftoken=' . $csrfToken,
'referer' => InstagramHelper::URL_BASE,
'x-csrftoken' => $csrfToken,
'user-agent' => OptionHelper::$USER_AGENT,
'accept-language' => OptionHelper::$LOCALE,
],
'cookies' => $cookieJar,
]);
} catch (ClientException $exception) {
CacheResponse::setResponse($exception->getResponse());
$data = json_decode((string) $exception->getResponse()->getBody());
if ($data && $data->message === 'checkpoint_required') {
// @codeCoverageIgnoreStart
return $this->checkpointChallenge($cookieJar, $data);
// @codeCoverageIgnoreEnd
} else {
throw new InstagramAuthException('Unknown error, please report it with a GitHub issue. ' . $exception->getMessage());
}
}
CacheResponse::setResponse($query);
$response = json_decode((string) $query->getBody());
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');
}
}
/**
* @param \array $session
*
* @return CookieJar
*
* @throws InstagramAuthException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function withCookies(array $session): CookieJar
{
$cookies = new CookieJar(true, [$session]);
$baseRequest = $this->client->request('GET', InstagramHelper::URL_BASE, [
'headers' => [
'user-agent' => OptionHelper::$USER_AGENT,
],
'cookies' => $cookies
]);
CacheResponse::setResponse($baseRequest);
$html = (string) $baseRequest->getBody();
preg_match('/\\\"csrf_token\\\":\\\"(.*?)\\\"/', $html, $matches);
if (isset($matches[1])) {
$data = $matches[1];
if (!isset($data->config->viewer) && !isset($data->config->viewerId)) {
throw new InstagramAuthException('Please login with instagram credentials.');
}
}
return $cookies;
}
/**
* @param CookieJar $cookieJar
* @param \StdClass $data
*
* @return CookieJar
*
* @throws InstagramAuthException
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @codeCoverageIgnore
*/
private function checkpointChallenge(CookieJar $cookieJar, \StdClass $data): CookieJar
{
if (!$this->imapClient instanceof ImapClient) {
throw new InstagramAuthException('Checkpoint required, please provide IMAP credentials to process authentication.');
}
$challenge = new Challenge($this->client, $cookieJar, $data->checkpoint_url, $this->challengeDelay);
$challengeContent = $challenge->fetchChallengeContent();
$challenge->sendSecurityCode($challengeContent);
//$challenge->reSendSecurityCode($challengeContent);
$code = $this->imapClient->getLastInstagramEmailContent();
return $challenge->submitSecurityCode($challengeContent, $code);
}
}