1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Libcast\AssetDistributor\Dailymotion; |
4
|
|
|
|
5
|
|
|
use Dailymotion as DailymotionBase; |
6
|
|
|
use Libcast\AssetDistributor\Request; |
7
|
|
|
|
8
|
|
|
class Dailymotion extends DailymotionBase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* {@inheritdoc} |
12
|
|
|
*/ |
13
|
|
|
public function getAccessToken($forceRefresh = false) |
14
|
|
|
{ |
15
|
|
|
if ($this->grantType === null) { |
16
|
|
|
// No grant type defined, the request won't be authenticated |
17
|
|
|
return null; |
18
|
|
|
} |
19
|
|
|
$session = $this->getSession(); |
20
|
|
|
|
21
|
|
|
// Check if session is present and if it was created for the same grant type |
22
|
|
|
// i.e: if the grant type to create the session was `AUTHORIZATION` and the current grant type is |
23
|
|
|
// `CLIENT_CREDENTIALS`, we don't want to call the API on behalf of another user. |
24
|
|
|
if (!empty($session) && isset($session['grant_type']) && ((int)$session['grant_type'] === $this->grantType)) { |
25
|
|
|
if (!$forceRefresh && isset($session['access_token'])) { |
26
|
|
|
if (!isset($session['expires']) || (time() < $session['expires'])) { |
27
|
|
|
return $session['access_token']; |
28
|
|
|
} |
29
|
|
|
// else: Token expired |
30
|
|
|
} |
31
|
|
|
// No valid access token found, try to refresh it |
32
|
|
|
if (isset($session['refresh_token'])) { |
33
|
|
|
$grantType = $session['grant_type']; |
34
|
|
|
$session = $this->oauthTokenRequest(array( |
35
|
|
|
'grant_type' => 'refresh_token', |
36
|
|
|
'client_id' => $this->grantInfo['key'], |
37
|
|
|
'client_secret' => $this->grantInfo['secret'], |
38
|
|
|
'scope' => implode(chr(32), $this->grantInfo['scope']), |
39
|
|
|
'refresh_token' => $session['refresh_token'], |
40
|
|
|
)); |
41
|
|
|
$session['grant_type'] = $grantType; |
42
|
|
|
$this->setSession($session); |
43
|
|
|
return $session['access_token']; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
try { |
47
|
|
|
if ($this->grantType === self::GRANT_TYPE_AUTHORIZATION) { |
48
|
|
|
// Use Request singleton object to avoid getting values from another Adapter |
49
|
|
|
$request = Request::get(); |
50
|
|
|
$code = $request->query->get('code'); |
51
|
|
|
|
52
|
|
|
$error = filter_input(INPUT_GET, 'error'); |
53
|
|
|
|
54
|
|
|
if (!empty($code)) { |
55
|
|
|
// We've been called back by authorization server |
56
|
|
|
$session = $this->oauthTokenRequest(array( |
57
|
|
|
'grant_type' => 'authorization_code', |
58
|
|
|
'client_id' => $this->grantInfo['key'], |
59
|
|
|
'client_secret' => $this->grantInfo['secret'], |
60
|
|
|
'scope' => implode(chr(32), $this->grantInfo['scope']), |
61
|
|
|
'redirect_uri' => $this->grantInfo['redirect_uri'], |
62
|
|
|
'code' => $code, |
63
|
|
|
)); |
64
|
|
|
$session['grant_type'] = $this->grantType; |
65
|
|
|
$this->setSession($session); |
66
|
|
|
return $session['access_token']; |
67
|
|
|
} elseif (!empty($error)) { |
68
|
|
|
$message = filter_input(INPUT_GET, 'error_description'); |
69
|
|
|
if ($error === 'access_denied') { |
70
|
|
|
$e = new \DailymotionAuthRefusedException($message); |
71
|
|
|
} else { |
72
|
|
|
$e = new \DailymotionAuthException($message); |
73
|
|
|
} |
74
|
|
|
$e->error = $error; |
75
|
|
|
throw $e; |
76
|
|
|
} else { |
77
|
|
|
// Ask the client to request end-user authorization |
78
|
|
|
throw new \DailymotionAuthRequiredException(); |
79
|
|
|
} |
80
|
|
|
} elseif ($this->grantType === self::GRANT_TYPE_CLIENT_CREDENTIALS) { |
81
|
|
|
$session = $this->oauthTokenRequest(array( |
82
|
|
|
'grant_type' => 'client_credentials', |
83
|
|
|
'client_id' => $this->grantInfo['key'], |
84
|
|
|
'client_secret' => $this->grantInfo['secret'], |
85
|
|
|
'scope' => implode(chr(32), $this->grantInfo['scope']), |
86
|
|
|
)); |
87
|
|
|
$session['grant_type'] = $this->grantType; |
88
|
|
|
$this->setSession($session); |
89
|
|
|
return $session['access_token']; |
90
|
|
|
} elseif ($this->grantType === self::GRANT_TYPE_PASSWORD) { |
91
|
|
|
if (!isset($this->grantInfo['username']) || !isset($this->grantInfo['password'])) { |
92
|
|
|
// Ask the client to request end-user credentials |
93
|
|
|
throw new \DailymotionAuthRequiredException(); |
94
|
|
|
} |
95
|
|
|
$session = $this->oauthTokenRequest(array( |
96
|
|
|
'grant_type' => 'password', |
97
|
|
|
'client_id' => $this->grantInfo['key'], |
98
|
|
|
'client_secret' => $this->grantInfo['secret'], |
99
|
|
|
'scope' => implode(chr(32), $this->grantInfo['scope']), |
100
|
|
|
'username' => $this->grantInfo['username'], |
101
|
|
|
'password' => $this->grantInfo['password'], |
102
|
|
|
)); |
103
|
|
|
$session['grant_type'] = $this->grantType; |
104
|
|
|
$this->setSession($session); |
105
|
|
|
return $session['access_token']; |
106
|
|
|
} |
107
|
|
|
} catch (\DailymotionAuthException $e) { |
108
|
|
|
// clear session on error |
109
|
|
|
$this->clearSession(); |
110
|
|
|
throw $e; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|