PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Hybridauth.php

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