driver.php 4.4 KB
Newer Older
1
<?php namespace Laravel\Auth\Drivers;
2 3 4

use Laravel\Str;
use Laravel\Cookie;
5
use Laravel\Config;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
use Laravel\Session;

abstract class Driver {

	/**
	 * The user currently being managed by the driver.
	 *
	 * @var mixed
	 */
	public $user;

	/**
	 * The current value of the user's token.
	 *
	 * @var string|null
	 */
	public $token;

	/**
	 * Create a new login auth driver instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		if (Session::started())
		{
			$this->token = Session::get($this->token());
		}

		// If a token did not exist in the session for the user, we will attempt
		// to load the value of a "remember me" cookie for the driver, which
		// serves as a long-lived client side authenticator for the user.
		if (is_null($this->token))
		{
			$this->token = $this->recall();
		}
	}

	/**
46 47 48 49 50 51 52 53 54 55 56 57
	 * Determine if the user of the application is not logged in.
	 *
	 * This method is the inverse of the "check" method.
	 *
	 * @return bool
	 */
	public function guest()
	{
		return ! $this->check();
	}

	/**
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	 * Determine if the user is logged in.
	 *
	 * @return bool
	 */
	public function check()
	{
		return ! is_null($this->user());
	}

	/**
	 * Get the current user of the application.
	 *
	 * If the user is a guest, null should be returned.
	 *
	 * @return mixed|null
	 */
74 75 76 77 78 79 80 81 82 83 84 85 86 87
	public function user()
	{
		if ( ! is_null($this->user)) return $this->user;

		return $this->user = $this->retrieve($this->token);
	}

	/**
	 * Get the a given application user by ID.
	 *
	 * @param  int    $id
	 * @return mixed
	 */
	abstract public function retrieve($id);
88 89 90 91

	/**
	 * Attempt to log a user into the application.
	 *
92
	 * @param  array  $arguments
93 94
	 * @return void
	 */
95
	abstract public function attempt($arguments = array());
96 97 98 99 100 101 102

	/**
	 * Login the user assigned to the given token.
	 *
	 * The token is typically a numeric ID for the user.
	 *
	 * @param  string  $token
103 104
	 * @param  bool    $remember
	 * @return bool
105
	 */
106
	public function login($token, $remember = false)
107
	{
Taylor Otwell committed
108 109
		$this->token = $token;

110
		$this->store($token);
111 112 113 114

		if ($remember) $this->remember($token);

		return true;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	}

	/**
	 * Log the user out of the driver's auth context.
	 *
	 * @return void
	 */
	public function logout()
	{
		$this->user = null;

		$this->cookie($this->recaller(), null, -2000);

		Session::forget($this->token());
	}

	/**
	 * Store a user's token in the session.
	 *
	 * @param  string  $token
	 * @return void
	 */
	protected function store($token)
	{
		Session::put($this->token(), $token);
	}

	/**
	 * Store a user's token in a long-lived cookie.
	 *
	 * @param  string  $token
	 * @return void
	 */
	protected function remember($token)
	{
		$token = Crypter::encrypt($token.'|'.Str::random(40));

		$this->cookie($this->recaller(), $token, Cookie::forever);
	}

	/**
	 * Attempt to find a "remember me" cookie for the user.
	 *
	 * @return string|null
	 */
	protected function recall()
	{
		$cookie = Cookie::get($this->recaller());

		// By default, "remember me" cookies are encrypted and contain the user
		// token as well as a random string. If it exists, we'll decrypt it
		// and return the first segment, which is the user's ID token.
		if ( ! is_null($cookie))
		{
			return head(explode('|', Crypter::decrypt($cookie)));
		}
	}

	/**
	 * Store an authentication cookie.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  int     $minutes
	 * @return void
	 */
	protected function cookie($name, $value, $minutes)
	{
		// When setting the default implementation of an authentication
		// cookie we'll use the same settings as the session cookie.
		// This typically makes sense as they both are sensitive.
		$config = Config::get('session');

		extract($config);

190
		Cookie::put($name, $minutes, $value, $path, $domain, $secure);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	}

	/**
	 * Get session key name used to store the token.
	 *
	 * @return string
	 */
	protected function token()
	{
		return $this->name().'_login';
	}

	/**
	 * Get the name used for the "remember me" cookie.
	 *
	 * @return string
	 */
	protected function recaller()
	{
		return $this->name().'_remember';
	}

	/**
	 * Get the name of the driver in a storage friendly format.
	 *
	 * @return string
	 */
	protected function name()
	{
		return strtolower(str_replace('\\', '_', get_class($this)));
	}

}