driver.php 4.4 KB

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