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