|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 Dirk Groenen |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Dirk Groenen <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace DirkGroenen\Pinterest\Auth; |
|
12
|
|
|
|
|
13
|
|
|
use DirkGroenen\Pinterest\Transport\Request; |
|
14
|
|
|
use DirkGroenen\Pinterest\Exceptions\PinterestException; |
|
15
|
|
|
|
|
16
|
|
|
class PinterestOAuth { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The application ID |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $client_id; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The app secret |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $client_secret; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Random string indicating the state |
|
34
|
|
|
* to prevent spoofing |
|
35
|
|
|
* |
|
36
|
|
|
* @var void |
|
37
|
|
|
*/ |
|
38
|
|
|
private $state; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* A reference to the request instance |
|
42
|
|
|
* |
|
43
|
|
|
* @var Request |
|
44
|
|
|
*/ |
|
45
|
|
|
private $request; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Pinterest's oauth endpoint |
|
49
|
|
|
*/ |
|
50
|
|
|
const AUTH_HOST = "https://api.pinterest.com/oauth/"; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Construct |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $client_id |
|
56
|
|
|
* @param string $client_secret |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
*/ |
|
59
|
43 |
|
public function __construct($client_id, $client_secret, $request) |
|
60
|
|
|
{ |
|
61
|
43 |
|
$this->client_id = $client_id; |
|
62
|
43 |
|
$this->client_secret = $client_secret; |
|
63
|
|
|
|
|
64
|
|
|
// Generate and set the state |
|
65
|
43 |
|
$this->state = $this->generateState(); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// Set request instance |
|
68
|
43 |
|
$this->request = $request; |
|
69
|
43 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the login url |
|
73
|
|
|
* |
|
74
|
|
|
* @access public |
|
75
|
|
|
* @param array $scopes |
|
76
|
|
|
* @param string $redirect_uri |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getLoginUrl($redirect_uri, $scopes = array("read_public"), $response_type = "code") |
|
80
|
|
|
{ |
|
81
|
|
|
$queryparams = array( |
|
82
|
|
|
"response_type" => $response_type, |
|
83
|
|
|
"redirect_uri" => $redirect_uri, |
|
84
|
|
|
"client_id" => $this->client_id, |
|
85
|
|
|
"scope" => implode(",", $scopes), |
|
86
|
|
|
"state" => $this->state |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
// Build url and return it |
|
90
|
|
|
return sprintf("%s?%s", self::AUTH_HOST, http_build_query($queryparams)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Generates a random string and returns is |
|
95
|
|
|
* |
|
96
|
|
|
* @access private |
|
97
|
|
|
* @return string random string |
|
98
|
|
|
*/ |
|
99
|
43 |
|
private function generateState() |
|
100
|
|
|
{ |
|
101
|
43 |
|
return substr(md5(rand()), 0, 7); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the generated state |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function getState() |
|
110
|
|
|
{ |
|
111
|
2 |
|
return $this->state; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Set a state manually |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $state |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function setState($state) |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->state = $state; |
|
|
|
|
|
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Change the code for an access_token |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $code |
|
129
|
|
|
* @return \DirkGroenen\Pinterest\Transport\Response |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getOAuthToken($code) |
|
132
|
|
|
{ |
|
133
|
|
|
// Build data array |
|
134
|
|
|
$data = array( |
|
135
|
|
|
"grant_type" => "authorization_code", |
|
136
|
|
|
"client_id" => $this->client_id, |
|
137
|
|
|
"client_secret" => $this->client_secret, |
|
138
|
|
|
"code" => $code |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
// Perform post request |
|
142
|
|
|
$response = $this->request->post("oauth/token", $data); |
|
143
|
|
|
|
|
144
|
|
|
return $response; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set the access_token for further requests |
|
149
|
|
|
* |
|
150
|
|
|
* @access public |
|
151
|
|
|
* @param string $access_token |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
43 |
|
public function setOAuthToken($access_token) |
|
155
|
|
|
{ |
|
156
|
43 |
|
$this->request->setAccessToken($access_token); |
|
157
|
|
|
} |
|
158
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..