driver.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php namespace Laravel\Auth\Drivers;
  2. use Laravel\Str;
  3. use Laravel\Cookie;
  4. use Laravel\Config;
  5. use Laravel\Event;
  6. use Laravel\Session;
  7. use Laravel\Crypter;
  8. abstract class Driver {
  9. /**
  10. * The user currently being managed by the driver.
  11. *
  12. * @var mixed
  13. */
  14. public $user;
  15. /**
  16. * The current value of the user's token.
  17. *
  18. * @var string|null
  19. */
  20. public $token;
  21. /**
  22. * Create a new login auth driver instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. if (Session::started())
  29. {
  30. $this->token = Session::get($this->token());
  31. }
  32. // If a token did not exist in the session for the user, we will attempt
  33. // to load the value of a "remember me" cookie for the driver, which
  34. // serves as a long-lived client side authenticator for the user.
  35. if (is_null($this->token))
  36. {
  37. $this->token = $this->recall();
  38. }
  39. }
  40. /**
  41. * Determine if the user of the application is not logged in.
  42. *
  43. * This method is the inverse of the "check" method.
  44. *
  45. * @return bool
  46. */
  47. public function guest()
  48. {
  49. return ! $this->check();
  50. }
  51. /**
  52. * Determine if the user is logged in.
  53. *
  54. * @return bool
  55. */
  56. public function check()
  57. {
  58. return ! is_null($this->user());
  59. }
  60. /**
  61. * Get the current user of the application.
  62. *
  63. * If the user is a guest, null should be returned.
  64. *
  65. * @return mixed|null
  66. */
  67. public function user()
  68. {
  69. if ( ! is_null($this->user)) return $this->user;
  70. return $this->user = $this->retrieve($this->token);
  71. }
  72. /**
  73. * Get the given application user by ID.
  74. *
  75. * @param int $id
  76. * @return mixed
  77. */
  78. abstract public function retrieve($id);
  79. /**
  80. * Attempt to log a user into the application.
  81. *
  82. * @param array $arguments
  83. * @return void
  84. */
  85. abstract public function attempt($arguments = array());
  86. /**
  87. * Login the user assigned to the given token.
  88. *
  89. * The token is typically a numeric ID for the user.
  90. *
  91. * @param string $token
  92. * @param bool $remember
  93. * @return bool
  94. */
  95. public function login($token, $remember = false)
  96. {
  97. $this->token = $token;
  98. $this->store($token);
  99. if ($remember) $this->remember($token);
  100. Event::fire('laravel.auth: login');
  101. return true;
  102. }
  103. /**
  104. * Log the user out of the driver's auth context.
  105. *
  106. * @return void
  107. */
  108. public function logout()
  109. {
  110. $this->user = null;
  111. $this->cookie($this->recaller(), null, -2000);
  112. Session::forget($this->token());
  113. Event::fire('laravel.auth: logout');
  114. $this->token = null;
  115. }
  116. /**
  117. * Store a user's token in the session.
  118. *
  119. * @param string $token
  120. * @return void
  121. */
  122. protected function store($token)
  123. {
  124. Session::put($this->token(), $token);
  125. }
  126. /**
  127. * Store a user's token in a long-lived cookie.
  128. *
  129. * @param string $token
  130. * @return void
  131. */
  132. protected function remember($token)
  133. {
  134. $token = Crypter::encrypt($token.'|'.Str::random(40));
  135. $this->cookie($this->recaller(), $token, Cookie::forever);
  136. }
  137. /**
  138. * Attempt to find a "remember me" cookie for the user.
  139. *
  140. * @return string|null
  141. */
  142. protected function recall()
  143. {
  144. $cookie = Cookie::get($this->recaller());
  145. // By default, "remember me" cookies are encrypted and contain the user
  146. // token as well as a random string. If it exists, we'll decrypt it
  147. // and return the first segment, which is the user's ID token.
  148. if ( ! is_null($cookie))
  149. {
  150. return head(explode('|', Crypter::decrypt($cookie)));
  151. }
  152. }
  153. /**
  154. * Store an authentication cookie.
  155. *
  156. * @param string $name
  157. * @param string $value
  158. * @param int $minutes
  159. * @return void
  160. */
  161. protected function cookie($name, $value, $minutes)
  162. {
  163. // When setting the default implementation of an authentication
  164. // cookie we'll use the same settings as the session cookie.
  165. // This typically makes sense as they both are sensitive.
  166. $config = Config::get('session');
  167. extract($config);
  168. Cookie::put($name, $value, $minutes, $path, $domain, $secure);
  169. }
  170. /**
  171. * Get the session key name used to store the token.
  172. *
  173. * @return string
  174. */
  175. protected function token()
  176. {
  177. return $this->name().'_login';
  178. }
  179. /**
  180. * Get the name used for the "remember me" cookie.
  181. *
  182. * @return string
  183. */
  184. protected function recaller()
  185. {
  186. return Config::get('auth.cookie', $this->name().'_remember');
  187. }
  188. /**
  189. * Get the name of the driver in a storage friendly format.
  190. *
  191. * @return string
  192. */
  193. protected function name()
  194. {
  195. return strtolower(str_replace('\\', '_', get_class($this)));
  196. }
  197. }