AuthManager.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace Illuminate\Auth;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use Illuminate\Contracts\Auth\Factory as FactoryContract;
  6. class AuthManager implements FactoryContract
  7. {
  8. use CreatesUserProviders;
  9. /**
  10. * The application instance.
  11. *
  12. * @var \Illuminate\Foundation\Application
  13. */
  14. protected $app;
  15. /**
  16. * The registered custom driver creators.
  17. *
  18. * @var array
  19. */
  20. protected $customCreators = [];
  21. /**
  22. * The array of created "drivers".
  23. *
  24. * @var array
  25. */
  26. protected $guards = [];
  27. /**
  28. * The user resolver shared by various services.
  29. *
  30. * Determines the default user for Gate, Request, and the Authenticatable contract.
  31. *
  32. * @var \Closure
  33. */
  34. protected $userResolver;
  35. /**
  36. * Create a new Auth manager instance.
  37. *
  38. * @param \Illuminate\Foundation\Application $app
  39. * @return void
  40. */
  41. public function __construct($app)
  42. {
  43. $this->app = $app;
  44. $this->userResolver = function ($guard = null) {
  45. return $this->guard($guard)->user();
  46. };
  47. }
  48. /**
  49. * Attempt to get the guard from the local cache.
  50. *
  51. * @param string $name
  52. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
  53. */
  54. public function guard($name = null)
  55. {
  56. $name = $name ?: $this->getDefaultDriver();
  57. return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);
  58. }
  59. /**
  60. * Resolve the given guard.
  61. *
  62. * @param string $name
  63. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
  64. *
  65. * @throws \InvalidArgumentException
  66. */
  67. protected function resolve($name)
  68. {
  69. $config = $this->getConfig($name);
  70. if (is_null($config)) {
  71. throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
  72. }
  73. if (isset($this->customCreators[$config['driver']])) {
  74. return $this->callCustomCreator($name, $config);
  75. }
  76. $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
  77. if (method_exists($this, $driverMethod)) {
  78. return $this->{$driverMethod}($name, $config);
  79. }
  80. throw new InvalidArgumentException("Auth driver [{$config['driver']}] for guard [{$name}] is not defined.");
  81. }
  82. /**
  83. * Call a custom driver creator.
  84. *
  85. * @param string $name
  86. * @param array $config
  87. * @return mixed
  88. */
  89. protected function callCustomCreator($name, array $config)
  90. {
  91. return $this->customCreators[$config['driver']]($this->app, $name, $config);
  92. }
  93. /**
  94. * Create a session based authentication guard.
  95. *
  96. * @param string $name
  97. * @param array $config
  98. * @return \Illuminate\Auth\SessionGuard
  99. */
  100. public function createSessionDriver($name, $config)
  101. {
  102. $provider = $this->createUserProvider($config['provider'] ?? null);
  103. $guard = new SessionGuard($name, $provider, $this->app['session.store']);
  104. // When using the remember me functionality of the authentication services we
  105. // will need to be set the encryption instance of the guard, which allows
  106. // secure, encrypted cookie values to get generated for those cookies.
  107. if (method_exists($guard, 'setCookieJar')) {
  108. $guard->setCookieJar($this->app['cookie']);
  109. }
  110. if (method_exists($guard, 'setDispatcher')) {
  111. $guard->setDispatcher($this->app['events']);
  112. }
  113. if (method_exists($guard, 'setRequest')) {
  114. $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
  115. }
  116. return $guard;
  117. }
  118. /**
  119. * Create a token based authentication guard.
  120. *
  121. * @param string $name
  122. * @param array $config
  123. * @return \Illuminate\Auth\TokenGuard
  124. */
  125. public function createTokenDriver($name, $config)
  126. {
  127. // The token guard implements a basic API token based guard implementation
  128. // that takes an API token field from the request and matches it to the
  129. // user in the database or another persistence layer where users are.
  130. $guard = new TokenGuard(
  131. $this->createUserProvider($config['provider'] ?? null),
  132. $this->app['request']
  133. );
  134. $this->app->refresh('request', $guard, 'setRequest');
  135. return $guard;
  136. }
  137. /**
  138. * Get the guard configuration.
  139. *
  140. * @param string $name
  141. * @return array
  142. */
  143. protected function getConfig($name)
  144. {
  145. return $this->app['config']["auth.guards.{$name}"];
  146. }
  147. /**
  148. * Get the default authentication driver name.
  149. *
  150. * @return string
  151. */
  152. public function getDefaultDriver()
  153. {
  154. return $this->app['config']['auth.defaults.guard'];
  155. }
  156. /**
  157. * Set the default guard driver the factory should serve.
  158. *
  159. * @param string $name
  160. * @return void
  161. */
  162. public function shouldUse($name)
  163. {
  164. $name = $name ?: $this->getDefaultDriver();
  165. $this->setDefaultDriver($name);
  166. $this->userResolver = function ($name = null) {
  167. return $this->guard($name)->user();
  168. };
  169. }
  170. /**
  171. * Set the default authentication driver name.
  172. *
  173. * @param string $name
  174. * @return void
  175. */
  176. public function setDefaultDriver($name)
  177. {
  178. $this->app['config']['auth.defaults.guard'] = $name;
  179. }
  180. /**
  181. * Register a new callback based request guard.
  182. *
  183. * @param string $driver
  184. * @param callable $callback
  185. * @return $this
  186. */
  187. public function viaRequest($driver, callable $callback)
  188. {
  189. return $this->extend($driver, function () use ($callback) {
  190. $guard = new RequestGuard($callback, $this->app['request'], $this->createUserProvider());
  191. $this->app->refresh('request', $guard, 'setRequest');
  192. return $guard;
  193. });
  194. }
  195. /**
  196. * Get the user resolver callback.
  197. *
  198. * @return \Closure
  199. */
  200. public function userResolver()
  201. {
  202. return $this->userResolver;
  203. }
  204. /**
  205. * Set the callback to be used to resolve users.
  206. *
  207. * @param \Closure $userResolver
  208. * @return $this
  209. */
  210. public function resolveUsersUsing(Closure $userResolver)
  211. {
  212. $this->userResolver = $userResolver;
  213. return $this;
  214. }
  215. /**
  216. * Register a custom driver creator Closure.
  217. *
  218. * @param string $driver
  219. * @param \Closure $callback
  220. * @return $this
  221. */
  222. public function extend($driver, Closure $callback)
  223. {
  224. $this->customCreators[$driver] = $callback;
  225. return $this;
  226. }
  227. /**
  228. * Register a custom provider creator Closure.
  229. *
  230. * @param string $name
  231. * @param \Closure $callback
  232. * @return $this
  233. */
  234. public function provider($name, Closure $callback)
  235. {
  236. $this->customProviderCreators[$name] = $callback;
  237. return $this;
  238. }
  239. /**
  240. * Dynamically call the default driver instance.
  241. *
  242. * @param string $method
  243. * @param array $parameters
  244. * @return mixed
  245. */
  246. public function __call($method, $parameters)
  247. {
  248. return $this->guard()->{$method}(...$parameters);
  249. }
  250. }