This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathAuth.php
241 lines (206 loc) · 6.19 KB
/
Auth.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace seregazhuk\PinterestBot\Api\Providers;
use LogicException;
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
use seregazhuk\PinterestBot\Api\Forms\Registration;
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
use seregazhuk\PinterestBot\Api\Traits\ResolvesCurrentUser;
use seregazhuk\PinterestBot\Api\Traits\SendsRegisterActions;
class Auth extends Provider
{
use SendsRegisterActions, ResolvesCurrentUser;
/**
* @var array
*/
protected $loginRequiredFor = [
'logout',
'convertToBusiness',
];
const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105';
/**
* Login as pinner.
*
* @param string $username
* @param string $password
* @param bool $autoLogin
* @return bool
*/
public function login($username, $password, $autoLogin = true)
{
if ($this->isLoggedIn()) {
return true;
}
$this->checkCredentials($username, $password);
// Trying to load previously saved cookies from last login session for this username.
// Then grab user profile info to check, if cookies are ok. If an empty response
// was returned, then send login request.
if ($autoLogin && $this->processAutoLogin($username)) {
return true;
}
return $this->processLogin($username, $password);
}
public function logout()
{
$this->request->logout();
}
/**
* Register a new user.
*
* @param string|Registration $email
* @param string|null $password
* @param string|null $name
* @param string $country @deprecated
* @param int $age @deprecated
*
* @return bool
*/
public function register($email, $password = null, $name = null, $country = 'GB', $age = 18)
{
$registrationForm = $this->getRegistrationForm($email, $password, $name, $country, $age);
return $this->makeRegisterCall($registrationForm);
}
/**
* Register a new business account. At first we register a basic type account.
* Then convert it to a business one. This is done to receive a confirmation
* email after registration.
*
* @param string|Registration $registrationForm
* @param string $password
* @param string $name
* @param string $website
* @return bool|mixed
*/
public function registerBusiness($registrationForm, $password = null, $name = null, $website = '')
{
$registration = $this->register($registrationForm, $password, $name);
if (!$registration) {
return false;
}
$website = ($registrationForm instanceof Registration) ?
$registrationForm->getSite() : $website;
return $this->convertToBusiness($name, $website);
}
/**
* Convert your account to a business one.
*
* @param string $businessName
* @param string $websiteUrl
* @return bool
*/
public function convertToBusiness($businessName, $websiteUrl = '')
{
$data = [
'business_name' => $businessName,
'website_url' => $websiteUrl,
'account_type' => 'other',
];
return $this->post(UrlBuilder::RESOURCE_CONVERT_TO_BUSINESS, $data);
}
/**
* @param string $link
* @return array|bool
*/
public function confirmEmail($link)
{
return $this->get($link);
}
/**
* Validates password and login.
*
* @param string $username
* @param string $password
* @throws LogicException
*/
protected function checkCredentials($username, $password)
{
if (!$username || !$password) {
throw new LogicException('You must set username and password to login.');
}
}
/**
* @return bool
*/
protected function completeRegistration()
{
return $this->post(
UrlBuilder::RESOURCE_REGISTRATION_COMPLETE,
['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID]
);
}
/**
* @param Registration $registrationForm
* @return bool|mixed
*/
protected function makeRegisterCall(Registration $registrationForm)
{
if (!$this->sendEmailVerificationAction()) {
return false;
}
if (!$this->post(UrlBuilder::RESOURCE_CREATE_REGISTER, $registrationForm->toArray())) {
return false;
}
if (!$this->sendRegistrationActions()) {
return false;
}
return $this->completeRegistration();
}
/**
* @param string $username
* @param string $password
* @return bool
*/
protected function processLogin($username, $password)
{
$this->request->loadCookiesFor($username);
$credentials = [
'username_or_email' => $username,
'password' => $password,
];
$this->post(UrlBuilder::RESOURCE_LOGIN, $credentials);
if ($this->response->isEmpty()) {
return false;
}
$this->request->login();
return true;
}
/**
* @param string $username
* @return bool
*/
protected function processAutoLogin($username)
{
return $this->request->autoLogin($username) && $this->resolveCurrentUserId();
}
/**
* @param string $registrationForm
* @param string $password
* @param string $name
* @param string $country
* @param string $age
* @return Registration
*/
protected function fillRegistrationForm($registrationForm, $password, $name, $country, $age)
{
return (new Registration($registrationForm, $password, $name))
->setCountry($country)
->setAge($age)
->setGender('male');
}
/**
* @param string|Registration $email
* @param string $password
* @param string $name
* @param string $country
* @param string $age
* @return Registration
*/
protected function getRegistrationForm($email, $password, $name, $country, $age)
{
if ($email instanceof Registration) {
return $email;
}
return $this->fillRegistrationForm(
$email, $password, $name, $country, $age
);
}
}