You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

244 lines
5.1 KiB

  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Socialite;
  11. use Closure;
  12. use InvalidArgumentException;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. /**
  16. * Class SocialiteManager.
  17. */
  18. class SocialiteManager implements FactoryInterface
  19. {
  20. /**
  21. * The configuration.
  22. *
  23. * @var \Overtrue\Socialite\Config
  24. */
  25. protected $config;
  26. /**
  27. * The request instance.
  28. *
  29. * @var Request
  30. */
  31. protected $request;
  32. /**
  33. * The registered custom driver creators.
  34. *
  35. * @var array
  36. */
  37. protected $customCreators = [];
  38. /**
  39. * The initial drivers.
  40. *
  41. * @var array
  42. */
  43. protected $initialDrivers = [
  44. 'facebook' => 'Facebook',
  45. 'github' => 'GitHub',
  46. 'google' => 'Google',
  47. 'linkedin' => 'Linkedin',
  48. 'weibo' => 'Weibo',
  49. 'qq' => 'QQ',
  50. 'wechat' => 'WeChat',
  51. 'wechat_open' => 'WeChatOpenPlatform',
  52. 'douban' => 'Douban',
  53. 'wework' => 'WeWork',
  54. ];
  55. /**
  56. * The array of created "drivers".
  57. *
  58. * @var ProviderInterface[]
  59. */
  60. protected $drivers = [];
  61. /**
  62. * SocialiteManager constructor.
  63. *
  64. * @param array $config
  65. * @param Request|null $request
  66. */
  67. public function __construct(array $config, Request $request = null)
  68. {
  69. $this->config = new Config($config);
  70. if ($request) {
  71. $this->setRequest($request);
  72. }
  73. }
  74. /**
  75. * Set config instance.
  76. *
  77. * @param \Overtrue\Socialite\Config $config
  78. *
  79. * @return $this
  80. */
  81. public function config(Config $config)
  82. {
  83. $this->config = $config;
  84. return $this;
  85. }
  86. /**
  87. * Get a driver instance.
  88. *
  89. * @param string $driver
  90. *
  91. * @return ProviderInterface
  92. */
  93. public function driver($driver)
  94. {
  95. if (!isset($this->drivers[$driver])) {
  96. $this->drivers[$driver] = $this->createDriver($driver);
  97. }
  98. return $this->drivers[$driver];
  99. }
  100. /**
  101. * @param \Symfony\Component\HttpFoundation\Request $request
  102. *
  103. * @return $this
  104. */
  105. public function setRequest(Request $request)
  106. {
  107. $this->request = $request;
  108. return $this;
  109. }
  110. /**
  111. * @return \Symfony\Component\HttpFoundation\Request
  112. */
  113. public function getRequest()
  114. {
  115. return $this->request ?: $this->createDefaultRequest();
  116. }
  117. /**
  118. * Create a new driver instance.
  119. *
  120. * @param string $driver
  121. *
  122. * @throws \InvalidArgumentException
  123. *
  124. * @return ProviderInterface
  125. */
  126. protected function createDriver($driver)
  127. {
  128. if (isset($this->initialDrivers[$driver])) {
  129. $provider = $this->initialDrivers[$driver];
  130. $provider = __NAMESPACE__.'\\Providers\\'.$provider.'Provider';
  131. return $this->buildProvider($provider, $this->formatConfig($this->config->get($driver)));
  132. }
  133. if (isset($this->customCreators[$driver])) {
  134. return $this->callCustomCreator($driver);
  135. }
  136. throw new InvalidArgumentException("Driver [$driver] not supported.");
  137. }
  138. /**
  139. * Call a custom driver creator.
  140. *
  141. * @param string $driver
  142. *
  143. * @return ProviderInterface
  144. */
  145. protected function callCustomCreator($driver)
  146. {
  147. return $this->customCreators[$driver]($this->config);
  148. }
  149. /**
  150. * Create default request instance.
  151. *
  152. * @return Request
  153. */
  154. protected function createDefaultRequest()
  155. {
  156. $request = Request::createFromGlobals();
  157. $session = new Session();
  158. $request->setSession($session);
  159. return $request;
  160. }
  161. /**
  162. * Register a custom driver creator Closure.
  163. *
  164. * @param string $driver
  165. * @param \Closure $callback
  166. *
  167. * @return $this
  168. */
  169. public function extend($driver, Closure $callback)
  170. {
  171. $this->customCreators[$driver] = $callback;
  172. return $this;
  173. }
  174. /**
  175. * Get all of the created "drivers".
  176. *
  177. * @return ProviderInterface[]
  178. */
  179. public function getDrivers()
  180. {
  181. return $this->drivers;
  182. }
  183. /**
  184. * Build an OAuth 2 provider instance.
  185. *
  186. * @param string $provider
  187. * @param array $config
  188. *
  189. * @return ProviderInterface
  190. */
  191. public function buildProvider($provider, $config)
  192. {
  193. return new $provider(
  194. $this->getRequest(), $config['client_id'],
  195. $config['client_secret'], $config['redirect']
  196. );
  197. }
  198. /**
  199. * Format the server configuration.
  200. *
  201. * @param array $config
  202. *
  203. * @return array
  204. */
  205. public function formatConfig(array $config)
  206. {
  207. return array_merge([
  208. 'identifier' => $config['client_id'],
  209. 'secret' => $config['client_secret'],
  210. 'callback_uri' => $config['redirect'],
  211. ], $config);
  212. }
  213. }