| 1 |
<?php |
| 2 |
/*! |
| 3 |
* Hybridauth |
| 4 |
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth |
| 5 |
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Hybridauth; |
| 9 |
|
| 10 |
use Hybridauth\Exception\InvalidArgumentException; |
| 11 |
use Hybridauth\Exception\UnexpectedValueException; |
| 12 |
use Hybridauth\Storage\StorageInterface; |
| 13 |
use Hybridauth\Logger\LoggerInterface; |
| 14 |
use Hybridauth\Logger\Logger; |
| 15 |
use Hybridauth\HttpClient\HttpClientInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* Hybridauth\Hybridauth |
| 19 |
* |
| 20 |
* For ease of use of multiple providers, Hybridauth implements the class Hybridauth\Hybridauth, |
| 21 |
* a sort of factory/façade which acts as an unified interface or entry point, and it expects a |
| 22 |
* configuration array containing the list of providers you want to use, their respective credentials |
| 23 |
* and authorized callback. |
| 24 |
*/ |
| 25 |
class Hybridauth |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Hybridauth config. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $config; |
| 33 |
|
| 34 |
/** |
| 35 |
* Storage. |
| 36 |
* |
| 37 |
* @var StorageInterface |
| 38 |
*/ |
| 39 |
protected $storage; |
| 40 |
|
| 41 |
/** |
| 42 |
* HttpClient. |
| 43 |
* |
| 44 |
* @var HttpClientInterface |
| 45 |
*/ |
| 46 |
protected $httpClient; |
| 47 |
|
| 48 |
/** |
| 49 |
* Logger. |
| 50 |
* |
| 51 |
* @var LoggerInterface |
| 52 |
*/ |
| 53 |
protected $logger; |
| 54 |
|
| 55 |
/** |
| 56 |
* @param array|string $config Array with configuration or Path to PHP file that will return array |
| 57 |
* @param HttpClientInterface $httpClient |
| 58 |
* @param StorageInterface $storage |
| 59 |
* @param LoggerInterface $logger |
| 60 |
* |
| 61 |
* @throws InvalidArgumentException |
| 62 |
*/ |
| 63 |
public function __construct( |
| 64 |
$config, |
| 65 |
HttpClientInterface $httpClient = null, |
| 66 |
StorageInterface $storage = null, |
| 67 |
LoggerInterface $logger = null |
| 68 |
) { |
| 69 |
if (is_string($config) && file_exists($config)) { |
| 70 |
$config = include $config; |
| 71 |
} elseif (!is_array($config)) { |
| 72 |
throw new InvalidArgumentException('Hybridauth config does not exist on the given path.'); |
| 73 |
} |
| 74 |
|
| 75 |
$this->config = $config + [ |
| 76 |
'debug_mode' => Logger::NONE, |
| 77 |
'debug_file' => '', |
| 78 |
'curl_options' => null, |
| 79 |
'providers' => [] |
| 80 |
]; |
| 81 |
$this->storage = $storage; |
| 82 |
$this->logger = $logger; |
| 83 |
$this->httpClient = $httpClient; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Instantiate the given provider and authentication or authorization protocol. |
| 88 |
* |
| 89 |
* If not authenticated yet, the user will be redirected to the provider's site for |
| 90 |
* authentication/authorisation, otherwise it will simply return an instance of |
| 91 |
* provider's adapter. |
| 92 |
* |
| 93 |
* @param string $name adapter's name (case insensitive) |
| 94 |
* |
| 95 |
* @return \Hybridauth\Adapter\AdapterInterface |
| 96 |
* @throws InvalidArgumentException |
| 97 |
* @throws UnexpectedValueException |
| 98 |
*/ |
| 99 |
public function authenticate($name) |
| 100 |
{ |
| 101 |
$adapter = $this->getAdapter($name); |
| 102 |
|
| 103 |
$adapter->authenticate(); |
| 104 |
|
| 105 |
return $adapter; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns a new instance of a provider's adapter by name |
| 110 |
* |
| 111 |
* @param string $name adapter's name (case insensitive) |
| 112 |
* |
| 113 |
* @return \Hybridauth\Adapter\AdapterInterface |
| 114 |
* @throws InvalidArgumentException |
| 115 |
* @throws UnexpectedValueException |
| 116 |
*/ |
| 117 |
public function getAdapter($name) |
| 118 |
{ |
| 119 |
$config = $this->getProviderConfig($name); |
| 120 |
|
| 121 |
$adapter = isset($config['adapter']) ? $config['adapter'] : sprintf('Hybridauth\\Provider\\%s', $name); |
| 122 |
|
| 123 |
if (!class_exists($adapter)) { |
| 124 |
$adapter = null; |
| 125 |
$fs = new \FilesystemIterator(__DIR__ . '/Provider/'); |
| 126 |
/** @var \SplFileInfo $file */ |
| 127 |
foreach ($fs as $file) { |
| 128 |
if (!$file->isDir()) { |
| 129 |
$provider = strtok($file->getFilename(), '.'); |
| 130 |
if (strtolower($name) === mb_strtolower($provider)) { |
| 131 |
$adapter = sprintf('Hybridauth\\Provider\\%s', $provider); |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
if ($adapter === null) { |
| 137 |
throw new InvalidArgumentException('Unknown Provider.'); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return new $adapter($config, $this->httpClient, $this->storage, $this->logger); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get provider config by name. |
| 146 |
* |
| 147 |
* @param string $name adapter's name (case insensitive) |
| 148 |
* |
| 149 |
* @throws UnexpectedValueException |
| 150 |
* @throws InvalidArgumentException |
| 151 |
* |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public function getProviderConfig($name) |
| 155 |
{ |
| 156 |
$name = strtolower($name); |
| 157 |
|
| 158 |
$providersConfig = array_change_key_case($this->config['providers'], CASE_LOWER); |
| 159 |
|
| 160 |
if (!isset($providersConfig[$name])) { |
| 161 |
throw new InvalidArgumentException('Unknown Provider.'); |
| 162 |
} |
| 163 |
|
| 164 |
if (!$providersConfig[$name]['enabled']) { |
| 165 |
throw new UnexpectedValueException('Disabled Provider.'); |
| 166 |
} |
| 167 |
|
| 168 |
$config = $providersConfig[$name]; |
| 169 |
$config += [ |
| 170 |
'debug_mode' => $this->config['debug_mode'], |
| 171 |
'debug_file' => $this->config['debug_file'], |
| 172 |
]; |
| 173 |
|
| 174 |
if (!isset($config['callback']) && isset($this->config['callback'])) { |
| 175 |
$config['callback'] = $this->config['callback']; |
| 176 |
} |
| 177 |
|
| 178 |
return $config; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns a boolean of whether the user is connected with a provider |
| 183 |
* |
| 184 |
* @param string $name adapter's name (case insensitive) |
| 185 |
* |
| 186 |
* @return bool |
| 187 |
* @throws InvalidArgumentException |
| 188 |
* @throws UnexpectedValueException |
| 189 |
*/ |
| 190 |
public function isConnectedWith($name) |
| 191 |
{ |
| 192 |
return $this->getAdapter($name)->isConnected(); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Returns a list of enabled adapters names |
| 197 |
* |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
public function getProviders() |
| 201 |
{ |
| 202 |
$providers = []; |
| 203 |
|
| 204 |
foreach ($this->config['providers'] as $name => $config) { |
| 205 |
if ($config['enabled']) { |
| 206 |
$providers[] = $name; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return $providers; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Returns a list of currently connected adapters names |
| 215 |
* |
| 216 |
* @return array |
| 217 |
* @throws InvalidArgumentException |
| 218 |
* @throws UnexpectedValueException |
| 219 |
*/ |
| 220 |
public function getConnectedProviders() |
| 221 |
{ |
| 222 |
$providers = []; |
| 223 |
|
| 224 |
foreach ($this->getProviders() as $name) { |
| 225 |
if ($this->isConnectedWith($name)) { |
| 226 |
$providers[] = $name; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return $providers; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Returns a list of new instances of currently connected adapters |
| 235 |
* |
| 236 |
* @return \Hybridauth\Adapter\AdapterInterface[] |
| 237 |
* @throws InvalidArgumentException |
| 238 |
* @throws UnexpectedValueException |
| 239 |
*/ |
| 240 |
public function getConnectedAdapters() |
| 241 |
{ |
| 242 |
$adapters = []; |
| 243 |
|
| 244 |
foreach ($this->getProviders() as $name) { |
| 245 |
$adapter = $this->getAdapter($name); |
| 246 |
|
| 247 |
if ($adapter->isConnected()) { |
| 248 |
$adapters[$name] = $adapter; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return $adapters; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Disconnect all currently connected adapters at once |
| 257 |
*/ |
| 258 |
public function disconnectAllAdapters() |
| 259 |
{ |
| 260 |
foreach ($this->getProviders() as $name) { |
| 261 |
$adapter = $this->getAdapter($name); |
| 262 |
|
| 263 |
if ($adapter->isConnected()) { |
| 264 |
$adapter->disconnect(); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|