forked from jwilsson/spotify-web-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.php
More file actions
279 lines (242 loc) · 7.4 KB
/
Copy pathSession.php
File metadata and controls
279 lines (242 loc) · 7.4 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
namespace SpotifyWebAPI;
class Session
{
protected $accessToken = '';
protected $clientId = '';
protected $clientSecret = '';
protected $expirationTime = 0;
protected $redirectUri = '';
protected $refreshToken = '';
protected $scope = '';
protected $request = null;
/**
* Constructor
* Set up client credentials.
*
* @param string $clientId The client ID.
* @param string $clientSecret The client secret.
* @param string $redirectUri Optional. The redirect URI.
* @param Request $request Optional. The Request object to use.
*/
public function __construct($clientId, $clientSecret, $redirectUri = '', $request = null)
{
$this->setClientId($clientId);
$this->setClientSecret($clientSecret);
$this->setRedirectUri($redirectUri);
$this->request = $request ?: new Request();
}
/**
* Get the authorization URL.
*
* @param array|object $options Optional. Options for the authorization URL.
* - array scope Optional. Scope(s) to request from the user.
* - boolean show_dialog Optional. Whether or not to force the user to always approve the app. Default is false.
* - string state Optional. A CSRF token.
*
* @return string The authorization URL.
*/
public function getAuthorizeUrl($options = [])
{
$options = (array) $options;
$parameters = [
'client_id' => $this->getClientId(),
'redirect_uri' => $this->getRedirectUri(),
'response_type' => 'code',
'scope' => isset($options['scope']) ? implode(' ', $options['scope']) : null,
'show_dialog' => !empty($options['show_dialog']) ? 'true' : null,
'state' => isset($options['state']) ? $options['state'] : null,
];
return Request::ACCOUNT_URL . '/authorize?' . http_build_query($parameters);
}
/**
* Get the access token.
*
* @return string The access token.
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* Get the client ID.
*
* @return string The client ID.
*/
public function getClientId()
{
return $this->clientId;
}
/**
* Get the client secret.
*
* @return string The client secret.
*/
public function getClientSecret()
{
return $this->clientSecret;
}
/**
* Get the access token expiration time.
*
* @return int A Unix timestamp indicating the token expiration time.
*/
public function getTokenExpiration()
{
return $this->expirationTime;
}
/**
* Get the client's redirect URI.
*
* @return string The redirect URI.
*/
public function getRedirectUri()
{
return $this->redirectUri;
}
/**
* Get the refresh token.
*
* @return string The refresh token.
*/
public function getRefreshToken()
{
return $this->refreshToken;
}
/**
* Get the scope for the current access token
*
* @return array The scope for the current access token
*/
public function getScope()
{
return explode(' ', $this->scope);
}
/**
* Refresh an access token.
*
* @param string $refreshToken The refresh token to use.
*
* @return bool Whether the access token was successfully refreshed.
*/
public function refreshAccessToken($refreshToken)
{
$payload = base64_encode($this->getClientId() . ':' . $this->getClientSecret());
$parameters = [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
];
$headers = [
'Authorization' => 'Basic ' . $payload,
];
$response = $this->request->account('POST', '/api/token', $parameters, $headers);
$response = $response['body'];
if (isset($response->access_token)) {
$this->accessToken = $response->access_token;
$this->expirationTime = time() + $response->expires_in;
$this->scope = isset($response->scope) ? $response->scope : $this->scope;
if (isset($response->refresh_token)) {
$this->refreshToken = $response->refresh_token;
} elseif (empty($this->refreshToken)) {
$this->refreshToken = $refreshToken;
}
return true;
}
return false;
}
/**
* Request an access token using the Client Credentials Flow.
*
* @return bool True when an access token was successfully granted, false otherwise.
*/
public function requestCredentialsToken()
{
$payload = base64_encode($this->getClientId() . ':' . $this->getClientSecret());
$parameters = [
'grant_type' => 'client_credentials',
];
$headers = [
'Authorization' => 'Basic ' . $payload,
];
$response = $this->request->account('POST', '/api/token', $parameters, $headers);
$response = $response['body'];
if (isset($response->access_token)) {
$this->accessToken = $response->access_token;
$this->expirationTime = time() + $response->expires_in;
$this->scope = isset($response->scope) ? $response->scope : $this->scope;
return true;
}
return false;
}
/**
* Request an access token given an authorization code.
*
* @param string $authorizationCode The authorization code from Spotify.
*
* @return bool True when the access token was successfully granted, false otherwise.
*/
public function requestAccessToken($authorizationCode)
{
$parameters = [
'client_id' => $this->getClientId(),
'client_secret' => $this->getClientSecret(),
'code' => $authorizationCode,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->getRedirectUri(),
];
$response = $this->request->account('POST', '/api/token', $parameters, []);
$response = $response['body'];
if (isset($response->refresh_token) && isset($response->access_token)) {
$this->refreshToken = $response->refresh_token;
$this->accessToken = $response->access_token;
$this->expirationTime = time() + $response->expires_in;
$this->scope = isset($response->scope) ? $response->scope : $this->scope;
return true;
}
return false;
}
/**
* Set the client ID.
*
* @param string $clientId The client ID.
*
* @return void
*/
public function setClientId($clientId)
{
$this->clientId = $clientId;
}
/**
* Set the client secret.
*
* @param string $clientSecret The client secret.
*
* @return void
*/
public function setClientSecret($clientSecret)
{
$this->clientSecret = $clientSecret;
}
/**
* Set the client's redirect URI.
*
* @param string $redirectUri The redirect URI.
*
* @return void
*/
public function setRedirectUri($redirectUri)
{
$this->redirectUri = $redirectUri;
}
/**
* Set the session's refresh token.
*
* @param string $refreshToken The refresh token.
*
* @return void
*/
public function setRefreshToken($refreshToken)
{
$this->refreshToken = $refreshToken;
}
}