-
Notifications
You must be signed in to change notification settings - Fork 66
/
Service.php
209 lines (187 loc) · 5.43 KB
/
Service.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
<?php
/**
* @author AlloVince
* @copyright Copyright (c) 2015 EvaEngine Team (https://github.com/EvaEngine)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Eva\EvaOAuth;
use Eva\EvaOAuth\Exception\BadMethodCallException;
use Eva\EvaOAuth\Exception\InvalidArgumentException;
use Eva\EvaOAuth\OAuth1\Consumer;
use Eva\EvaOAuth\OAuth2\Client;
use GuzzleHttp\Subscriber\Log\Formatter;
use GuzzleHttp\Subscriber\Log\LogSubscriber;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Eva\EvaOAuth\OAuth1\Providers\AbstractProvider as OAuth1Provider;
use Eva\EvaOAuth\OAuth2\Providers\AbstractProvider as OAuth2Provider;
/**
* Class Service
* @package Eva\EvaOAuth
*/
class Service
{
/**
* OAuth 1 flag
*/
const OAUTH_VERSION_1 = 'OAuth1';
/**
* OAuth 2 flag
*/
const OAUTH_VERSION_2 = 'OAuth2';
/**
* @var Consumer
*/
protected $consumer;
/**
* @var Client
*/
protected $client;
/**
* @var string
*/
protected $version;
/**
* @var OAuth1Provider|OAuth2Provider
*/
protected $provider;
/**
* @var array
*/
protected static $providers = [
'twitter' => 'Eva\EvaOAuth\OAuth1\Providers\Twitter',
'douban' => 'Eva\EvaOAuth\OAuth2\Providers\Douban',
'tencent' => 'Eva\EvaOAuth\OAuth2\Providers\Tencent',
'weibo' => 'Eva\EvaOAuth\OAuth2\Providers\Weibo',
'hundsun' => 'Eva\EvaOAuth\OAuth2\Providers\Hundsun',
];
/**
* @param string $name
* @param string $class
*/
public static function registerProvider($name, $class)
{
self::$providers[$name] = $class;
}
/**
* @param array $classes
*/
public static function registerProviders(array $classes)
{
foreach ($classes as $name => $class) {
self::$providers[$name] = $class;
}
}
/**
* @return array
*/
public static function getProviders()
{
return self::$providers;
}
/**
* @return OAuth1Provider|OAuth2Provider
*/
public function getProvider()
{
return $this->provider;
}
/**
* @return Consumer|Client
*/
public function getAdapter()
{
if ($this->version === self::OAUTH_VERSION_2) {
return $this->client;
}
return $this->consumer;
}
/**
* Redirect to authorize url
*/
public function requestAuthorize()
{
$this->getAdapter()->requestAuthorize($this->provider);
}
/**
* @return Token\AccessTokenInterface
*/
public function getAccessToken()
{
return $this->getAdapter()->getAccessToken($this->provider);
}
/**
* @param array $options
* @param $version
* @return array
*/
public function convertOptions(array $options, $version)
{
if ($version === self::OAUTH_VERSION_2) {
return array_merge([
'client_id' => $options['key'],
'client_secret' => $options['secret'],
'redirect_uri' => $options['callback'],
], $options);
}
return array_merge([
'consumer_key' => $options['key'],
'consumer_secret' => $options['secret'],
'callback' => $options['callback'],
], $options);
}
/**
* @param string $logPath
* @return $this
*/
public function setLogPath($logPath)
{
$log = new Logger('EvaOAuth');
$log->pushHandler(new StreamHandler($logPath, Logger::DEBUG));
$adapter = $this->getAdapter();
$adapter::getHttpClient()->getEmitter()->attach(new LogSubscriber($log, Formatter::DEBUG));
return $this;
}
/**
* Enable debug mode
* Guzzle will print all request and response on screen
* @return $this
*/
public function debug()
{
$adapter = $this->getAdapter();
$adapter::getHttpClient()->getEmitter()->attach(new LogSubscriber(og, Formatter::DEBUG));
return $this;
}
/**
* @param string $providerName
* @param array $options
*/
public function __construct($providerName, array $options)
{
$providerName = strtolower($providerName);
if (false === isset(self::$providers[$providerName])) {
throw new BadMethodCallException(sprintf('Provider %s not found', $providerName));
}
$options = array_merge([
'key' => '',
'secret' => '',
'callback' => '',
], $options);
$providerClass = self::$providers[$providerName];
$implements = class_implements($providerClass);
if (true === in_array('Eva\EvaOAuth\OAuth2\ResourceServerInterface', $implements)) {
$options = $this->convertOptions($options, self::OAUTH_VERSION_2);
$this->client = new Client($options);
$this->version = self::OAUTH_VERSION_2;
$this->provider = new $providerClass();
} elseif (true === in_array('Eva\EvaOAuth\OAuth1\ServiceProviderInterface', $implements)) {
$options = $this->convertOptions($options, self::OAUTH_VERSION_1);
$this->consumer = new Consumer($options);
$this->version = self::OAUTH_VERSION_1;
$this->provider = new $providerClass();
} else {
throw new InvalidArgumentException(sprintf("Class %s is not correct oauth adapter", $providerClass));
}
}
}