|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/twitter/ |
|
4
|
|
|
* @copyright Copyright (c) Dukt |
|
5
|
|
|
* @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\twitter\services; |
|
9
|
|
|
|
|
10
|
|
|
use Craft; |
|
11
|
|
|
use dukt\twitter\errors\InvalidAccountException; |
|
12
|
|
|
use dukt\twitter\Plugin; |
|
13
|
|
|
use yii\base\Component; |
|
14
|
|
|
use League\OAuth1\Client\Credentials\TokenCredentials; |
|
15
|
|
|
use craft\helpers\UrlHelper; |
|
16
|
|
|
use \League\OAuth1\Client\Server\Twitter as TwitterProvider; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* OAuth Service |
|
20
|
|
|
* |
|
21
|
|
|
* @author Dukt <[email protected]> |
|
22
|
|
|
* @since 3.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Oauth extends Component |
|
25
|
|
|
{ |
|
26
|
|
|
// Properties |
|
27
|
|
|
// ========================================================================= |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var TokenCredentials|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $token; |
|
33
|
|
|
|
|
34
|
|
|
// Public Methods |
|
35
|
|
|
// ========================================================================= |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Saves a token |
|
39
|
|
|
* |
|
40
|
|
|
* @param TokenCredentials $token |
|
41
|
|
|
* @return bool |
|
42
|
|
|
* @throws InvalidAccountException |
|
43
|
|
|
* @throws \yii\db\Exception |
|
44
|
|
|
*/ |
|
45
|
|
|
public function saveToken(TokenCredentials $token) |
|
46
|
|
|
{ |
|
47
|
|
|
$account = Plugin::$plugin->getAccounts()->getAccount(); |
|
48
|
|
|
$account->token = $token->getIdentifier(); |
|
49
|
|
|
$account->tokenSecret = $token->getSecret(); |
|
50
|
|
|
|
|
51
|
|
|
return Plugin::$plugin->getAccounts()->saveAccount($account); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets a token |
|
56
|
|
|
* |
|
57
|
|
|
* @return TokenCredentials|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getToken() |
|
60
|
|
|
{ |
|
61
|
|
|
$account = Plugin::$plugin->getAccounts()->getAccount(); |
|
62
|
|
|
|
|
63
|
|
|
if (!$account || !$account->token || !$account->tokenSecret) { |
|
|
|
|
|
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$token = new TokenCredentials(); |
|
68
|
|
|
$token->setIdentifier($account->token); |
|
69
|
|
|
$token->setSecret($account->tokenSecret); |
|
70
|
|
|
|
|
71
|
|
|
return $token; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Deletes a token |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
* @throws \Throwable |
|
79
|
|
|
* @throws \yii\db\StaleObjectException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function deleteToken() |
|
82
|
|
|
{ |
|
83
|
|
|
$account = Plugin::$plugin->getAccounts()->getAccount(); |
|
84
|
|
|
|
|
85
|
|
|
return Plugin::$plugin->getAccounts()->deleteAccount($account); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns a Twitter provider (server) object. |
|
90
|
|
|
* |
|
91
|
|
|
* @return TwitterProvider |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getOauthProvider() |
|
94
|
|
|
{ |
|
95
|
|
|
$options = [ |
|
96
|
|
|
'identifier' => Plugin::getInstance()->getConsumerKey(), |
|
97
|
|
|
'secret' => Craft::parseEnv(Plugin::getInstance()->getConsumerSecret()), |
|
|
|
|
|
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
if (!isset($options['callback_uri'])) { |
|
101
|
|
|
$options['callback_uri'] = UrlHelper::actionUrl('twitter/oauth/callback'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return new TwitterProvider($options); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Gets the javascript origin |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getJavascriptOrigin() |
|
114
|
|
|
{ |
|
115
|
|
|
return UrlHelper::baseUrl(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get redirect URI |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getRedirectUri() |
|
124
|
|
|
{ |
|
125
|
|
|
return UrlHelper::actionUrl('twitter/oauth/callback'); |
|
126
|
|
|
} |
|
127
|
|
|
} |